generated from Astrial.org/template
refactor(wip): overhaul inventory system
This commit is contained in:
parent
a310bfde34
commit
7241a1e8cd
3 changed files with 82 additions and 0 deletions
32
Cargo.lock
generated
32
Cargo.lock
generated
|
@ -200,6 +200,7 @@ dependencies = [
|
||||||
"indoc",
|
"indoc",
|
||||||
"regex",
|
"regex",
|
||||||
"serde",
|
"serde",
|
||||||
|
"serde_yaml",
|
||||||
"toml",
|
"toml",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -266,6 +267,12 @@ version = "1.70.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
|
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itoa"
|
||||||
|
version = "1.0.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "js-sys"
|
name = "js-sys"
|
||||||
version = "0.3.76"
|
version = "0.3.76"
|
||||||
|
@ -418,6 +425,12 @@ version = "0.8.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
|
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ryu"
|
||||||
|
version = "1.0.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.216"
|
version = "1.0.216"
|
||||||
|
@ -447,6 +460,19 @@ dependencies = [
|
||||||
"serde",
|
"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]]
|
[[package]]
|
||||||
name = "shlex"
|
name = "shlex"
|
||||||
version = "1.3.0"
|
version = "1.3.0"
|
||||||
|
@ -516,6 +542,12 @@ version = "1.0.14"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
|
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unsafe-libyaml"
|
||||||
|
version = "0.2.11"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "utf8parse"
|
name = "utf8parse"
|
||||||
version = "0.2.2"
|
version = "0.2.2"
|
||||||
|
|
|
@ -11,4 +11,5 @@ iana-time-zone = "0.1.61"
|
||||||
indoc = "2.0.5"
|
indoc = "2.0.5"
|
||||||
regex = "1.11.1"
|
regex = "1.11.1"
|
||||||
serde = { version = "1.0.216", features = ["derive"] }
|
serde = { version = "1.0.216", features = ["derive"] }
|
||||||
|
serde_yaml = { version = "0.9.34" }
|
||||||
toml = "0.8.19"
|
toml = "0.8.19"
|
||||||
|
|
49
src/mgmt.rs
49
src/mgmt.rs
|
@ -1,3 +1,4 @@
|
||||||
|
use chrono::TimeZone;
|
||||||
use diaryrs::macros::UnwrapOrFatalAble;
|
use diaryrs::macros::UnwrapOrFatalAble;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
|
@ -24,6 +25,31 @@ pub struct Inventory {
|
||||||
pub avg_word_count: f64,
|
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<Tz: TimeZone> {
|
||||||
|
pub path: path::PathBuf,
|
||||||
|
|
||||||
|
pub word_count: u64,
|
||||||
|
pub timestamp: chrono::DateTime<Tz>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Tz: TimeZone> Entry<Tz> {
|
||||||
|
//
|
||||||
|
pub fn read(path: path::PathBuf) -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
|
let contents = fs::read_to_string(&path)?;
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
path,
|
||||||
|
word_count: count_words(&contents),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Inventory {
|
impl Inventory {
|
||||||
/// Take inventory of the diary entries
|
/// Take inventory of the diary entries
|
||||||
///
|
///
|
||||||
|
@ -113,3 +139,26 @@ fn word_count_mds(paths: &Vec<path::PathBuf>) -> Result<u64, Box<dyn std::error:
|
||||||
|
|
||||||
Ok(word_count as u64)
|
Ok(word_count as u64)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn count_words(contents: &str) -> 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::<Vec<&str>>()
|
||||||
|
.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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue