Skip to content

Commit

Permalink
Merge pull request #17 from Kijewski/pr-no-fmt-error
Browse files Browse the repository at this point in the history
Don't capture `std::fmt::Error` in `rinja::Error`
  • Loading branch information
GuillaumeGomez authored Jun 18, 2024
2 parents 435ecc2 + ad645e7 commit 40a307a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
22 changes: 11 additions & 11 deletions rinja/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::convert::Infallible;
use std::fmt::{self, Display};

pub type Result<I, E = Error> = ::std::result::Result<I, E>;
pub type Result<I, E = Error> = std::result::Result<I, E>;

/// rinja error type
///
Expand All @@ -27,20 +27,18 @@ pub type Result<I, E = Error> = ::std::result::Result<I, E>;
#[derive(Debug)]
pub enum Error {
/// formatting error
Fmt(fmt::Error),

Fmt,
/// an error raised by using `?` in a template
Custom(Box<dyn std::error::Error + Send + Sync>),

/// json conversion error
#[cfg(feature = "serde_json")]
Json(::serde_json::Error),
Json(serde_json::Error),
}

impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self {
Error::Fmt(ref err) => Some(err),
Error::Fmt => None,
Error::Custom(ref err) => Some(err.as_ref()),
#[cfg(feature = "serde_json")]
Error::Json(ref err) => Some(err),
Expand All @@ -51,7 +49,7 @@ impl std::error::Error for Error {
impl Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Fmt(err) => write!(formatter, "formatting error: {err}"),
Error::Fmt => write!(formatter, "formatting error"),
Error::Custom(err) => write!(formatter, "{err}"),
#[cfg(feature = "serde_json")]
Error::Json(err) => write!(formatter, "json conversion error: {err}"),
Expand All @@ -60,14 +58,16 @@ impl Display for Error {
}

impl From<fmt::Error> for Error {
fn from(err: fmt::Error) -> Self {
Error::Fmt(err)
#[inline]
fn from(_: fmt::Error) -> Self {
Error::Fmt
}
}

#[cfg(feature = "serde_json")]
impl From<::serde_json::Error> for Error {
fn from(err: ::serde_json::Error) -> Self {
impl From<serde_json::Error> for Error {
#[inline]
fn from(err: serde_json::Error) -> Self {
Error::Json(err)
}
}
Expand Down
12 changes: 5 additions & 7 deletions rinja/src/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ use dep_num_traits::{cast::NumCast, Signed};
use percent_encoding::{utf8_percent_encode, AsciiSet, NON_ALPHANUMERIC};
use rinja_escape::{Escaper, MarkupDisplay};

use super::Result;
#[allow(unused_imports)]
use crate::error::Error::Fmt;
use crate::{Error, Result};

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

#[cfg(feature = "num-traits")]
Expand All @@ -410,7 +408,7 @@ pub fn into_isize<T>(number: T) -> Result<isize>
where
T: NumCast,
{
number.to_isize().ok_or(Fmt(fmt::Error))
number.to_isize().ok_or(Error::Fmt)
}

/// Joins iterable into a string separated by provided argument
Expand Down Expand Up @@ -728,8 +726,8 @@ mod tests {
assert_eq!(into_isize(1.5_f64).unwrap(), 1_isize);
assert_eq!(into_isize(-1.5_f64).unwrap(), -1_isize);
match into_isize(f64::INFINITY) {
Err(Fmt(fmt::Error)) => {}
_ => panic!("Should return error of type Err(Fmt(fmt::Error))"),
Err(Error::Fmt) => {}
_ => panic!("Should return error of type Err(Error::Fmt)"),
};
}

Expand Down
6 changes: 3 additions & 3 deletions rinja_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1599,9 +1599,9 @@ impl<'a> Generator<'a> {
buf.writeln(");");
buf.writeln("let _len = _cycle.len();");
buf.writeln("if _len == 0 {");
buf.write("return ::core::result::Result::Err(");
buf.write(CRATE);
buf.writeln("::Error::Fmt(::core::fmt::Error));");
buf.writeln(format_args!(
"return ::core::result::Result::Err({CRATE}::Error::Fmt);"
));
buf.writeln("}");
buf.writeln("_cycle[_loop_item.index % _len]");
buf.writeln("})");
Expand Down

0 comments on commit 40a307a

Please sign in to comment.