Skip to content

Commit

Permalink
Restore get_entity function
Browse files Browse the repository at this point in the history
  • Loading branch information
Folyd committed Jul 3, 2023
1 parent 291b3ce commit 7a2da94
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ impl ZineData {
self
}

pub fn get_authors(&self) -> Vec<&Author> {
self.authors.iter().by_ref().collect()
}

pub fn get_author_by_id(&self, author_id: &str) -> Option<&Author> {
self.authors
.iter()
Expand Down
14 changes: 14 additions & 0 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ impl Generator for ZineGenerator {
}

env.add_function("load_json", load_json);
env.add_function("get_entity", get_entity);
env.add_function("get_author", get_author_function);
let fluent_loader = FluentLoader::new(source, &zine.site.locale);
env.add_function("fluent", move |key: &str, number: Option<i64>| -> String {
Expand Down Expand Up @@ -226,6 +227,19 @@ fn get_author_function(id: &str) -> JinjaValue {
JinjaValue::from_serializable(&author)
}

fn get_entity(name: &str) -> Result<JinjaValue, JinjaError> {
match name {
"authors" => {
let data = data::read();
Ok(JinjaValue::from_serializable(&data.get_authors()))
}
_ => Err(JinjaError::new(
ErrorKind::NonKey,
format!("invalid entity name `{name}` for `get_entity` function."),
)),
}
}

static DATA_JSON: OnceCell<RwLock<HashMap<String, JinjaValue>>> = OnceCell::new();

fn load_json(filename: &str) -> Result<JinjaValue, JinjaError> {
Expand Down

0 comments on commit 7a2da94

Please sign in to comment.