diff options
author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-06-13 06:40:48 +0200 |
---|---|---|
committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-06-13 06:40:48 +0200 |
commit | 07c2924e7abc9641df6f6bc6181c912b70904972 (patch) | |
tree | c006b1f66588c1b4e6f1a96c7d6be24c1a2d87a4 /src/new/mod.rs | |
parent | feat(cli): Switch to stderrlog, configured by cli options (diff) | |
download | lpm-07c2924e7abc9641df6f6bc6181c912b70904972.tar.gz lpm-07c2924e7abc9641df6f6bc6181c912b70904972.zip |
feat(new): Ensure that file names are ASCII only
This allows us to just query the last chapter instead of storing it.
Diffstat (limited to 'src/new/mod.rs')
-rw-r--r-- | src/new/mod.rs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/new/mod.rs b/src/new/mod.rs index a85187c..8d8193c 100644 --- a/src/new/mod.rs +++ b/src/new/mod.rs @@ -1,2 +1,37 @@ +use std::fmt::Display; + +use convert_case::{Case, Casing}; +use deunicode::deunicode; + pub mod chapter; pub mod section; + +#[derive(PartialEq, Eq, PartialOrd, Ord)] +pub struct MangledName(String); + +impl MangledName { + pub fn new(name: &str) -> Self { + let ascii_name = deunicode(&name); + Self(ascii_name.to_case(Case::Snake)) + } + + pub fn from_str_unsafe(name: &str) -> Self { + Self(name.to_owned()) + } + + pub fn check_mangled(name: &str) -> bool { + let mangled = Self::new(name); + let normal = Self::from_str_unsafe(name); + + mangled == normal + } + + pub fn as_str(&self) -> &str { + &self.0 + } +} +impl Display for MangledName { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&self.0) + } +} |