summaryrefslogtreecommitdiff
path: root/src/skaldpress/metadata_parser.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/skaldpress/metadata_parser.rs')
-rw-r--r--src/skaldpress/metadata_parser.rs23
1 files changed, 19 insertions, 4 deletions
diff --git a/src/skaldpress/metadata_parser.rs b/src/skaldpress/metadata_parser.rs
index 77ee7b8..4d6e352 100644
--- a/src/skaldpress/metadata_parser.rs
+++ b/src/skaldpress/metadata_parser.rs
@@ -16,10 +16,21 @@ impl fmt::Display for YamlValue {
}
}
+impl TryFrom<&YamlValue> for String {
+ type Error = String;
+
+ fn try_from(s: &YamlValue) -> Result<String, String> {
+ match s {
+ YamlValue::Scalar(s) => Ok(String::from(s)),
+ YamlValue::List(_) => Err(String::from("List value cannot be turned into string")),
+ }
+ }
+}
+
/// Extract a beginning YAML block of a input-string into a HashMap
-///
-/// Currently, all files MUST have a metadata block, or this will fail completely
-pub fn extract_parse_yaml_metadata<'a>(markdown: &'a str) -> (HashMap<String, YamlValue>, &'a str) {
+pub fn extract_parse_yaml_metadata<'a>(
+ markdown: &'a str,
+) -> Option<(HashMap<String, YamlValue>, &'a str)> {
let mut yaml_map = HashMap::new();
let lines = markdown.lines();
let mut yaml_started = false;
@@ -28,6 +39,10 @@ pub fn extract_parse_yaml_metadata<'a>(markdown: &'a str) -> (HashMap<String, Ya
let mut current_key: Option<String> = None;
let mut current_list: Vec<String> = Vec::new();
+ if lines.clone().next().unwrap_or("").trim() != "---" {
+ return None;
+ }
+
for (i, line) in lines.enumerate() {
if line.trim() == "---" {
if yaml_started {
@@ -69,5 +84,5 @@ pub fn extract_parse_yaml_metadata<'a>(markdown: &'a str) -> (HashMap<String, Ya
end_index = markdown.len();
}
- (yaml_map, &markdown[end_index..])
+ Some((yaml_map, &markdown[end_index..]))
}