Skip to content

Commit

Permalink
feat: parsing warnings and improve errors (#451)
Browse files Browse the repository at this point in the history
* fix: make cargo hack pass

Signed-off-by: Jérémie Drouet <[email protected]>

* feat!(mrml-core): introduce parse output struct with warnings

Signed-off-by: Jérémie Drouet <[email protected]>

* refactor(mrml-core): update warning struct

Signed-off-by: Jérémie Drouet <[email protected]>

* feat(mrml-wasm): output parsing warnings

Signed-off-by: Jérémie Drouet <[email protected]>

* feat!(mrml-python): return generated html and warnings

Signed-off-by: Jérémie Drouet <[email protected]>

* fix(example): return warnings

Signed-off-by: Jérémie Drouet <[email protected]>

* feat(mrml-cli): log warnings when validating

Signed-off-by: Jérémie Drouet <[email protected]>

* refactor(mrml-core): warning split in warning kind

Signed-off-by: Jérémie Drouet <[email protected]>

* fix(mrml-core): remove Default for Span

Signed-off-by: Jérémie Drouet <[email protected]>

* feat(mrml-core): add origin in warnings

Signed-off-by: Jérémie Drouet <[email protected]>

* fix(mrml-core): update bench code according to changes

Signed-off-by: Jérémie Drouet <[email protected]>

* fix(mrml-wasm): add check on warnings

Signed-off-by: Jérémie Drouet <[email protected]>

* feat(mrml-core): add origin to parsing errors

Signed-off-by: Jérémie Drouet <[email protected]>

* fix(mrml-cli): better format error

Signed-off-by: Jérémie Drouet <[email protected]>

* refactor(mrml-core): wrap errors into named attributes

Signed-off-by: Jérémie Drouet <[email protected]>

* refactor(mrml-cli): use error attributes

Signed-off-by: Jérémie Drouet <[email protected]>

* fix(mrml-wasm): update expected error message

Signed-off-by: Jérémie Drouet <[email protected]>

---------

Signed-off-by: Jérémie Drouet <[email protected]>
  • Loading branch information
jdrouet authored Aug 2, 2024
1 parent 791f458 commit 977aa64
Show file tree
Hide file tree
Showing 78 changed files with 1,538 additions and 709 deletions.
44 changes: 38 additions & 6 deletions examples/axum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,20 @@ impl Default for Engine {
}

impl Engine {
async fn handle(&self, input: String) -> Result<String, EngineError> {
mrml::async_parse_with_options(input, self.parser.clone())
async fn handle(&self, input: String) -> Result<Response, EngineError> {
let item = mrml::async_parse_with_options(input, self.parser.clone())
.await
.map_err(EngineError::Parse)
.and_then(|mjml| mjml.render(&self.render).map_err(EngineError::Render))
.map_err(EngineError::Parse)?;

let content = item
.element
.render(&self.render)
.map_err(EngineError::Render)?;

Ok(Response {
content,
warnings: item.warnings.into_iter().map(Warning::from).collect(),
})
}
}

Expand All @@ -97,12 +106,35 @@ struct Payload {
template: String,
}

#[derive(Debug, serde::Serialize)]
struct Response {
content: String,
warnings: Vec<Warning>,
}

#[derive(Debug, serde::Serialize)]
struct Warning {
code: &'static str,
start: usize,
end: usize,
}

impl From<mrml::prelude::parser::Warning> for Warning {
fn from(value: mrml::prelude::parser::Warning) -> Self {
Warning {
code: value.kind.as_str(),
start: value.span.start,
end: value.span.end,
}
}
}

#[axum::debug_handler]
async fn handler(
State(engine): State<Engine>,
Json(payload): Json<Payload>,
) -> Result<String, EngineError> {
engine.handle(payload.template).await
) -> Result<Json<Response>, EngineError> {
engine.handle(payload.template).await.map(Json)
}

fn create_app() -> axum::Router {
Expand Down
4 changes: 2 additions & 2 deletions packages/mrml-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ name = "mrml"

[dependencies]
mrml = { version = "4.0.1", path = "../mrml-core", features = [
"http-loader-ureq",
"local-loader",
"http-loader-ureq",
"local-loader",
] }
clap = { version = "4.5", features = ["derive"] }
env_logger = "0.11"
Expand Down
Loading

0 comments on commit 977aa64

Please sign in to comment.