summaryrefslogtreecommitdiff
path: root/src/model/content.rs
diff options
context:
space:
mode:
authorMichael Lohr <michael@lohr.dev>2023-05-03 12:34:35 +0200
committerGitHub <noreply@github.com>2023-05-03 12:34:35 +0200
commitffad8516af0b91121dc0761c8026e0a12939a7d4 (patch)
treeada5c6b5510b5f509997ccf5308a9cafc8618990 /src/model/content.rs
parentca8462642a96ec282afeb0774b6d5daf546ac236 (diff)
Implement default values for at() (#995)
Diffstat (limited to 'src/model/content.rs')
-rw-r--r--src/model/content.rs16
1 files changed, 11 insertions, 5 deletions
diff --git a/src/model/content.rs b/src/model/content.rs
index 4af4e655..1bd19f14 100644
--- a/src/model/content.rs
+++ b/src/model/content.rs
@@ -252,8 +252,10 @@ impl Content {
}
/// Borrow the value of the given field.
- pub fn at(&self, field: &str) -> StrResult<Value> {
- self.field(field).ok_or_else(|| missing_field(field))
+ pub fn at(&self, field: &str, default: Option<Value>) -> StrResult<Value> {
+ self.field(field)
+ .or(default)
+ .ok_or_else(|| missing_field_no_default(field))
}
/// The content's label.
@@ -582,8 +584,12 @@ impl Fold for Vec<Meta> {
}
}
-/// The missing key access error message.
+/// The missing key access error message when no default value was given.
#[cold]
-fn missing_field(key: &str) -> EcoString {
- eco_format!("content does not contain field {:?}", Str::from(key))
+fn missing_field_no_default(key: &str) -> EcoString {
+ eco_format!(
+ "content does not contain field {:?} and \
+ no default value was specified",
+ Str::from(key)
+ )
}