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 schemars::JsonSchema;
11
12use crate::{
13    builders::BuilderEntry, changelog::ChangelogConfig, version_extractors::AnyVersionExtractor,
14};
15
16/// The output format for generated pages.
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, JsonSchema)]
18#[serde(rename_all = "lowercase")]
19pub enum OutputFormat {
20    Html,
21    Gemtext,
22}
23
24impl OutputFormat {
25    /// File extension for this format (e.g. `"html"` or `"gmi"`).
26    pub fn extension(&self) -> &'static str {
27        match self {
28            Self::Html => "html",
29            Self::Gemtext => "gmi",
30        }
31    }
32
33    /// Whether this format supports inline formatting (HTML does, gemtext doesn't).
34    pub fn supports_inline_formatting(&self) -> bool {
35        match self {
36            Self::Html => true,
37            Self::Gemtext => false,
38        }
39    }
40}
41
42fn default_formats() -> Vec<OutputFormat> {
43    vec![OutputFormat::Html]
44}
45
46/// General website metadata.
47#[derive(Debug, Default, Clone, Deserialize, Serialize, JsonSchema)]
48pub struct SiteConfig {
49    /// Display name of the project, used in page titles and headings.
50    pub name: String,
51    /// Where to output generated files. Defaults to `public`.
52    #[serde(default = "abbaye_output_dir")]
53    pub output_dir: PathBuf,
54    /// Path to the README file rendered on each version page.
55    /// Defaults to `README.md` in the current working directory.
56    pub readme: Option<PathBuf>,
57    /// Canonical base URL of the published site (e.g. `"https://example.com"`).
58    /// When set, the generated `releases.atom` feed will include absolute links
59    /// and proper entry IDs.  Trailing slashes are stripped automatically.
60    pub base_url: Option<String>,
61    /// URL of the project's repository (e.g. `"https://git.sr.ht/~ololduck/abbaye"`).
62    /// When set, we will show the repository link in the generated website.
63    pub repo_url: Option<String>,
64    /// An optional language code for the website (e.g. `"en"` or `"fr"`).
65    /// When set, the generated HTML will include a `lang` attribute on the `<html>` tag. Defaults to `"en"`.
66    pub lang: Option<String>,
67    /// If you have a Fediverse account, you can set this to your username to enable Fediverse integration. (don't forget the starting '@' !)
68    pub fediverse_creator: Option<String>,
69    /// OpenGraph configuration for the website.
70    pub opengraph: Option<OpenGraphConfig>,
71    /// Output formats to generate. Defaults to `["html"]`.
72    /// Set to `["html", "gemtext"]` to also generate Gemini text pages.
73    #[serde(default = "default_formats")]
74    pub formats: Vec<OutputFormat>,
75}
76
77/// OpenGraph configuration for the website.
78#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
79pub struct OpenGraphConfig {
80    /// OpenGraph type of the website. It will be used as the `og:type` meta tag.
81    /// Note: by default, the value "website" will be enforced for the version listing page and "article" for the release notes page.
82    pub r#type: Option<String>,
83    /// URL of the website's image.
84    pub image: String,
85    /// Alt text for the website's image.
86    pub image_alt_text: Option<String>,
87    /// URL of the website. If not set, the `base_url` will be used instead. If `base_url` is not set either, the world explodes. Think of the kittens.
88    pub url: Option<String>,
89    /// Author of the website. Used on release notes pages to indicate the author.
90    pub author: Option<String>,
91}
92
93/// Configuration for the git repository web UI.
94#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
95pub struct GitUiConfig {
96    #[serde(default = "default_branch")]
97    pub default_branch: String,
98    /// Maximum number of commits to show in the log page. Defaults to 200.
99    #[serde(default = "default_max_commits")]
100    pub max_commits: usize,
101    /// Path to the git repository to read. Defaults to `.` (the current directory).
102    pub repo_path: Option<PathBuf>,
103    /// Explicit clone URL displayed in the UI (e.g. `"https://example.com/repository.git"`).
104    /// When absent, derived from `site.base_url` by appending `/repository.git`.
105    pub clone_url: Option<String>,
106    /// Glob patterns matched against a ref's short name (e.g. `"main"`,
107    /// `"feature/*"`, `"v1.0.0"`), using [`globset`](https://docs.rs/globset)
108    /// syntax. Any branch or tag matching one of these patterns is left out
109    /// of the generated UI. Defaults to empty (nothing excluded).
110    #[serde(default)]
111    pub exclude: Vec<String>,
112    /// Glob patterns (same syntax as `exclude`). When non-empty, acts as an
113    /// allowlist: only refs matching at least one `include` pattern are
114    /// considered at all (and are then still subject to `exclude`). When
115    /// empty (the default), every ref is a candidate.
116    #[serde(default)]
117    pub include: Vec<String>,
118}
119
120fn default_branch() -> String {
121    "main".to_string()
122}
123
124fn default_max_commits() -> usize {
125    200
126}
127
128/// A full configuration for the Abbaye site generator.
129///
130/// Here's a sample configuration that works well as a starting point for rust projects:
131///
132/// ```toml
133/// [site]
134/// name = "Abbaye"
135///
136/// [version_extractor]
137/// type = "cargo" # extract version from Cargo.toml
138///
139/// [changelog]
140/// # let's use the default changelog extractor
141///
142/// [[builders]]
143/// type = "cargo" # calls `cargo build --release` for each target
144/// targets = ["x86_64-unknown-linux-gnu", "x86_64-unknown-linux-musl"]
145/// [[builders]]
146/// type = "cargo_doc" # generates documentation using `cargo doc`
147/// no_deps = true
148///
149/// [[builders]]
150/// type = "archive" # creates a compressed tarball of the source code
151/// ```
152///
153/// You can learn more about each builder type in the [builders module documentation](crate::builders).
154///
155#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
156pub struct AbbayeConfig {
157    /// Metadata about the site.
158    pub site: SiteConfig,
159    /// which version extractor to use to extract the version(s) of the project
160    pub version_extractor: AnyVersionExtractor,
161    /// Configuration for the changelog extractor.
162    pub changelog: ChangelogConfig,
163    /// Builders to run during the build process.
164    pub builders: Vec<BuilderEntry>,
165    /// Optional git repository web UI. When present, abbaye generates browsable HTML
166    /// pages at `<output>/repository/` and a clonable bare repository at
167    /// `<output>/repository.git/`.
168    #[serde(default)]
169    pub git_ui: Option<GitUiConfig>,
170}
171
172fn abbaye_output_dir() -> PathBuf {
173    PathBuf::from("public")
174}
175
176/// Load the Abbaye2 configuration from the current working directory.
177///
178/// Looks for `.abbaye.toml` first, then `abbaye.toml`; when both are present
179/// `abbaye.toml` takes precedence (last merge wins).
180pub fn load_config() -> Result<AbbayeConfig> {
181    let cwd = std::env::current_dir().into_diagnostic()?;
182    Figment::new()
183        .merge(Toml::file(cwd.join(".abbaye.toml")))
184        .merge(Toml::file(cwd.join("abbaye.toml")))
185        .merge(Toml::file(cwd.join(".abbaye").join("abbaye.toml")))
186        .extract()
187        .into_diagnostic()
188}