Skip to content

Commit 40a307a

Browse files
Merge pull request #17 from Kijewski/pr-no-fmt-error
Don't capture `std::fmt::Error` in `rinja::Error`
2 parents 435ecc2 + ad645e7 commit 40a307a

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

rinja/src/error.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::convert::Infallible;
22
use std::fmt::{self, Display};
33

4-
pub type Result<I, E = Error> = ::std::result::Result<I, E>;
4+
pub type Result<I, E = Error> = std::result::Result<I, E>;
55

66
/// rinja error type
77
///
@@ -27,20 +27,18 @@ pub type Result<I, E = Error> = ::std::result::Result<I, E>;
2727
#[derive(Debug)]
2828
pub enum Error {
2929
/// formatting error
30-
Fmt(fmt::Error),
31-
30+
Fmt,
3231
/// an error raised by using `?` in a template
3332
Custom(Box<dyn std::error::Error + Send + Sync>),
34-
3533
/// json conversion error
3634
#[cfg(feature = "serde_json")]
37-
Json(::serde_json::Error),
35+
Json(serde_json::Error),
3836
}
3937

4038
impl std::error::Error for Error {
4139
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
4240
match *self {
43-
Error::Fmt(ref err) => Some(err),
41+
Error::Fmt => None,
4442
Error::Custom(ref err) => Some(err.as_ref()),
4543
#[cfg(feature = "serde_json")]
4644
Error::Json(ref err) => Some(err),
@@ -51,7 +49,7 @@ impl std::error::Error for Error {
5149
impl Display for Error {
5250
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
5351
match self {
54-
Error::Fmt(err) => write!(formatter, "formatting error: {err}"),
52+
Error::Fmt => write!(formatter, "formatting error"),
5553
Error::Custom(err) => write!(formatter, "{err}"),
5654
#[cfg(feature = "serde_json")]
5755
Error::Json(err) => write!(formatter, "json conversion error: {err}"),
@@ -60,14 +58,16 @@ impl Display for Error {
6058
}
6159

6260
impl From<fmt::Error> for Error {
63-
fn from(err: fmt::Error) -> Self {
64-
Error::Fmt(err)
61+
#[inline]
62+
fn from(_: fmt::Error) -> Self {
63+
Error::Fmt
6564
}
6665
}
6766

6867
#[cfg(feature = "serde_json")]
69-
impl From<::serde_json::Error> for Error {
70-
fn from(err: ::serde_json::Error) -> Self {
68+
impl From<serde_json::Error> for Error {
69+
#[inline]
70+
fn from(err: serde_json::Error) -> Self {
7171
Error::Json(err)
7272
}
7373
}

rinja/src/filters/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ use dep_num_traits::{cast::NumCast, Signed};
2222
use percent_encoding::{utf8_percent_encode, AsciiSet, NON_ALPHANUMERIC};
2323
use rinja_escape::{Escaper, MarkupDisplay};
2424

25-
use super::Result;
26-
#[allow(unused_imports)]
27-
use crate::error::Error::Fmt;
25+
use crate::{Error, Result};
2826

2927
#[cfg(feature = "percent-encoding")]
3028
// Urlencode char encoding set. Only the characters in the unreserved set don't
@@ -401,7 +399,7 @@ pub fn into_f64<T>(number: T) -> Result<f64>
401399
where
402400
T: NumCast,
403401
{
404-
number.to_f64().ok_or(Fmt(fmt::Error))
402+
number.to_f64().ok_or(Error::Fmt)
405403
}
406404

407405
#[cfg(feature = "num-traits")]
@@ -410,7 +408,7 @@ pub fn into_isize<T>(number: T) -> Result<isize>
410408
where
411409
T: NumCast,
412410
{
413-
number.to_isize().ok_or(Fmt(fmt::Error))
411+
number.to_isize().ok_or(Error::Fmt)
414412
}
415413

416414
/// Joins iterable into a string separated by provided argument
@@ -728,8 +726,8 @@ mod tests {
728726
assert_eq!(into_isize(1.5_f64).unwrap(), 1_isize);
729727
assert_eq!(into_isize(-1.5_f64).unwrap(), -1_isize);
730728
match into_isize(f64::INFINITY) {
731-
Err(Fmt(fmt::Error)) => {}
732-
_ => panic!("Should return error of type Err(Fmt(fmt::Error))"),
729+
Err(Error::Fmt) => {}
730+
_ => panic!("Should return error of type Err(Error::Fmt)"),
733731
};
734732
}
735733

rinja_derive/src/generator.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,9 +1599,9 @@ impl<'a> Generator<'a> {
15991599
buf.writeln(");");
16001600
buf.writeln("let _len = _cycle.len();");
16011601
buf.writeln("if _len == 0 {");
1602-
buf.write("return ::core::result::Result::Err(");
1603-
buf.write(CRATE);
1604-
buf.writeln("::Error::Fmt(::core::fmt::Error));");
1602+
buf.writeln(format_args!(
1603+
"return ::core::result::Result::Err({CRATE}::Error::Fmt);"
1604+
));
16051605
buf.writeln("}");
16061606
buf.writeln("_cycle[_loop_item.index % _len]");
16071607
buf.writeln("})");

0 commit comments

Comments
 (0)