8000 feat(mj-include): create an include-loader to load from the filesystem by jdrouet · Pull Request #32 · jolimail/mrml-core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Jul 25, 2023. It is now read-only.

feat(mj-include): create an include-loader to load from the filesystem #32

Merged
merged 2 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ parse = ["mrml-parse-macros", "xmlparser"]
print = ["mrml-print-macros"]
render = ["rand"]
orderedmap = ["indexmap", "rustc-hash"]
local-loader = []

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
61 changes: 60 additions & 1 deletion src/prelude/parse/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use xmlparser::Token;
pub struct IncludeLoaderError {
pub path: String,
pub reason: ErrorKind,
pub message: Option<&'static str>,
pub cause: Option<Box<dyn std::error::Error>>,
}

Expand All @@ -22,6 +23,7 @@ impl IncludeLoaderError {
Self {
path: path.to_string(),
reason,
message: None,
cause: None,
}
}
Expand All @@ -30,14 +32,33 @@ impl IncludeLoaderError {
Self {
path: path.to_string(),
reason: ErrorKind::NotFound,
message: None,
cause: None,
}
}

pub fn with_message(mut self, message: &'static str) -> Self {
self.message = Some(message);
self
}

pub fn with_cause(mut self, cause: Box<dyn std::error::Error>) -> Self {
self.cause = Some(cause);
self
}
}

impl std::fmt::Display for IncludeLoaderError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Unable to load template {}: {}", self.path, self.reason)
if let Some(msg) = self.message {
write!(
f,
"Unable to load template {}: {} ({})",
self.path, msg, self.reason
)
} else {
write!(f, "Unable to load template {}: {}", self.path, self.reason)
}
}
}

Expand Down Expand Up @@ -67,3 +88,41 @@ pub fn parse<T: Parsable + From<Comment> + From<Text>>(
_ => Err(Error::InvalidFormat),
}
}

#[cfg(test)]
mod tests {
use std::io::ErrorKind;

use super::IncludeLoaderError;

#[test]
fn should_display_basic() {
assert_eq!(
IncludeLoaderError::new("foo.mjml", ErrorKind::NotFound).to_string(),
"Unable to load template foo.mjml: entity not found",
);
}

#[test]
fn should_display_with_message() {
assert_eq!(
IncludeLoaderError::new("foo.mjml", ErrorKind::NotFound)
.with_message("oops")
.to_string(),
"Unable to load template foo.mjml: oops (entity not found)",
);
}

#[test]
fn should_display_with_cause() {
assert_eq!(
IncludeLoaderError::new("foo.mjml", ErrorKind::NotFound)
.with_cause(Box::new(IncludeLoaderError::new(
"bar.mjml",
ErrorKind::InvalidInput
)))
.to_string(),
"Unable to load template foo.mjml: entity not found",
);
}
}
149 changes: 149 additions & 0 deletions src/prelude/parse/local_loader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
//! Module containing a loader where all the possible files are stored on the filesystem.

use super::loader::IncludeLoaderError;
use crate::prelude::parse::loader::IncludeLoader;
use std::{io::ErrorKind, path::PathBuf};

#[derive(Debug, Default)]
/// This struct is an [`IncludeLoader`](crate::prelude::parse::loader::IncludeLoader) where
/// you can read a template for the filesystem and be able to use it with [`mj-include`](crate::mj_include).
///
/// # Example
/// ```rust
/// use std::path::PathBuf;
/// use std::rc::Rc;
/// use mrml::mj_include::body::MjIncludeBodyKind;
/// use mrml::prelude::parse::local_loader::LocalIncludeLoader;
/// use mrml::prelude::parse::ParserOptions;
///
/// let root = PathBuf::default()
/// .join("resources")
/// .join("compare")
/// .join("success");
/// let resolver = LocalIncludeLoader::new(root);
/// let opts = ParserOptions {
/// include_loader: Box::new(resolver),
/// };
/// let template = r#"<mjml>
/// <mj-body>
/// <mj-include path="file:///mj-accordion.mjml" />
/// </mj-body>
/// </mjml>"#;
/// match mrml::parse_with_options(template, Rc::new(opts)) {
/// Ok(_) => println!("Success!"),
/// Err(err) => eprintln!("Couldn't parse template: {err:?}"),
/// }
/// ```
///
/// About the security: this loader doesn't allow to go fetch a template that
/// is in a parent directory of the root directory.
pub struct LocalIncludeLoader {
root: PathBuf,
}

