Skip to main content

abbaye/
config.rs

1use std::path::PathBuf;
2
3use figment::{
4    Figment,
5    providers::{Format, Toml},
6};
7use miette::{IntoDiagnostic, Result};
8use serde::{Deserialize, Serialize};
9
10use crate::{
11    builders::AnyBuilder, changelog::ChangelogConfig, version_extractors::AnyVersionExtractor,
12};
13
14/// General website metadata.
15#[derive(Debug, Clone, Deserialize, Serialize)]
16pub struct SiteConfig {
17    /// Display name of the project, used in page titles and headings.
18    pub name: String,
19    /// Path to the README file rendered on each version page.
20    /// Defaults to `README.md` in the current working directory.
21    pub readme: Option<PathBuf>,
22    /// Canonical base URL of the published site (e.g. `"https://example.com"`).
23    /// When set, the generated `releases.atom` feed will include absolute links
24    /// and proper entry IDs.  Trailing slashes are stripped automatically.
25    pub base_url: Option<String>,
26    /// URL of the project's repository (e.g. `"https://git.sr.ht/~ololduck/abbaye"`).
27    /// When set, we will show the repository link in the generated website.
28    pub repo_url: Option<String>,
29    /// An optional language code for the website (e.g. `"en"` or `"fr"`).
30    /// When set, the generated HTML will include a `lang` attribute on the `<html>` tag. Defaults to `"en"`.
31    pub lang: Option<String>,
32}
33
34/// A full configuration for the Abbaye site generator.
35///
36/// Here's a sample configuration that works well as a starting point for rust projects:
37///
38/// ```toml
39/// [site]
40/// name = "Abbaye"
41///
42/// [version_extractor]
43/// type = "cargo" # extract version from Cargo.toml
44///
45/// [changelog]
46/// # let's use the default changelog extractor
47///
48/// [[builders]]
49/// type = "cargo" # calls `cargo build --release` for each target
50/// targets = ["x86_64-unknown-linux-gnu", "x86_64-unknown-linux-musl"]
51/// [[builders]]
52/// type = "cargo_doc" # generates documentation using `cargo doc`
53/// no_deps = true
54///
55/// [[builders]]
56/// type = "archive" # creates a compressed tarball of the source code
57/// ```
58///
59/// You can learn more about each builder type in the [builders module documentation](crate::builders).
60///
61#[derive(Debug, Clone, Deserialize, Serialize)]
62pub struct AbbayeConfig {
63    pub site: SiteConfig,
64    pub version_extractor: AnyVersionExtractor,
65    pub changelog: ChangelogConfig,
66    pub builders: Vec<AnyBuilder>,
67    #[serde(default = "abbaye_output_dir")]
68    pub output_dir: Option<PathBuf>,
69}
70
71fn abbaye_output_dir() -> Option<PathBuf> {
72    Some(PathBuf::from("public"))
73}
74
75/// Load the Abbaye2 configuration from the current working directory.
76///
77/// Looks for `.abbaye.toml` first, then `abbaye.toml`; when both are present
78/// `abbaye.toml` takes precedence (last merge wins).
79pub fn load_config() -> Result<AbbayeConfig> {
80    let cwd = std::env::current_dir().into_diagnostic()?;
81    Figment::new()
82        .merge(Toml::file(cwd.join(".abbaye.toml")))
83        .merge(Toml::file(cwd.join("abbaye.toml")))
84        .merge(Toml::file(cwd.join(".abbaye").join("abbaye.toml")))
85        .extract()
86        .into_diagnostic()
87}