fix: Utilize StaticType for Dhall types

This commit is contained in:
Nathan McCarty 2023-03-19 15:53:12 -04:00
parent fc62d8c30c
commit 98b744faa7
Signed by: thatonelutenist
GPG Key ID: D70DA3DD4D1E9F96
4 changed files with 38 additions and 15 deletions

View File

@ -59,6 +59,7 @@ impl Site {
///
/// Will return an error if serialization fails, the user does not have write permissions for
/// the directory, or any other IO errors occur while writing out the config.
#[allow(clippy::too_many_lines)]
pub fn write(&mut self, site_dir: impl AsRef<Path>) -> Result<(), SiteError> {
let site_dir = site_dir.as_ref();
// Setup logging
@ -80,6 +81,7 @@ impl Site {
debug!(?self.config, "Writing out the config");
let config_path = site_dir.join("config.dhall");
let serialized_config = serde_dhall::serialize(&self.config)
.static_type_annotation()
.to_string()
.context(ConfigReifySnafu)?;
let mut config_file =
@ -196,6 +198,7 @@ impl Site {
let config_path = site_dir.join("config.dhall");
debug!(?config_path, "Reading main site configuration file");
let config: Config = serde_dhall::from_file(&config_path)
.static_type_annotation()
.parse()
.context(ConfigReadSnafu { path: &config_path })?;

View File

@ -1,9 +1,10 @@
//! Configuration for a site
use serde::{Deserialize, Serialize};
use serde_dhall::StaticType;
/// Description of the domain name and related settings for a site
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, StaticType)]
pub struct Domain {
/// The domain name itself
pub domain_name: String,
@ -23,7 +24,7 @@ impl Default for Domain {
/// Top level configuration for a site
///
/// Describes the file located at `site/config.dhall`
#[derive(Serialize, Deserialize, Debug, Default, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Debug, Default, PartialEq, Eq, StaticType)]
pub struct Config {
/// Doman name settings
pub domain: Domain,

View File

@ -9,6 +9,7 @@ use std::{
};
use serde::{Deserialize, Serialize};
use serde_dhall::StaticType;
use snafu::{ensure, ResultExt};
use tracing::{debug, info, info_span, warn};
@ -19,7 +20,7 @@ use super::{
};
/// Representation of the configuration for a page
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, StaticType)]
pub struct PageConfig {
/// Title of the page
title: String,
@ -95,6 +96,7 @@ impl Page {
let config_path = page_dir.join("page.dhall");
debug!(?config_path, ?self.config, "Writing out page config");
let serialized_config = serde_dhall::serialize(&self.config)
.static_type_annotation()
.to_string()
.context(ConfigReifySnafu)?;
let mut config_file =
@ -172,12 +174,12 @@ impl Page {
let config_path = dhall_canidates.remove(0);
debug!(?config_path, "Reading config");
let real_config_path = page_dir.join(config_path);
let config: PageConfig =
serde_dhall::from_file(&real_config_path)
.parse()
.context(ConfigReadSnafu {
path: &real_config_path,
})?;
let config: PageConfig = serde_dhall::from_file(&real_config_path)
.static_type_annotation()
.parse()
.context(ConfigReadSnafu {
path: &real_config_path,
})?;
// Find the djot file
let mut djot_canidates = file_names

View File

@ -2,6 +2,7 @@
use std::{
borrow::Cow,
collections::HashMap,
ffi::OsStr,
fs::{create_dir_all, read_dir, File},
io::Write,
@ -10,6 +11,7 @@ use std::{
use chrono::{Local, NaiveDate};
use serde::{Deserialize, Serialize};
use serde_dhall::{SimpleType, StaticType};
use snafu::{ensure, ResultExt};
use tracing::{debug, info, info_span, span, warn, Level};
@ -44,6 +46,20 @@ impl Default for PostConfig {
}
}
impl StaticType for PostConfig {
fn static_type() -> serde_dhall::SimpleType {
let mut type_map: HashMap<String, SimpleType> = HashMap::new();
type_map.insert("title".into(), SimpleType::Text);
// TODO: Find a way to represent a date as strongly typed value instead of stringly typed
type_map.insert("date".into(), SimpleType::Text);
type_map.insert(
"stylesheets".into(),
SimpleType::List(Box::new(SimpleType::Text)),
);
SimpleType::Record(type_map)
}
}
/// Representation of a post
#[derive(Debug, PartialEq, Eq)]
pub struct Post {
@ -100,6 +116,7 @@ impl Post {
let config_path = post_dir.join("post.dhall");
debug!(?config_path, ?self.config, "Writing out config");
let serialized_config = serde_dhall::serialize(&self.config)
.static_type_annotation()
.to_string()
.context(ConfigReifySnafu)?;
let mut config_file =
@ -177,12 +194,12 @@ impl Post {
let config_path = dhall_canidates.remove(0);
debug!(?config_path, "Reading config");
let real_config_path = post_dir.join(config_path);
let config: PostConfig =
serde_dhall::from_file(&real_config_path)
.parse()
.context(ConfigReadSnafu {
path: &real_config_path,
})?;
let config: PostConfig = serde_dhall::from_file(&real_config_path)
.static_type_annotation()
.parse()
.context(ConfigReadSnafu {
path: &real_config_path,
})?;
// Find the djot file
let mut djot_canidates = file_names