use std::path::Path;
use anyhow::{Context, Result};
use serde::Deserialize;
use typst::ecow::{EcoString, EcoVec, eco_vec};
#[derive(Clone, Deserialize)]
#[serde(default)]
pub struct HighlightConfig {
pub enabled: bool,
pub themes: Themes,
}
impl Default for HighlightConfig {
fn default() -> Self {
Self {
enabled: true,
themes: Themes::default(),
}
}
}
#[derive(Clone, Deserialize)]
#[serde(default)]
pub struct Themes {
pub light: EcoString,
pub dark: EcoString,
}
impl Default for Themes {
fn default() -> Self {
Self {
light: "github_light".into(),
dark: "github_dark".into(),
}
}
}
#[derive(Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct PathsConfig {
pub pages: EcoString,
pub generate: EcoString,
pub content: EcoString,
pub public: EcoString,
pub output: EcoString,
}
impl Default for PathsConfig {
fn default() -> Self {
Self {
pages: "pages".into(),
generate: "generate".into(),
content: "content".into(),
public: "public".into(),
output: "dist".into(),
}
}
}
#[derive(Clone, Deserialize)]
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
pub struct OutputConfig {
pub assets: EcoString,
pub pretty: bool,
}
impl Default for OutputConfig {
fn default() -> Self {
Self {
assets: "_assets".into(),
pretty: false,
}
}
}
#[derive(Clone, Deserialize)]
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
pub struct AssetsConfig {
pub image_inline_threshold: usize,
pub images: ImageConfig,
}
impl Default for AssetsConfig {
fn default() -> Self {
Self {
image_inline_threshold: 1024,
images: ImageConfig::default(),
}
}
}
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq)]
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
pub struct ImageConfig {
pub enabled: bool,
pub jpeg_quality: u8,
pub frame_density: Option<u8>,
}
impl Default for ImageConfig {
fn default() -> Self {
Self {
enabled: true,
jpeg_quality: 85,
frame_density: None,
}
}
}
#[derive(Clone, Deserialize, Eq, Hash, PartialEq)]
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
pub struct CssConfig {
pub minify: bool,
pub targets: EcoVec<EcoString>,
pub custom_media: bool,
}
impl Default for CssConfig {
fn default() -> Self {
Self {
minify: true,
targets: eco_vec!["defaults".into()],
custom_media: false,
}
}
}
#[derive(Clone, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct WatchConfig {
pub paths: EcoVec<EcoString>,
}
#[derive(Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PostprocessConfig {
pub name: EcoString,
pub command: EcoVec<EcoString>,
#[serde(default)]
pub mount: Option<EcoString>,
#[serde(default)]
pub watch: EcoVec<EcoString>,
}
#[derive(Clone, Default, Deserialize)]
#[serde(default)]
pub struct TypstConfig {
pub fonts: FontConfig,
}
#[derive(Clone, Deserialize, Eq, PartialEq)]
#[serde(default)]
pub struct FontConfig {
pub paths: EcoVec<EcoString>,
pub system: bool,
}
impl Default for FontConfig {
fn default() -> Self {
Self {
paths: EcoVec::new(),
system: true,
}
}
}
#[derive(Default, Deserialize)]
#[serde(default)]
pub struct AsterConfig {
pub paths: PathsConfig,
pub output: OutputConfig,
pub assets: AssetsConfig,
pub css: CssConfig,
pub watch: WatchConfig,
pub postprocess: EcoVec<PostprocessConfig>,
pub typst: TypstConfig,
pub highlight: HighlightConfig,
}
impl AsterConfig {
pub fn parse(content: &[u8], path: &Path) -> Result<Self> {
let content = std::str::from_utf8(content)
.with_context(|| format!("{} is not valid UTF-8", path.display()))?;
let table: toml::Table = content
.parse()
.with_context(|| format!("failed to parse {}", path.display()))?;
Self::deserialize(toml::Value::Table(table))
.with_context(|| format!("failed to extract Aster config from {}", path.display()))
}
}
#[cfg(test)]
mod tests {
use super::*;
fn load(path: &Path) -> Result<AsterConfig> {
let content = std::fs::read(path)?;
AsterConfig::parse(&content, path)
}
#[test]
fn loads_aster_config_and_ignores_project_fields() {
let temp = tempfile::tempdir().unwrap();
let config_file = temp.path().join("aster.toml");
std::fs::write(
&config_file,
concat!(
"title = \"Aster\"\n",
"published = 1979-05-27T07:32:00Z\n",
"[site]\n",
"enabled = true\n",
"[highlight.themes]\n",
"light = \"catppuccin_latte\"\n",
"dark = \"catppuccin_mocha\"\n",
),
)
.unwrap();
let config = load(&config_file).unwrap();
assert_eq!(config.highlight.themes.light, "catppuccin_latte");
assert_eq!(config.highlight.themes.dark, "catppuccin_mocha");
assert_eq!(
config
.css
.targets
.iter()
.map(EcoString::as_str)
.collect::<Vec<_>>(),
["defaults"]
);
assert!(config.watch.paths.is_empty());
}
#[test]
fn fills_missing_highlight_settings_with_defaults() {
let temp = tempfile::tempdir().unwrap();
let config_file = temp.path().join("aster.toml");
std::fs::write(
&config_file,
"[highlight.themes]\nlight = \"catppuccin_latte\"\n",
)
.unwrap();
let config = load(&config_file).unwrap();
assert_eq!(config.highlight.themes.light, "catppuccin_latte");
assert_eq!(config.highlight.themes.dark, "github_dark");
}
#[test]
fn loads_build_options_and_fills_their_defaults() {
let temp = tempfile::tempdir().unwrap();
let config_file = temp.path().join("aster.toml");
std::fs::write(
&config_file,
concat!(
"[paths]\n",
"pages = \"routes\"\n",
"public = \"files\"\n",
"output = \"public\"\n",
"[output]\n",
"assets = \"static/generated\"\n",
"pretty = true\n",
"[assets]\n",
"image-inline-threshold = 2048\n",
"[assets.images]\n",
"jpeg-quality = 90\n",
"frame-density = 2\n",
"[css]\n",
"minify = false\n",
"targets = [\"last 2 Chrome versions\", \"Firefox ESR\"]\n",
"custom-media = true\n",
"[watch]\n",
"paths = [\"components\", \"plugins/theme.ts\"]\n",
"[typst.fonts]\n",
"paths = [\"fonts\"]\n",
"system = false\n",
"[highlight]\n",
"enabled = false\n",
),
)
.unwrap();
let config = load(&config_file).unwrap();
assert_eq!(config.paths.pages, "routes");
assert_eq!(config.paths.generate, "generate");
assert_eq!(config.paths.content, "content");
assert_eq!(config.paths.public, "files");
assert_eq!(config.paths.output, "public");
assert_eq!(config.output.assets, "static/generated");
assert!(config.output.pretty);
assert_eq!(config.assets.image_inline_threshold, 2048);
assert!(config.assets.images.enabled);
assert_eq!(config.assets.images.jpeg_quality, 90);
assert_eq!(config.assets.images.frame_density, Some(2));
assert!(!config.css.minify);
assert_eq!(
config
.css
.targets
.iter()
.map(EcoString::as_str)
.collect::<Vec<_>>(),
["last 2 Chrome versions", "Firefox ESR"]
);
assert!(config.css.custom_media);
assert_eq!(
config
.watch
.paths
.iter()
.map(EcoString::as_str)
.collect::<Vec<_>>(),
["components", "plugins/theme.ts"]
);
assert_eq!(config.typst.fonts.paths, eco_vec!["fonts".into()]);
assert!(!config.typst.fonts.system);
assert!(!config.highlight.enabled);
assert_eq!(config.highlight.themes.light, "github_light");
}
#[test]
fn rejects_invalid_highlight_settings() {
let temp = tempfile::tempdir().unwrap();
let config_file = temp.path().join("aster.toml");
std::fs::write(&config_file, "[highlight]\nthemes = \"github_light\"\n").unwrap();
let error = match load(&config_file) {
Ok(_) => panic!("invalid highlight settings must be rejected"),
Err(error) => error,
};
assert!(
format!("{error:#}").contains("invalid type"),
"unexpected error: {error:#}"
);
}
}