Skip to content

Commit

Permalink
add error macro
Browse files Browse the repository at this point in the history
  • Loading branch information
jonian committed Dec 1, 2023
1 parent 0532b3d commit 1c2a343
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions ext/mrml/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ fn mrml_error() -> ExceptionClass {
})
}

macro_rules! error {
($ex:ident) => {
Error::new(mrml_error(), $ex.to_string())
};
}

#[magnus::wrap(class = "MRML::Template", free_immediately, size)]
struct Template {
res: MJML
Expand All @@ -25,14 +31,14 @@ impl Template {
fn new(input: String) -> Result<Self, Error> {
match mrml::parse(&input) {
Ok(res) => Ok(Self { res }),
Err(ex) => Err(Error::new(mrml_error(), ex.to_string()))
Err(ex) => Err(error!(ex))
}
}

fn from_json(input: String) -> Result<Self, Error> {
match serde_json::from_str::<MJML>(&input) {
Ok(res) => Ok(Self { res }),
Err(ex) => Err(Error::new(mrml_error(), ex.to_string()))
Err(ex) => Err(error!(ex))
}
}

Expand All @@ -51,14 +57,14 @@ impl Template {
fn to_json(&self) -> Result<String, Error> {
match serde_json::to_string(&self.res) {
Ok(res) => Ok(res),
Err(ex) => Err(Error::new(mrml_error(), ex.to_string()))
Err(ex) => Err(error!(ex))
}
}

fn to_html(&self) -> Result<String, Error> {
match self.res.render(&Options::default()) {
Ok(res) => Ok(res),
Err(ex) => Err(Error::new(mrml_error(), ex.to_string()))
Err(ex) => Err(error!(ex))
}
}
}
Expand Down

0 comments on commit 1c2a343

Please sign in to comment.