1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
use self::super::{ScriptElement, StyleElement, LanguageTag, TagName}; use toml::de::from_str as from_toml_str; use std::collections::BTreeMap; use self::super::super::Error; use std::default::Default; use std::path::PathBuf; use std::fs::File; use std::io::Read; /// Generic post metadata. #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct PostMetadata { /// Post language override. /// /// If not present, default post language is used. pub language: Option<LanguageTag>, /// Post author override. /// /// If not present, default post author is used. pub author: Option<String>, /// A set of tags. /// /// If not present, defaults to empty. pub tags: Vec<TagName>, /// A set of style descriptors. /// /// If not present, defaults to empty. pub styles: Vec<StyleElement>, /// A set of style descriptors. /// /// If not present, defaults to empty. pub scripts: Vec<ScriptElement>, /// Additional static data to substitute in header and footer. /// /// If not present, defaults to empty. pub data: BTreeMap<String, String>, } #[derive(Deserialize)] struct PostMetadataSerialised { pub language: Option<LanguageTag>, pub author: Option<String>, pub tags: Option<Vec<TagName>>, pub styles: Option<Vec<StyleElement>>, pub scripts: Option<Vec<ScriptElement>>, pub data: Option<BTreeMap<String, String>>, } impl PostMetadata { /// Read the post metadata from the specified root directory. /// /// If the metadata file doesn't exist, `Ok(Default::default())` is returned. /// /// # Examples /// /// Given `$POST_ROOT/metadata.toml` containing: /// /// ```toml /// language = "pl" /// tags = ["vodka", "depression"] /// /// [[scripts]] /// class = "link" /// data = "/content/assets/syllable.js" /// /// [[scripts]] /// class = "file" /// data = "MathJax-config.js" /// /// [data] /// desc = "Każdy koniec to nowy początek [PL]" /// ``` /// /// The following holds: /// /// ``` /// # use bloguen::ops::{ScriptElement, PostMetadata}; /// # use std::fs::{self, File}; /// # use std::env::temp_dir; /// # use std::io::Write; /// # let post_root = temp_dir().join("bloguen-doctest").join("ops-metadata-read_or_default-0"); /// # fs::create_dir_all(&post_root).unwrap(); /// # File::create(post_root.join("metadata.toml")).unwrap().write_all(r#" /// # language = "pl" /// # tags = ["vodka", "depression"] /// # /// # [[scripts]] /// # class = "link" /// # data = "/content/assets/syllable.js" /// # /// # [[scripts]] /// # class = "file" /// # data = "MathJax-config.js" /// # /// # [data] /// # desc = "Każdy koniec to nowy początek [PL]" /// # "#.as_bytes()).unwrap(); /// # /* /// let post_root: PathBuf = /* obtained elsewhere */; /// # */ /// let metadata = /// PostMetadata::read_or_default(&("$POST_ROOT/".to_string(), post_root.clone())).unwrap(); /// assert_eq!(metadata, /// PostMetadata { /// language: Some("pl".parse().unwrap()), /// author: None, /// tags: vec!["vodka".parse().unwrap(), "depression".parse().unwrap()], /// styles: vec![], /// scripts: vec![ScriptElement::from_link("/content/assets/syllable.js"), /// ScriptElement::from_path("MathJax-config.js")], /// data: vec![("desc".to_string(), /// "Każdy koniec to nowy początek [PL]".to_string())] /// .into_iter().collect(), /// }); /// ``` pub fn read_or_default(post_root: &(String, PathBuf)) -> Result<PostMetadata, Error> { let mut buf = String::new(); if let Ok(f) = File::open(post_root.1.join("metadata.toml")) { f } else { return Ok(Default::default()); }.read_to_string(&mut buf) .map_err(|_| { Error::Io { desc: "post metadata".into(), op: "read", more: "not UTF-8".into(), } })?; let serialised: PostMetadataSerialised = from_toml_str(&buf).map_err(move |err| { Error::FileParsingFailed { desc: "post metadata".into(), errors: err.to_string().into(), } })?; Ok(PostMetadata { language: serialised.language, author: serialised.author, tags: serialised.tags.unwrap_or_default(), styles: serialised.styles.unwrap_or_default(), scripts: serialised.scripts.unwrap_or_default(), data: serialised.data.unwrap_or_default(), }) } } impl Default for PostMetadata { fn default() -> PostMetadata { PostMetadata { language: None, author: None, tags: vec![], styles: vec![], scripts: vec![], data: BTreeMap::new(), } } }