diff --git a/Cargo.lock b/Cargo.lock index 6ee9e99..e29d3d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -200,6 +200,7 @@ dependencies = [ "indoc", "regex", "serde", + "serde_yaml", "toml", ] @@ -266,6 +267,12 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" +[[package]] +name = "itoa" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" + [[package]] name = "js-sys" version = "0.3.76" @@ -418,6 +425,12 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + [[package]] name = "serde" version = "1.0.216" @@ -447,6 +460,19 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_yaml" +version = "0.9.34+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" +dependencies = [ + "indexmap", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + [[package]] name = "shlex" version = "1.3.0" @@ -516,6 +542,12 @@ version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" +[[package]] +name = "unsafe-libyaml" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" + [[package]] name = "utf8parse" version = "0.2.2" diff --git a/Cargo.toml b/Cargo.toml index 0b0b667..e7eeb23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,5 @@ iana-time-zone = "0.1.61" indoc = "2.0.5" regex = "1.11.1" serde = { version = "1.0.216", features = ["derive"] } +serde_yaml = { version = "0.9.34" } toml = "0.8.19" diff --git a/src/mgmt.rs b/src/mgmt.rs index dfa4e70..cd2457f 100644 --- a/src/mgmt.rs +++ b/src/mgmt.rs @@ -1,3 +1,4 @@ +use chrono::TimeZone; use diaryrs::macros::UnwrapOrFatalAble; use regex::Regex; @@ -24,6 +25,31 @@ pub struct Inventory { pub avg_word_count: f64, } +/// Represents a single diary entry (markdown file) and its associated stats/metadata +/// +/// Does not include the actual contents of the entry, only stats +/// +/// The timezone of the entry is determined by the `Tz` type parameter +#[derive(Debug)] +pub struct Entry { + pub path: path::PathBuf, + + pub word_count: u64, + pub timestamp: chrono::DateTime, +} + +impl Entry { + // + pub fn read(path: path::PathBuf) -> Result> { + let contents = fs::read_to_string(&path)?; + + Ok(Self { + path, + word_count: count_words(&contents), + }) + } +} + impl Inventory { /// Take inventory of the diary entries /// @@ -113,3 +139,26 @@ fn word_count_mds(paths: &Vec) -> Result u64 { + let re_titles = Regex::new(r"(?m)^#{1,6} .+") + .unwrap_or_fatal("Failed to compile title regex. Something is very wrong!"); + let re_comments = Regex::new(r"(?s-m)") + .unwrap_or_fatal("Failed to compile comment regex. Something is very wrong!"); + let re_images = Regex::new(r"!\[.*\]\(.+\)") + .unwrap_or_fatal("Failed to compile image regex. Something is very wrong!"); + + // Cut YAML header, comments and images + let contents = &re_titles.replace_all( + contents + .split("---") + .collect::>() + .pop() + .ok_or("No content found")?, + "", + ); + let contents = &re_comments.replace_all(contents, ""); + let contents = &re_images.replace_all(contents, ""); + + contents.split_whitespace().count() as u64 +}