switch from json to yaml

yaml is just easier to write by hand, and since we're using it for
files which are all meant to be written/edited by hand, it seems like
a no-brainer.
This commit is contained in:
Gered 2023-06-28 17:38:41 -04:00
parent 1fbdab9c58
commit 6e3eae7abd
10 changed files with 130 additions and 93 deletions

53
Cargo.lock generated
View file

@ -630,6 +630,12 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "equivalent"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88bffebc5d80432c9b140ee17875ff173a8ab62faad5b257da912bd2f6c1c0a1"
[[package]] [[package]]
name = "flate2" name = "flate2"
version = "1.0.26" version = "1.0.26"
@ -751,7 +757,7 @@ dependencies = [
"futures-sink", "futures-sink",
"futures-util", "futures-util",
"http", "http",
"indexmap", "indexmap 1.9.3",
"slab", "slab",
"tokio", "tokio",
"tokio-util", "tokio-util",
@ -764,6 +770,12 @@ version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "hashbrown"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a"
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.2.6" version = "0.2.6"
@ -883,7 +895,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99"
dependencies = [ dependencies = [
"autocfg", "autocfg",
"hashbrown", "hashbrown 0.12.3",
]
[[package]]
name = "indexmap"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5477fe2230a79769d8dc68e0eabf5437907c0457a5614a9e8dddb67f65eb65d"
dependencies = [
"equivalent",
"hashbrown 0.14.0",
] ]
[[package]] [[package]]
@ -1035,7 +1057,7 @@ dependencies = [
"serde-value", "serde-value",
"serde_derive", "serde_derive",
"serde_json", "serde_json",
"serde_yaml", "serde_yaml 0.8.26",
"thread-id", "thread-id",
"winapi", "winapi",
] ]
@ -1221,7 +1243,7 @@ dependencies = [
"pulldown-cmark", "pulldown-cmark",
"rss", "rss",
"serde", "serde",
"serde_json", "serde_yaml 0.9.22",
"simple-log", "simple-log",
"syntect", "syntect",
"tera", "tera",
@ -1343,7 +1365,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9bd9647b268a3d3e14ff09c23201133a62589c658db02bb7388c7246aafe0590" checksum = "9bd9647b268a3d3e14ff09c23201133a62589c658db02bb7388c7246aafe0590"
dependencies = [ dependencies = [
"base64", "base64",
"indexmap", "indexmap 1.9.3",
"line-wrap", "line-wrap",
"quick-xml", "quick-xml",
"serde", "serde",
@ -1592,12 +1614,25 @@ version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578a7433b776b56a35785ed5ce9a7e777ac0598aac5a6dd1b4b18a307c7fc71b" checksum = "578a7433b776b56a35785ed5ce9a7e777ac0598aac5a6dd1b4b18a307c7fc71b"
dependencies = [ dependencies = [
"indexmap", "indexmap 1.9.3",
"ryu", "ryu",
"serde", "serde",
"yaml-rust", "yaml-rust",
] ]
[[package]]
name = "serde_yaml"
version = "0.9.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "452e67b9c20c37fa79df53201dc03839651086ed9bbe92b3ca585ca9fdaa7d85"
dependencies = [
"indexmap 2.0.0",
"itoa",
"ryu",
"serde",
"unsafe-libyaml",
]
[[package]] [[package]]
name = "sha1" name = "sha1"
version = "0.10.5" version = "0.10.5"
@ -2009,6 +2044,12 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
[[package]]
name = "unsafe-libyaml"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1865806a559042e51ab5414598446a5871b561d21b6764f2eabb0dd481d880a6"
[[package]] [[package]]
name = "url" name = "url"
version = "2.4.0" version = "2.4.0"

View file

@ -14,7 +14,7 @@ log = "0.4.19"
pulldown-cmark = "0.9.3" pulldown-cmark = "0.9.3"
rss = "2.0.4" rss = "2.0.4"
serde = { version = "1.0.164", features = ["derive"]} serde = { version = "1.0.164", features = ["derive"]}
serde_json = "1.0.97" serde_yaml = "0.9.22"
simple-log = "1.6.0" simple-log = "1.6.0"
syntect = "5.0.0" syntect = "5.0.0"
tera = "1.19.0" tera = "1.19.0"

View file

@ -1,15 +0,0 @@
{
"pages": [
{
"file_path": "about.md",
"title": "About This Site",
"url": "/about/"
},
{
"file_path": "joke.md",
"title": "Joke",
"url": "/joke/",
"alternate_urls": ["/trying-to-be-funny/"]
}
]
}

11
example-site/pages.yml Normal file
View file

@ -0,0 +1,11 @@
pages:
- file_path: about.md
title: About This Site
url: /about/
- file_path: joke.md
title: Joke
url: /joke/
alternate_urls:
- /trying-to-be-funny/

View file

@ -1,45 +0,0 @@
{
"posts": [
{
"file_path": "2023-01-01-hello-world.md",
"title": "Hello, world!",
"date": "2023-01-01 12:30:42",
"slug": "hello-world",
"tags": ["aaa", "hello", "testing"]
},
{
"file_path": "2023-02-01-commonmark-testing.md",
"title": "CommonMark Testing",
"date": "2023-02-01",
"slug": "commonmark-testing",
"tags": ["testing"],
"alternate_urls": ["/testing/commonmark/"]
},
{
"file_path": "2023-03-20-lorem-ipsum.md",
"title": "Lorem Ipsum",
"date": "2023-03-20 18:01",
"slug": "lorem-ipsum"
},
{
"file_path": "2023-04-15-static-site-generators-are-boring.md",
"title": "Static Site Generators Are Boring",
"date": "2023-04-15",
"slug": "static-site-generators-are-boring",
"tags": ["aaa", "rant"]
},
{
"file_path": "2023-06-27-donuts-are-pretty-great.md",
"title": "Donuts Are Pretty Great",
"date": "2023-06-27",
"slug": "donuts-are-pretty-great",
"tags": ["aaa", "donuts", "coffee"]
}
],
"rss": {
"title": "My Site",
"description": "This is my site. There are others like it, but this one is mine.",
"url": "http://localhost:8080/",
"count": 10
}
}

47
example-site/posts.yml Normal file
View file

@ -0,0 +1,47 @@
posts:
- file_path: 2023-01-01-hello-world.md
title: Hello, world!
date: 2023-01-01 12:30:42
slug: hello-world
tags:
- aaa
- hello
- testing
- file_path: 2023-02-01-commonmark-testing.md
title: CommonMark Testing
date: 2023-02-01
slug: commonmark-testing
tags:
- testing
alternate_urls:
- /testing/commonmark/
- file_path: 2023-03-20-lorem-ipsum.md
title: Lorem Ipsum
date: 2023-03-20 18:01
slug: lorem-ipsum
- file_path: 2023-04-15-static-site-generators-are-boring.md
title: Static Site Generators Are Boring
date: 2023-04-15
slug: static-site-generators-are-boring
tags:
- aaa
- rant
- file_path: 2023-06-27-donuts-are-pretty-great.md
title: Donuts Are Pretty Great
date: 2023-06-27
slug: donuts-are-pretty-great
tags:
- aaa
- donuts
- coffee
rss:
title: My Site
description: This is my site. There are others like it, but this one is mine.
url: http://localhost:8080/
count: 10

View file

@ -1,8 +0,0 @@
{
"bind_addr": "127.0.0.1",
"bind_port": 8080,
"static_files_path": "static",
"templates_path": "templates",
"pages_path": "pages",
"posts_path": "posts"
}

6
example-site/server.yml Normal file
View file

@ -0,0 +1,6 @@
bind_addr: 127.0.0.1
bind_port: 8080
static_files_path: static
templates_path: templates
pages_path: pages
posts_path: posts

View file

@ -52,28 +52,28 @@ pub struct Posts {
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum ConfigError { pub enum ConfigError {
#[error("Json config I/O error with path {0}")] #[error("Config I/O error with path {0}")]
IOError(PathBuf, #[source] std::io::Error), IOError(PathBuf, #[source] std::io::Error),
#[error("Json deserialization error: {0}")] #[error("Deserialization error: {0}")]
SerdeJsonError(String), SerdeError(String),
} }
fn load_json_config<T>(path: &PathBuf) -> Result<T, ConfigError> fn load_config<T>(path: &PathBuf) -> Result<T, ConfigError>
where where
T: serde::de::DeserializeOwned, T: serde::de::DeserializeOwned,
{ {
let file = File::open(path).map_err(|e| ConfigError::IOError(path.clone(), e))?; let file = File::open(path).map_err(|e| ConfigError::IOError(path.clone(), e))?;
let mut reader = BufReader::new(file); let mut reader = BufReader::new(file);
match serde_json::from_reader(&mut reader) { match serde_yaml::from_reader(&mut reader) {
Ok(deserialized) => Ok(deserialized), Ok(deserialized) => Ok(deserialized),
Err(err) => Err(ConfigError::SerdeJsonError(err.to_string())), Err(err) => Err(ConfigError::SerdeError(err.to_string())),
} }
} }
pub fn load_server(path: &PathBuf, site_root: &PathBuf) -> Result<Server, ConfigError> { pub fn load_server(path: &PathBuf, site_root: &PathBuf) -> Result<Server, ConfigError> {
log::info!("Loading server json config from {:?}", path); log::info!("Loading server config from {:?}", path);
let mut server_config: Server = load_json_config(path)?; let mut server_config: Server = load_config(path)?;
server_config.static_files_path = [site_root, &server_config.static_files_path].iter().collect(); server_config.static_files_path = [site_root, &server_config.static_files_path].iter().collect();
server_config.templates_path = [site_root, &server_config.templates_path].iter().collect(); server_config.templates_path = [site_root, &server_config.templates_path].iter().collect();
server_config.pages_path = [site_root, &server_config.pages_path].iter().collect(); server_config.pages_path = [site_root, &server_config.pages_path].iter().collect();
@ -86,13 +86,13 @@ pub fn load_content(
posts_path: &PathBuf, posts_path: &PathBuf,
server_config: &Server, server_config: &Server,
) -> Result<(Pages, Posts), ConfigError> { ) -> Result<(Pages, Posts), ConfigError> {
log::info!("Loading pages json config from {:?}", pages_path); log::info!("Loading pages config from {:?}", pages_path);
let mut pages: Pages = load_json_config(pages_path)?; let mut pages: Pages = load_config(pages_path)?;
for page in pages.pages.iter_mut() { for page in pages.pages.iter_mut() {
page.file_path = [&server_config.pages_path, &page.file_path].iter().collect(); page.file_path = [&server_config.pages_path, &page.file_path].iter().collect();
} }
log::info!("Loading posts json config from {:?}", posts_path); log::info!("Loading posts config from {:?}", posts_path);
let mut posts: Posts = load_json_config(posts_path)?; let mut posts: Posts = load_config(posts_path)?;
for post in posts.posts.iter_mut() { for post in posts.posts.iter_mut() {
post.file_path = [&server_config.posts_path, &post.file_path].iter().collect(); post.file_path = [&server_config.posts_path, &post.file_path].iter().collect();
} }

View file

@ -71,7 +71,7 @@ async fn main() -> anyhow::Result<()> {
let first_arg = args.first().unwrap_or(&String::new()).to_lowercase(); let first_arg = args.first().unwrap_or(&String::new()).to_lowercase();
if first_arg == "-h" || first_arg == "--help" { if first_arg == "-h" || first_arg == "--help" {
println!("Usage: pbe <SITE_ROOT>"); println!("Usage: pbe <SITE_ROOT>");
println!("Where SITE_ROOT is a path that contains the json config files and all content and web resources."); println!("Where SITE_ROOT is a path that contains the config files and all content and web resources.");
Ok(()) Ok(())
} else { } else {
let site_root = if first_arg.is_empty() { let site_root = if first_arg.is_empty() {
@ -81,15 +81,15 @@ async fn main() -> anyhow::Result<()> {
}; };
log::info!("Using site root {:?}", site_root); log::info!("Using site root {:?}", site_root);
let server_config_path: PathBuf = [&site_root, &"server.json".into()].iter().collect(); let server_config_path: PathBuf = [&site_root, &"server.yml".into()].iter().collect();
let pages_config_path: PathBuf = [&site_root, &"pages.json".into()].iter().collect(); let pages_config_path: PathBuf = [&site_root, &"pages.yml".into()].iter().collect();
let posts_config_path: PathBuf = [&site_root, &"posts.json".into()].iter().collect(); let posts_config_path: PathBuf = [&site_root, &"posts.yml".into()].iter().collect();
log::info!("Loading config ..."); log::info!("Loading config ...");
let server_config = config::load_server(&server_config_path, &site_root) // let server_config = config::load_server(&server_config_path, &site_root) //
.context("Loading server json config")?; .context("Loading server config")?;
let (pages_config, posts_config) = config::load_content(&pages_config_path, &posts_config_path, &server_config) // let (pages_config, posts_config) = config::load_content(&pages_config_path, &posts_config_path, &server_config) //
.context("Loading content json configs")?; .context("Loading content configs")?;
log::info!("Initializing site data and content ..."); log::info!("Initializing site data and content ...");
let site_service = site::SiteService::new(server_config.clone(), pages_config, posts_config) let site_service = site::SiteService::new(server_config.clone(), pages_config, posts_config)