lib/
defaults.rs

1//! Defines the defaults for this crate.
2
3use std::path::PathBuf;
4
5use once_cell::sync::Lazy;
6
7/// The crates's root directory.
8pub static CRATE_ROOT: Lazy<PathBuf> = Lazy::new(|| env!("CARGO_MANIFEST_DIR").into());
9
10/// The user's home directory.
11//
12// Unwrap should be safe here. It would only fail if the user is deleted after the process has
13// started. Which is highly unlikely, and would be okay to panic if that was the case.
14pub static HOME: Lazy<PathBuf> = Lazy::new(|| {
15    let path = std::env::var_os("HOME").expect("could not determine home directory");
16    PathBuf::from(path)
17});
18
19/// Date format string for slugs. Translates to: `YYYY-MM-DD-HHMMSS` i.e. `1970-01-01-120000`.
20pub const DATE_FORMAT_SLUG: &str = "%Y-%m-%d-%H%M%S";
21
22/// Date format string for template `date` filter. Translates to: `YYYY-MM-DD` i.e. `1970-01-01`.
23pub const DATE_FORMAT_TEMPLATE: &str = "%Y-%m-%d";
24
25/// A list of "smart" Unicode symbols and their ASCII eqivalents.
26///
27/// Based on the following:
28///
29/// * [Daring Fireball - SmartyPants](https://daringfireball.net/projects/smartypants/)
30/// * [Python-Markdown - SmartyPants](https://python-markdown.github.io/extensions/smarty/)
31#[allow(clippy::doc_markdown)]
32pub static UNICODE_TO_ASCII_SYMBOLS: Lazy<Vec<(char, &str)>> = Lazy::new(|| {
33    [
34        ('‘', "'"),
35        ('’', "'"),
36        ('“', "\""),
37        ('”', "\""),
38        ('»', "<<"),
39        ('«', ">>"),
40        ('…', "..."),
41        ('–', "--"),
42        ('—', "---"),
43    ]
44    .into_iter()
45    .collect()
46});
47
48#[cfg(test)]
49pub(crate) mod test {
50
51    use super::*;
52
53    /// Defines the root path to the example templates.
54    pub static EXAMPLE_TEMPLATES: Lazy<PathBuf> = Lazy::new(|| {
55        let mut path = CRATE_ROOT.to_owned();
56        path.push("templates");
57        path
58    });
59
60    /// Defines the root path to the testing templates.
61    ///
62    /// The test templates are located at: [crate-root]/data/templates/[directory]/[filename]
63    pub static TEST_TEMPLATES: Lazy<PathBuf> = Lazy::new(|| {
64        let mut path = CRATE_ROOT.to_owned();
65        path.extend(["data", "templates"].iter());
66        path
67    });
68
69    /// Defines the root path to the testing templates.
70    #[derive(Debug, Copy, Clone)]
71    #[allow(missing_docs)]
72    pub enum TemplatesDirectory {
73        ValidConfig,
74        ValidContext,
75        ValidFilter,
76        ValidSyntax,
77        InvalidConfig,
78        InvalidContext,
79        InvalidFilter,
80        InvalidSyntax,
81    }
82
83    impl std::fmt::Display for TemplatesDirectory {
84        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85            match self {
86                Self::ValidConfig => write!(f, "valid-config"),
87                Self::ValidContext => write!(f, "valid-context"),
88                Self::ValidFilter => write!(f, "valid-filter"),
89                Self::ValidSyntax => write!(f, "valid-syntax"),
90                Self::InvalidConfig => write!(f, "invalid-config"),
91                Self::InvalidContext => write!(f, "invalid-context"),
92                Self::InvalidFilter => write!(f, "invalid-filter"),
93                Self::InvalidSyntax => write!(f, "invalid-syntax"),
94            }
95        }
96    }
97
98    impl From<TemplatesDirectory> for PathBuf {
99        fn from(directory: TemplatesDirectory) -> Self {
100            TEST_TEMPLATES.join(directory.to_string())
101        }
102    }
103}