impl LocalIncludeLoader {
pub fn new(root: PathBuf) -> Self {
Self { root }
}

fn build_path(&self, url: &str) -> Result<PathBuf, IncludeLoaderError> {
let path = url
.strip_prefix("file:///")
.map(|p| self.root.join(p))
.ok_or_else(|| {
IncludeLoaderError::new(url, ErrorKind::InvalidInput)
.with_message("the path should start with file:///")
})?;
path.canonicalize()
.map_err(|err| IncludeLoaderError::new(url, err.kind()))
.and_then(|path| {
if !path.starts_with(&self.root) {
Err(IncludeLoaderError::new(url, ErrorKind::NotFound))
} else {
Ok(path)
}
})
.map_err(|err| err.with_message("the path should stay in the context of the loader"))
}
}

impl IncludeLoader for LocalIncludeLoader {
fn resolve(&self, url: &str) -> Result<String, IncludeLoaderError> {
let path = self.build_path(url)?;
std::fs::read_to_string(path).map_err(|err| {
IncludeLoaderError::new(url, ErrorKind::InvalidData)
.with_message("unable to load the template file")
.with_cause(Box::new(err))
})
}
}

#[cfg(test)]
mod tests {
use super::LocalIncludeLoader;
use crate::prelude::parse::loader::IncludeLoader;
use std::{io::ErrorKind, path::PathBuf};

impl LocalIncludeLoader {
fn current_dir() -> Self {
Self::new(PathBuf::from(std::env::var("PWD").unwrap()))
}
}

#[test]
fn should_start_with_file() {
let loader = LocalIncludeLoader::default();
let err = loader
.build_path("/resources/compare/success/mj-body.mjml")
.unwrap_err();

assert_eq!(err.reason, ErrorKind::InvalidInput);
assert_eq!(err.to_string(), "Unable to load template /resources/compare/success/mj-body.mjml: the path should start with file:/// (invalid input parameter)");
}

#[test]
fn should_turn_into_path() {
let loader = LocalIncludeLoader::current_dir();
let path = loader
.build_path("file:///resources/compare/success/mj-body.mjml")
.unwrap();

assert_eq!(
path.as_os_str(),
format!(
"{}/resources/compare/success/mj-body.mjml",
loader.root.to_string_lossy()
)
.as_str()
);
}

#[test]
fn should_handle_dots_with_existing_file() {
let loader = LocalIncludeLoader::new(PathBuf::default().join("src"));

let err = loader
.build_path("file:///../resources/compare/success/mj-body.mjml")
.unwrap_err();

assert_eq!(err.reason, ErrorKind::NotFound);
}

#[test]
fn should_handle_dots_with_missing_file() {
let loader = LocalIncludeLoader::new(PathBuf::default().join("src"));

let err = loader.build_path("file:///../partial.mjml").unwrap_err();

assert_eq!(err.reason, ErrorKind::NotFound);
assert_eq!(err.to_string(), "Unable to load template file:///../partial.mjml: the path should stay in the context of the loader (entity not found)");
}

#[test]
fn should_resolve_path() {
let loader = LocalIncludeLoader::current_dir();
let _payload = loader
.resolve("file:///resources/compare/success/mj-body.mjml")
.unwrap();
}
}
2 changes: 2 additions & 0 deletions src/prelude/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use xmlparser::{StrSpan, Token, Tokenizer};
use self::loader::IncludeLoaderError;

pub mod loader;
#[cfg(feature = "local-loader")]
pub mod local_loader;
pub mod memory_loader;
pub mod noop_loader;

Expand Down
0