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
use anyhow::{anyhow, Result};
use std::path::{Path, PathBuf};
use entity::Zine;
use error::ZineError;
use parking_lot::RwLock;
use walkdir::WalkDir;
pub mod build;
mod code_blocks;
mod data;
mod engine;
mod entity;
mod error;
mod feed;
pub mod helpers;
mod html;
mod i18n;
pub mod lint;
mod locales;
mod markdown;
pub mod new;
pub mod serve;
pub use self::engine::ZineEngine;
pub use self::entity::Entity;
pub static ZINE_FILE: &str = "zine.toml";
pub static ZINE_CONTENT_DIR: &str = "content";
pub static ZINE_INTRO_FILE: &str = "intro.md";
pub static ZINE_BANNER: &str = r"
███████╗██╗███╗ ██╗███████╗
╚══███╔╝██║████╗ ██║██╔════╝
███╔╝ ██║██╔██╗ ██║█████╗
███╔╝ ██║██║╚██╗██║██╔══╝
███████╗██║██║ ╚████║███████╗
╚══════╝╚═╝╚═╝ ╚═══╝╚══════╝
";
static MODE: RwLock<Mode> = parking_lot::const_rwlock(Mode::Unknown);
#[derive(Copy, Clone)]
pub enum Mode {
Build,
Serve,
Unknown,
}
pub fn current_mode() -> Mode {
*MODE.read()
}
pub fn set_current_mode(mode: Mode) {
*MODE.write() = mode;
}
fn parse_root_zine_file<P: AsRef<Path>>(path: P) -> Result<Option<Zine>> {
if WalkDir::new(&path).max_depth(1).into_iter().any(|entry| {
let entry = entry.as_ref().unwrap();
entry.file_name() == crate::ZINE_FILE
}) {
return Ok(Some(Zine::parse_from_toml(path)?));
}
Ok(None)
}
pub fn locate_root_zine_folder<P: AsRef<Path>>(path: P) -> Result<Option<(PathBuf, Zine)>> {
match parse_root_zine_file(&path) {
Ok(Some(zine)) => return Ok(Some((path.as_ref().to_path_buf(), zine))),
Err(err) => match err.downcast::<ZineError>() {
Ok(inner_err @ ZineError::InvalidRootTomlFile(_)) => return Err(anyhow!(inner_err)),
Ok(ZineError::NotRootTomlFile) => {}
_ => {}
},
_ => {}
}
match path.as_ref().parent() {
Some(parent_path) => locate_root_zine_folder(parent_path),
None => Ok(None),
}
}