Skip to content

Commit

Permalink
Support null.
Browse files Browse the repository at this point in the history
  • Loading branch information
KmolYuan committed May 3, 2021
1 parent bd4d1c6 commit e875e94
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ rym pack

## Features

Here are the implemented features, or the functions are designed in progress. Generally, the parser will not check extra key values.
Here are the implemented features, or the functions are designed in progress. Generally, the parser will not check extra key values. In addition, almost all values support the `null` placeholder to present the default value (blank, `~`, `null`).

Some functions are planed to be demonstrated in the help page. Open the help page by adding `/help/` after URL, like `http://localhost:8080/help/`.

Expand Down
28 changes: 20 additions & 8 deletions src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,44 +60,47 @@ pub(crate) trait Unpack {

impl Unpack for Hash {
fn get_bool(&self, key: &str, default: bool, (i, j): Pos) -> Result<bool> {
match self.get(yaml_str![key]).unwrap_or(yaml_bool![default]) {
match self.get(yaml_str![key]).unwrap_or(yaml_null![]) {
Yaml::Boolean(b) => Ok(*b),
Yaml::Null => Ok(default),
_ => err!(format!("wrong {}: must be boolean ({}:{})", key, i, j)),
}
}
fn get_string(&self, key: &str, default: &str, (i, j): Pos) -> Result<String> {
match self.get(yaml_str![key]).unwrap_or(yaml_str![default]) {
match self.get(yaml_str![key]).unwrap_or(yaml_null![]) {
Yaml::String(s) => Ok(s.clone()),
Yaml::Null => Ok(String::from(default)),
_ => err!(format!("wrong {}: must be string ({}:{})", key, i, j)),
}
}
fn get_value(&self, key: &str, default: &str, (i, j): Pos) -> Result<String> {
match self.get(yaml_str![key]).unwrap_or(yaml_str![default]) {
match self.get(yaml_str![key]).unwrap_or(yaml_null![]) {
Yaml::Real(s) | Yaml::String(s) => Ok(s.clone()),
Yaml::Integer(v) => Ok(v.to_string()),
Yaml::Null => Ok(String::from(default)),
_ => err!(format!(
"wrong {}: must be integer, float or string ({}:{})",
key, i, j
)),
}
}
fn get_vec(&self, key: &str, (i, j): Pos) -> Result<(Iter<Yaml>, usize)> {
match self.get(yaml_str![key]).unwrap_or(yaml_bad![]) {
match self.get(yaml_str![key]).unwrap_or(yaml_null![]) {
Yaml::Array(a) => Ok((a.iter(), a.len())),
Yaml::BadValue => Ok(([].iter(), 0)),
Yaml::Null => Ok(([].iter(), 0)),
_ => err!(format!("wrong {}: must be array ({}:{})", key, i, j)),
}
}
fn get_custom_pairs(&self, key: &str, (i, j): Pos) -> Result<String> {
match self.get(yaml_str![key]).unwrap_or(yaml_bad![]) {
match self.get(yaml_str![key]).unwrap_or(yaml_null![]) {
Yaml::Hash(h) => {
let mut doc = String::new();
for (k, v) in h.custom_pairs(false, (i, j))? {
doc += &format!("{}: {}, ", k, v);
}
Ok(doc)
}
Yaml::BadValue => Ok("".into()),
Yaml::Null => Ok("".into()),
_ => err!(format!("wrong {}: must be map ({}:{})", key, i, j)),
}
}
Expand Down Expand Up @@ -238,7 +241,16 @@ pub(crate) fn content_block(slide: &Hash, pos: Pos, frag_count: &mut usize) -> R
if !imgs.is_empty() {
doc += "<div class=\"img-row\">";
for img in imgs {
doc += &frag.fragment("img", &img_block(&img.as_hash().unwrap(), pos)?);
doc += &frag.fragment(
"img",
&img_block(
img.assert_hash(&format!(
"wrong img: must be map {}:{}",
pos.0, pos.1
))?,
pos,
)?,
);
}
doc += "</div>";
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ macro_rules! get_archive {
}};
}

macro_rules! yaml_bad {
[] => { &yaml_rust::Yaml::BadValue };
macro_rules! yaml_null {
[] => { &yaml_rust::Yaml::Null };
}

macro_rules! yaml_bool {
Expand Down
8 changes: 4 additions & 4 deletions src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ struct Background {
impl Background {
fn new(meta: &Hash) -> Result<Self> {
let h = Hash::new();
let bg = match meta.get(yaml_str!["background"]).unwrap_or(yaml_bad![]) {
let bg = match meta.get(yaml_str!["background"]).unwrap_or(yaml_null![]) {
Yaml::Hash(h) => h,
Yaml::BadValue => &h,
Yaml::Null => &h,
_ => return err!("background must be hash"),
};
Ok(Self {
Expand Down Expand Up @@ -91,9 +91,9 @@ fn slide_block(slide: &Hash, bg: &Background, pos: Pos) -> Result<String> {

fn footer_block(meta: &Hash) -> Result<String> {
let h = Hash::new();
let footer = match meta.get(yaml_str!["footer"]).unwrap_or(yaml_bad![]) {
let footer = match meta.get(yaml_str!["footer"]).unwrap_or(yaml_null![]) {
Yaml::Hash(h) => h,
Yaml::BadValue => &h,
Yaml::Null => &h,
_ => return err!("invalid footer"),
};
let src = footer.get_string("src", "", (0, 0))?;
Expand Down

0 comments on commit e875e94

Please sign in to comment.