Skip to main content

anodizer_core/config/
archives.rs

1use std::collections::HashMap;
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Deserializer, Serialize};
5
6use super::{
7    ArchiveHooksConfig, SignConfig, StringOrBool, StringOrU32, deserialize_string_or_bool_opt,
8};
9
10// ---------------------------------------------------------------------------
11// ArchivesConfig — untagged enum: false => Disabled, array => Configs
12// ---------------------------------------------------------------------------
13
14#[derive(Debug, Clone, JsonSchema)]
15pub enum ArchivesConfig {
16    Disabled,
17    Configs(Vec<ArchiveConfig>),
18}
19
20impl Serialize for ArchivesConfig {
21    fn serialize<S: serde::Serializer>(
22        &self,
23        serializer: S,
24    ) -> std::result::Result<S::Ok, S::Error> {
25        match self {
26            ArchivesConfig::Disabled => serializer.serialize_bool(false),
27            ArchivesConfig::Configs(configs) => configs.serialize(serializer),
28        }
29    }
30}
31
32impl Default for ArchivesConfig {
33    fn default() -> Self {
34        ArchivesConfig::Configs(vec![])
35    }
36}
37
38/// Custom deserializer for ArchivesConfig.
39/// Accepts:
40///   - boolean `false`  → Disabled
41///   - array            → Configs(...)
42///   - missing/null     → Configs([])  (via serde default)
43pub(super) fn deserialize_archives_config<'de, D>(
44    deserializer: D,
45) -> Result<ArchivesConfig, D::Error>
46where
47    D: Deserializer<'de>,
48{
49    use serde::de::{self, Visitor};
50
51    struct ArchivesVisitor;
52
53    impl<'de> Visitor<'de> for ArchivesVisitor {
54        type Value = ArchivesConfig;
55
56        fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57            f.write_str("false or a list of archive configs")
58        }
59
60        fn visit_bool<E: de::Error>(self, v: bool) -> Result<Self::Value, E> {
61            if !v {
62                Ok(ArchivesConfig::Disabled)
63            } else {
64                Err(E::custom(
65                    "archives: true is not valid; use false or a list",
66                ))
67            }
68        }
69
70        fn visit_seq<A: de::SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
71            let mut configs = Vec::new();
72            while let Some(item) = seq.next_element::<ArchiveConfig>()? {
73                configs.push(item);
74            }
75            Ok(ArchivesConfig::Configs(configs))
76        }
77
78        // Handle YAML null / missing when serde calls the deserializer explicitly.
79        fn visit_unit<E: de::Error>(self) -> Result<Self::Value, E> {
80            Ok(ArchivesConfig::Configs(vec![]))
81        }
82
83        fn visit_none<E: de::Error>(self) -> Result<Self::Value, E> {
84            Ok(ArchivesConfig::Configs(vec![]))
85        }
86    }
87
88    deserializer.deserialize_any(ArchivesVisitor)
89}
90
91/// Custom deserializer for the `signs` / `sign` field.
92/// Accepts:
93///   - null/missing → empty vec (via serde default)
94///   - a single object → vec of one SignConfig
95///   - an array → vec of SignConfig
96pub(super) fn deserialize_signs<'de, D>(deserializer: D) -> Result<Vec<SignConfig>, D::Error>
97where
98    D: Deserializer<'de>,
99{
100    use serde::de::{self, Visitor};
101
102    struct SignsVisitor;
103
104    impl<'de> Visitor<'de> for SignsVisitor {
105        type Value = Vec<SignConfig>;
106
107        fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
108            f.write_str("a sign config object or an array of sign config objects")
109        }
110
111        fn visit_seq<A: de::SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
112            let mut configs = Vec::new();
113            while let Some(item) = seq.next_element::<SignConfig>()? {
114                configs.push(item);
115            }
116            Ok(configs)
117        }
118
119        fn visit_map<M: de::MapAccess<'de>>(self, map: M) -> Result<Self::Value, M::Error> {
120            let config = SignConfig::deserialize(de::value::MapAccessDeserializer::new(map))?;
121            Ok(vec![config])
122        }
123
124        fn visit_unit<E: de::Error>(self) -> Result<Self::Value, E> {
125            Ok(Vec::new())
126        }
127
128        fn visit_none<E: de::Error>(self) -> Result<Self::Value, E> {
129            Ok(Vec::new())
130        }
131    }
132
133    deserializer.deserialize_any(SignsVisitor)
134}
135
136// `binary_signs[].artifacts` is constrained at deserialize time (not as a
137// serde-typed enum) because `SignConfig` is shared with the top-level `signs:`
138// field, which legitimately accepts a wider set (`all`, `archive`, `binary`,
139// `checksum`, `package`, `sbom`, `none`). Promoting `artifacts` to an enum
140// would either narrow that surface or require a parallel `BinarySignConfig`
141// type duplicating every `SignConfig` field — the runtime check below keeps
142// `SignConfig` a single shared shape while still rejecting misconfigured
143// `binary_signs` entries at config-load time.
144//
145// The JSON schema for `binary_signs[]` therefore inherits `SignConfig`'s
146// unconstrained `artifacts: Option<String>` — the constraint lives in the
147// custom deserializer below and is exercised by the parse-time tests
148// `test_binary_signs_artifacts_*` further down this file.
149
150/// Wraps [`deserialize_signs`] and enforces that each entry's `artifacts`
151/// is one of the binary-only allowed values (`binary`, `none`, or omitted).
152/// Catches misconfiguration at load time instead of producing a silent
153/// no-op signing pipe.
154pub(super) fn deserialize_binary_signs<'de, D>(deserializer: D) -> Result<Vec<SignConfig>, D::Error>
155where
156    D: Deserializer<'de>,
157{
158    let configs = deserialize_signs(deserializer)?;
159    for (idx, cfg) in configs.iter().enumerate() {
160        if let Some(art) = cfg.artifacts.as_deref()
161            && art != "binary"
162            && art != "none"
163        {
164            return Err(serde::de::Error::custom(format!(
165                "binary_signs[{idx}].artifacts: '{art}' is not allowed; \
166                 binary_signs accepts only 'binary' or 'none' (use top-level \
167                 `signs:` for broader artifact filters)"
168            )));
169        }
170    }
171    Ok(configs)
172}
173
174// ---------------------------------------------------------------------------
175// WrapInDirectory – accepts bool (true = default dir name) or string
176// ---------------------------------------------------------------------------
177
178#[derive(Debug, Clone, PartialEq, Serialize, JsonSchema)]
179#[serde(untagged)]
180pub enum WrapInDirectory {
181    Bool(bool),
182    Name(String),
183}
184
185impl<'de> serde::Deserialize<'de> for WrapInDirectory {
186    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
187        let value = serde_yaml_ng::Value::deserialize(deserializer)?;
188        match value {
189            serde_yaml_ng::Value::Bool(b) => Ok(WrapInDirectory::Bool(b)),
190            serde_yaml_ng::Value::String(s) => Ok(WrapInDirectory::Name(s)),
191            _ => Err(serde::de::Error::custom("expected bool or string")),
192        }
193    }
194}
195
196impl WrapInDirectory {
197    /// Resolve the directory name to wrap archive contents in.
198    ///
199    /// When `true`, uses `default_name` (typically the archive stem).
200    /// When `false` or an empty string, returns `None` (no wrapping).
201    /// Otherwise returns the custom name.
202    pub fn directory_name(&self, default_name: &str) -> Option<String> {
203        match self {
204            WrapInDirectory::Bool(true) => Some(default_name.to_string()),
205            WrapInDirectory::Bool(false) => None,
206            WrapInDirectory::Name(s) if s.is_empty() => None,
207            WrapInDirectory::Name(s) => Some(s.clone()),
208        }
209    }
210}
211
212// ---------------------------------------------------------------------------
213// ArchiveConfig
214// ---------------------------------------------------------------------------
215
216#[derive(Debug, Clone, Serialize, Default, JsonSchema)]
217#[serde(deny_unknown_fields)]
218pub struct ArchiveConfig {
219    /// Unique identifier for cross-referencing this archive from other configs.
220    /// Defaults to `"default"` so a parse->serialise->reparse round-trip is
221    /// stable (stored verbatim, not as an Option).
222    pub id: Option<String>,
223    /// Archive filename template (supports templates, e.g., "{{ ProjectName }}_{{ Version }}_{{ Os }}_{{ Arch }}").
224    pub name_template: Option<String>,
225    /// Archive formats: tar.gz, tar.xz, tar.zst, tar, zip, gz, xz, or binary.
226    /// `gz` and `xz` are single-file compressors — supplying multiple input
227    /// files errors. Plural list; one archive per format is produced for each
228    /// target.
229    pub formats: Option<Vec<String>>,
230    /// Per-OS format overrides for this archive config.
231    pub format_overrides: Option<Vec<FormatOverride>>,
232    /// Extra files to include in the archive (glob patterns or detailed src/dst specs).
233    pub files: Option<Vec<ArchiveFileSpec>>,
234    /// Binary names to include (defaults to all binaries from matched builds).
235    pub binaries: Option<Vec<String>>,
236    /// When set, wrap archive contents in a top-level directory.
237    /// Accepts `true` (use archive stem as directory name), `false` (no wrapping),
238    /// or a string template for a custom directory name.
239    pub wrap_in_directory: Option<WrapInDirectory>,
240    /// Build IDs filter: only include artifacts from builds whose `id` is in this list.
241    pub ids: Option<Vec<String>>,
242    /// When true, create archive with no binaries (metadata-only).
243    pub meta: Option<bool>,
244    /// File permissions applied to binaries in archives.
245    pub builds_info: Option<ArchiveFileInfo>,
246    /// Strip binary parent directory in archive (place binaries at archive root).
247    pub strip_binary_directory: Option<bool>,
248    /// Allow different binary counts across targets. Default false (warn on mismatch).
249    pub allow_different_binary_count: Option<bool>,
250    /// Pre/post archive hooks (`before`/`after`).
251    pub hooks: Option<ArchiveHooksConfig>,
252    /// Templated files scoped to this archive entry. Rendered per-archive
253    /// (so each entry's `dst:` and contents see `.Os`, `.Arch`, `.Target`,
254    /// `.Format`, etc.) and packed into the archive at the rendered `dst:`
255    /// path. The `archives[].templated_files:` field.
256    pub templated_files: Option<Vec<super::TemplateFileConfig>>,
257    /// Template-conditional gate: when the rendered result is falsy
258    /// (`"false"` / `"0"` / `"no"` / empty), the archive entry is skipped
259    /// entirely (no archives produced for this `id`). Render failure
260    /// hard-errors. "Filter artifacts with `if` statements" is listed as a
261    /// blanket promise — anodizer surfaces it explicitly to keep imported
262    /// configs portable).
263    #[serde(rename = "if")]
264    pub if_condition: Option<String>,
265    /// Turnkey shell-completion generation: auto-generate (or harvest, or
266    /// copy) completion files and bundle them into every archive produced by
267    /// this entry. See `CompletionsConfig` for the three generation modes.
268    pub completions: Option<super::CompletionsConfig>,
269    /// Turnkey man-page generation: auto-generate (or harvest, or copy) man
270    /// pages and bundle them into every archive produced by this entry. See
271    /// `ManpagesConfig` for the three generation modes.
272    pub manpages: Option<super::ManpagesConfig>,
273}
274
275/// Fold a deprecated singular `format: tar.gz` into the canonical
276/// `formats: [tar.gz]` list, emitting a `tracing::warn!` deprecation notice
277/// keyed by `context_label` (the archive id or override `os=` so the user
278/// can locate the offending entry). Returns the folded list (creating one
279/// if `formats` was `None` and `legacy` is `Some`).
280///
281/// Shared by `ArchiveConfig` and `FormatOverride` to keep the deprecation
282/// message + fold semantics in one place.
283fn fold_format_into_formats(
284    context_label: &str,
285    context_kind: &str,
286    formats: Option<Vec<String>>,
287    legacy: Option<String>,
288) -> Option<Vec<String>> {
289    let mut formats = formats;
290    if let Some(legacy) = legacy {
291        tracing::warn!(
292            "DEPRECATION: {}[{}]: 'format: {}' is deprecated; \
293             use 'formats: [{}]' instead.",
294            context_kind,
295            context_label,
296            legacy,
297            legacy
298        );
299        formats.get_or_insert_with(Vec::new).push(legacy);
300    }
301    formats
302}
303
304// Custom Deserialize that accepts deprecated aliases:
305// - `format: tar.gz` (singular String) folded into `formats: [tar.gz]`
306//
307// - `builds: [foo]` folded into `ids: [foo]`
308//
309// Each alias hit emits a `tracing::warn!` deprecation notice.
310impl<'de> Deserialize<'de> for ArchiveConfig {
311    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
312    where
313        D: Deserializer<'de>,
314    {
315        #[derive(Deserialize, Default)]
316        #[serde(default, deny_unknown_fields)]
317        struct Raw {
318            id: Option<String>,
319            name_template: Option<String>,
320            formats: Option<Vec<String>>,
321            format: Option<String>,
322            format_overrides: Option<Vec<FormatOverride>>,
323            files: Option<Vec<ArchiveFileSpec>>,
324            binaries: Option<Vec<String>>,
325            wrap_in_directory: Option<WrapInDirectory>,
326            ids: Option<Vec<String>>,
327            builds: Option<Vec<String>>,
328            meta: Option<bool>,
329            builds_info: Option<ArchiveFileInfo>,
330            strip_binary_directory: Option<bool>,
331            allow_different_binary_count: Option<bool>,
332            hooks: Option<ArchiveHooksConfig>,
333            templated_files: Option<Vec<super::TemplateFileConfig>>,
334            #[serde(rename = "if")]
335            if_condition: Option<String>,
336            completions: Option<super::CompletionsConfig>,
337            manpages: Option<super::ManpagesConfig>,
338        }
339
340        let raw = Raw::deserialize(deserializer)?;
341
342        let id_label = raw.id.clone().unwrap_or_else(|| "default".to_string());
343        let formats = fold_format_into_formats(
344            &format!("id={}", id_label),
345            "archives",
346            raw.formats,
347            raw.format,
348        );
349        let mut ids = raw.ids;
350        if let Some(legacy) = raw.builds {
351            tracing::warn!(
352                "DEPRECATION: archives[id={}]: 'builds: {:?}' is deprecated; \
353                 use 'ids: [...]' instead.",
354                id_label,
355                legacy
356            );
357            let target = ids.get_or_insert_with(Vec::new);
358            target.extend(legacy);
359        }
360
361        Ok(ArchiveConfig {
362            id: raw.id.or_else(|| Some("default".to_string())),
363            name_template: raw.name_template,
364            formats,
365            format_overrides: raw.format_overrides,
366            files: raw.files,
367            binaries: raw.binaries,
368            wrap_in_directory: raw.wrap_in_directory,
369            ids,
370            meta: raw.meta,
371            builds_info: raw.builds_info,
372            strip_binary_directory: raw.strip_binary_directory,
373            allow_different_binary_count: raw.allow_different_binary_count,
374            hooks: raw.hooks,
375            templated_files: raw.templated_files,
376            if_condition: raw.if_condition,
377            completions: raw.completions,
378            manpages: raw.manpages,
379        })
380    }
381}
382
383#[derive(Debug, Clone, Serialize, JsonSchema)]
384#[serde(deny_unknown_fields)]
385pub struct FormatOverride {
386    /// Operating system this override applies to (e.g., "windows", "darwin", "linux").
387    pub os: String,
388    /// Plural format overrides for this OS: tar.gz, tar.xz, tar.zst, tar, zip,
389    /// gz, xz, or binary.
390    pub formats: Option<Vec<String>>,
391}
392
393// Custom Deserialize that accepts both `formats: [tar.gz]` (canonical) and
394// the deprecated singular `format: tar.gz`. The legacy spelling is folded
395// into `formats` at parse time via the shared `fold_format_into_formats`
396// helper, which also emits the deprecation warning.
397impl<'de> Deserialize<'de> for FormatOverride {
398    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
399    where
400        D: Deserializer<'de>,
401    {
402        #[derive(Deserialize, Default)]
403        #[serde(default, deny_unknown_fields)]
404        struct Raw {
405            os: String,
406            formats: Option<Vec<String>>,
407            format: Option<String>,
408        }
409        let raw = Raw::deserialize(deserializer)?;
410        let formats = fold_format_into_formats(
411            &format!("os={}", raw.os),
412            "archives.format_overrides",
413            raw.formats,
414            raw.format,
415        );
416        Ok(FormatOverride {
417            os: raw.os,
418            formats,
419        })
420    }
421}
422
423/// Specifies a file to include in archives. Can be a simple glob string or a
424/// detailed object with src/dst/info fields for controlling archive placement
425/// and file metadata.
426///
427/// NOTE: This is intentionally a separate type from [`ExtraFileSpec`] (used for
428/// checksum/release extra_files). `ArchiveFileSpec` needs `src`/`dst`/`info`
429/// fields for archive placement and file metadata (owner, group, mode, mtime),
430/// while `ExtraFileSpec` needs `glob`/`name_template` for checksumming and
431/// upload renaming. The fields and semantics are different enough that a unified
432/// type would be confusing.
433#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
434#[serde(untagged)]
435pub enum ArchiveFileSpec {
436    Glob(String),
437    Detailed {
438        src: String,
439        dst: Option<String>,
440        info: Option<ArchiveFileInfo>,
441        /// When true, strip the parent directory from the file path in the archive.
442        strip_parent: Option<bool>,
443    },
444}
445
446impl PartialEq<&str> for ArchiveFileSpec {
447    fn eq(&self, other: &&str) -> bool {
448        match self {
449            ArchiveFileSpec::Glob(s) => s.as_str() == *other,
450            _ => false,
451        }
452    }
453}
454
455/// Shared file metadata (owner, group, mode, mtime) used by both archive entries
456/// and nFPM package contents. Previously duplicated as `ArchiveFileInfo` and
457/// `NfpmFileInfo`; now unified.
458#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default, JsonSchema)]
459#[serde(default, deny_unknown_fields)]
460pub struct FileInfo {
461    /// File owner name (e.g., "root").
462    pub owner: Option<String>,
463    /// File group name (e.g., "root").
464    pub group: Option<String>,
465    /// File permission mode. Accepts a YAML int (decimal, e.g. `420` for
466    /// `0o644`) or an octal-prefixed string (`"0o644"`, `"0644"`). This
467    /// a `uint32` type for `Mode` on archive/nfpm contents
468    /// while letting users spell octal naturally in YAML.
469    pub mode: Option<StringOrU32>,
470    /// File modification time in RFC3339 format (e.g., "2024-01-01T00:00:00Z").
471    pub mtime: Option<String>,
472}
473
474/// Backward-compatible alias for archive code.
475pub type ArchiveFileInfo = FileInfo;
476
477/// Parse an octal mode string into a `u32`, handling common YAML-friendly
478/// representations: `"0755"`, `"0o755"`, `"0O755"`, `"755"`, and `"0"`.
479pub fn parse_octal_mode(s: &str) -> Option<u32> {
480    let cleaned = s
481        .strip_prefix("0o")
482        .or_else(|| s.strip_prefix("0O"))
483        .unwrap_or(s);
484    let cleaned = if cleaned.is_empty() { "0" } else { cleaned };
485    u32::from_str_radix(cleaned, 8).ok()
486}
487
488/// The set of archive format strings recognised by the archive stage.
489/// Used for early validation so typos are caught at config load time rather
490/// than mid-pipeline.
491pub const VALID_ARCHIVE_FORMATS: &[&str] = &[
492    "tar.gz",
493    "tgz",
494    "tar.xz",
495    "txz",
496    "tar.zst",
497    "tzst",
498    "tar",
499    "zip",
500    "gz",
501    "xz",
502    crate::artifact::FORMAT_BINARY,
503    "none",
504];
505
506// ---------------------------------------------------------------------------
507// ChecksumConfig
508// ---------------------------------------------------------------------------
509
510/// Specifies an extra file to include in checksums or release uploads. Can be a
511/// simple glob string or a detailed object with glob and name_template fields.
512///
513/// See [`ArchiveFileSpec`] doc comment for why this is a separate type.
514#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
515#[serde(untagged)]
516pub enum ExtraFileSpec {
517    Glob(String),
518    Detailed {
519        glob: String,
520        /// Optional override for the upload filename.
521        #[serde(default)]
522        name_template: Option<String>,
523        /// When true, treat a glob that matches zero files as a no-op
524        /// rather than a hard error. Useful for assets produced only in
525        /// CI (e.g. signing public keys derived from a secret) that
526        /// must not break local snapshot/dry-run flows. Defaults to
527        /// false, matching the prior fail-fast behavior.
528        #[serde(default)]
529        allow_empty: bool,
530    },
531}
532
533impl ExtraFileSpec {
534    /// Return the glob pattern for this spec.
535    pub fn glob(&self) -> &str {
536        match self {
537            ExtraFileSpec::Glob(s) => s,
538            ExtraFileSpec::Detailed { glob, .. } => glob,
539        }
540    }
541
542    /// Return the optional name_template (only present in Detailed variant).
543    pub fn name_template(&self) -> Option<&str> {
544        match self {
545            ExtraFileSpec::Glob(_) => None,
546            ExtraFileSpec::Detailed { name_template, .. } => name_template.as_deref(),
547        }
548    }
549
550    /// Return whether this spec allows a zero-match glob without erroring
551    /// (Detailed variant only; the bare string form is always fail-fast).
552    pub fn allow_empty(&self) -> bool {
553        match self {
554            ExtraFileSpec::Glob(_) => false,
555            ExtraFileSpec::Detailed { allow_empty, .. } => *allow_empty,
556        }
557    }
558}
559
560/// A file whose contents are rendered through the template engine before use.
561/// Used by `templated_extra_files` across multiple stages.
562#[derive(Debug, Clone, Serialize, Deserialize, Default, JsonSchema, PartialEq)]
563#[serde(default, deny_unknown_fields)]
564pub struct TemplatedExtraFile {
565    /// Source template file path.
566    pub src: String,
567    /// Destination filename for the rendered output.
568    /// Supports template variables (e.g. `"{{ ProjectName }}-NOTES.txt"`).
569    pub dst: Option<String>,
570    /// File permissions in octal notation as a string, e.g. `"0755"`.
571    /// Parsed at runtime via `parse_octal_mode()` to avoid YAML interpreting as decimal.
572    pub mode: Option<String>,
573}
574
575/// Content format for per-artifact sidecars written in `split` mode.
576#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default, JsonSchema)]
577#[serde(rename_all = "lowercase")]
578pub enum ChecksumSplitFormat {
579    /// Only the raw hex hash, no filename, no trailing newline. Matches
580    /// GoReleaser's split-checksum output. Default.
581    #[default]
582    Bare,
583    /// `<hash>  <filename>` with a trailing newline — the coreutils / BSD
584    /// digest format, so the sidecar verifies directly with
585    /// `shasum -c` / `sha256sum -c` from the directory holding the artifact.
586    Coreutils,
587}
588
589#[derive(Debug, Clone, Serialize, Deserialize, Default, JsonSchema)]
590#[serde(default, deny_unknown_fields)]
591pub struct ChecksumConfig {
592    /// Checksum filename template (default: "{{ ProjectName }}_{{ Version }}_checksums.txt").
593    pub name_template: Option<String>,
594    /// Hash algorithm (default: `sha256`). Accepted values: `sha1`, `sha224`,
595    /// `sha256`, `sha384`, `sha512`, `sha3-224`, `sha3-256`, `sha3-384`,
596    /// `sha3-512`, `blake2b`, `blake2s`, `blake3`, `crc32`, `md5`. An
597    /// unrecognized value is rejected at checksum-stage entry. The authoritative
598    /// set is [`ChecksumConfig::SUPPORTED_ALGORITHMS`].
599    pub algorithm: Option<String>,
600    /// Disable checksums. Accepts bool or template string.
601    /// Accepts the legacy `disable:` spelling via serde alias for back-compat.
602    #[serde(
603        alias = "disable",
604        deserialize_with = "deserialize_string_or_bool_opt",
605        default
606    )]
607    pub skip: Option<StringOrBool>,
608    /// Extra files to include in the checksum file (beyond build artifacts).
609    pub extra_files: Option<Vec<ExtraFileSpec>>,
610    /// Extra files whose contents are rendered through the template engine before inclusion.
611    /// Unlike `extra_files` which copy as-is, template variables like `{{ Tag }}` are expanded.
612    pub templated_extra_files: Option<Vec<TemplatedExtraFile>>,
613    /// Build IDs filter: only checksum artifacts from builds whose `id` is in this list.
614    pub ids: Option<Vec<String>>,
615    /// When true, produce one checksum file per artifact instead of a combined file.
616    pub split: Option<bool>,
617    /// Sidecar content format when `split: true` (default: `bare`). Set to
618    /// `coreutils` to write `<hash>  <filename>` so each sidecar verifies with
619    /// `shasum -c`. Ignored in combined mode (the combined file is always
620    /// coreutils-format).
621    pub split_format: Option<ChecksumSplitFormat>,
622}
623
624impl ChecksumConfig {
625    /// Default checksum filename template (combined mode). Mirrors
626    /// the checksums config.
627    pub const DEFAULT_NAME_TEMPLATE: &'static str = "{{ ProjectName }}_{{ Version }}_checksums.txt";
628
629    /// Default hash algorithm (`sha256`).
630    pub const DEFAULT_ALGORITHM: &'static str = "sha256";
631
632    /// The closed set of accepted [`Self::algorithm`] values. This is the
633    /// authoritative list the checksum stage's hash dispatch and
634    /// `validate_algorithm` are kept in sync with (a `stage-checksum`
635    /// drift-guard test asserts the two never diverge), so the config rustdoc
636    /// can name the full set without hand-copying a list that rots.
637    pub const SUPPORTED_ALGORITHMS: &'static [&'static str] = &[
638        "sha1", "sha224", "sha256", "sha384", "sha512", "sha3-224", "sha3-256", "sha3-384",
639        "sha3-512", "blake2b", "blake2s", "blake3", "crc32", "md5",
640    ];
641
642    /// Resolve the hash algorithm, falling back to the project default
643    /// when the user did not specify one. Stages MUST call this rather
644    /// than reading `self.algorithm` directly, so a future default change
645    /// (or user-facing override resolution) ends up in one place.
646    pub fn resolved_algorithm(&self) -> &str {
647        self.algorithm.as_deref().unwrap_or(Self::DEFAULT_ALGORITHM)
648    }
649
650    /// Whether split-mode (one sidecar per artifact) is requested.
651    /// Defaults to `false` (combined-file mode).
652    pub fn resolved_split(&self) -> bool {
653        self.split.unwrap_or(false)
654    }
655
656    /// Resolve the combined-mode checksum filename template, falling back
657    /// to the canonical default. Returns the raw template
658    /// string; the caller still renders it through Tera.
659    ///
660    /// Split mode constructs sidecar names per-artifact at the call site
661    /// (`<artifact>.<algo>` literal format) and intentionally does NOT
662    /// route through this accessor — that path needs no template rendering.
663    pub fn resolved_combined_name_template(&self) -> &str {
664        self.name_template
665            .as_deref()
666            .unwrap_or(Self::DEFAULT_NAME_TEMPLATE)
667    }
668
669    /// Resolve the combined-checksums `name_template` for a crate, applying the
670    /// canonical precedence — the crate's own `checksum.name_template`, then the
671    /// global `defaults.checksum.name_template`, then [`Self::DEFAULT_NAME_TEMPLATE`].
672    ///
673    /// The single source of truth shared by the checksum stage (which writes the
674    /// file) and the install-script stage (which references it in the generated
675    /// `install.sh`), so the two can never derive different names.
676    pub fn resolve_combined_name_template<'a>(
677        crate_checksum: Option<&'a ChecksumConfig>,
678        global_checksum: Option<&'a ChecksumConfig>,
679    ) -> &'a str {
680        crate_checksum
681            .and_then(|c| c.name_template.as_deref())
682            .or_else(|| global_checksum.and_then(|c| c.name_template.as_deref()))
683            .unwrap_or(Self::DEFAULT_NAME_TEMPLATE)
684    }
685}
686
687// ---------------------------------------------------------------------------
688// ContentSource — inline string, from_file, or from_url
689// ---------------------------------------------------------------------------
690
691/// A content source that can be an inline string, read from a file, or fetched
692/// from a URL. Used for release header/footer values.
693///
694/// YAML examples:
695///
696/// ```yaml
697/// header: "inline text"
698/// header:
699///   from_file: ./RELEASE_HEADER.md
700/// header:
701///   from_url: https://example.com/header.md
702/// header:
703///   from_url: https://example.com/header.md
704///   headers:
705///     X-API-Token: "{{ Env.API_TOKEN }}"
706///     Accept: "text/markdown"
707/// ```
708///
709/// Both `from_file` path and `from_url` URL are template-rendered before use.
710/// Header values are template-rendered.
711#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
712#[serde(untagged)]
713pub enum ContentSource {
714    Inline(String),
715    FromFile {
716        from_file: String,
717    },
718    FromUrl {
719        from_url: String,
720        /// Optional HTTP headers (value templates allowed). Enables private
721        /// mirrors and authenticated endpoints.
722        #[serde(default, skip_serializing_if = "Option::is_none")]
723        headers: Option<HashMap<String, String>>,
724    },
725}
726
727impl PartialEq for ContentSource {
728    fn eq(&self, other: &Self) -> bool {
729        match (self, other) {
730            (Self::Inline(a), Self::Inline(b)) => a == b,
731            (Self::FromFile { from_file: a }, Self::FromFile { from_file: b }) => a == b,
732            (
733                Self::FromUrl {
734                    from_url: a,
735                    headers: ha,
736                },
737                Self::FromUrl {
738                    from_url: b,
739                    headers: hb,
740                },
741            ) => a == b && ha == hb,
742            _ => false,
743        }
744    }
745}
746
747#[cfg(test)]
748mod tests {
749    use super::*;
750
751    // The `archives`/`signs`/`binary_signs` fields use hand-written
752    // deserializers (untagged shapes serde can't derive). Each visitor arm —
753    // bool, sequence, single-map, null — is driven here through a wrapper
754    // struct that mirrors the real field attributes.
755
756    #[derive(Deserialize)]
757    struct ArchivesWrapper {
758        #[serde(default, deserialize_with = "deserialize_archives_config")]
759        archives: ArchivesConfig,
760    }
761
762    #[test]
763    fn archives_false_is_disabled() {
764        let w: ArchivesWrapper = serde_yaml_ng::from_str("archives: false").unwrap();
765        assert!(matches!(w.archives, ArchivesConfig::Disabled));
766    }
767
768    #[test]
769    fn archives_true_is_rejected() {
770        // `true` is meaningless for archives — only `false` (disable) or a list.
771        let r: Result<ArchivesWrapper, _> = serde_yaml_ng::from_str("archives: true");
772        assert!(r.is_err(), "archives: true must be rejected");
773    }
774
775    #[test]
776    fn archives_list_becomes_configs() {
777        let w: ArchivesWrapper =
778            serde_yaml_ng::from_str("archives:\n  - id: a\n  - id: b\n").unwrap();
779        match w.archives {
780            ArchivesConfig::Configs(c) => assert_eq!(c.len(), 2),
781            other => panic!("expected Configs, got {other:?}"),
782        }
783    }
784
785    #[test]
786    fn archives_null_defaults_to_empty_configs() {
787        let w: ArchivesWrapper = serde_yaml_ng::from_str("archives: null").unwrap();
788        match w.archives {
789            ArchivesConfig::Configs(c) => assert!(c.is_empty()),
790            other => panic!("expected empty Configs, got {other:?}"),
791        }
792    }
793
794    #[derive(Deserialize)]
795    struct SignsWrapper {
796        #[serde(default, deserialize_with = "deserialize_signs")]
797        signs: Vec<SignConfig>,
798    }
799
800    #[test]
801    fn signs_single_object_becomes_one_element_vec() {
802        // A single sign-config map (not wrapped in a list) is accepted.
803        let w: SignsWrapper = serde_yaml_ng::from_str("signs:\n  artifacts: all\n").unwrap();
804        assert_eq!(w.signs.len(), 1);
805        assert_eq!(w.signs[0].artifacts.as_deref(), Some("all"));
806    }
807
808    #[test]
809    fn signs_sequence_collects_all() {
810        let w: SignsWrapper =
811            serde_yaml_ng::from_str("signs:\n  - artifacts: all\n  - artifacts: checksum\n")
812                .unwrap();
813        assert_eq!(w.signs.len(), 2);
814    }
815
816    #[test]
817    fn signs_null_is_empty_vec() {
818        let w: SignsWrapper = serde_yaml_ng::from_str("signs: null").unwrap();
819        assert!(w.signs.is_empty());
820    }
821
822    #[derive(Deserialize)]
823    struct BinarySignsWrapper {
824        #[serde(default, deserialize_with = "deserialize_binary_signs")]
825        binary_signs: Vec<SignConfig>,
826    }
827
828    #[test]
829    fn binary_signs_accepts_binary_and_none() {
830        let w: BinarySignsWrapper =
831            serde_yaml_ng::from_str("binary_signs:\n  - artifacts: binary\n").unwrap();
832        assert_eq!(w.binary_signs.len(), 1);
833        let w2: BinarySignsWrapper =
834            serde_yaml_ng::from_str("binary_signs:\n  - artifacts: none\n").unwrap();
835        assert_eq!(w2.binary_signs.len(), 1);
836    }
837
838    #[test]
839    fn binary_signs_rejects_broad_artifact_filter() {
840        // `all` is valid for top-level `signs:` but not the binary-only field.
841        let r: Result<BinarySignsWrapper, _> =
842            serde_yaml_ng::from_str("binary_signs:\n  - artifacts: all\n");
843        assert!(
844            r.is_err(),
845            "binary_signs must reject a non-binary artifact filter"
846        );
847    }
848
849    // --- WrapInDirectory ---------------------------------------------------
850
851    #[test]
852    fn wrap_in_directory_bool_true_uses_default_name() {
853        assert_eq!(
854            WrapInDirectory::Bool(true).directory_name("myapp_1.0"),
855            Some("myapp_1.0".to_string())
856        );
857    }
858
859    #[test]
860    fn wrap_in_directory_bool_false_disables_wrapping() {
861        assert_eq!(
862            WrapInDirectory::Bool(false).directory_name("myapp_1.0"),
863            None
864        );
865    }
866
867    #[test]
868    fn wrap_in_directory_empty_string_disables_wrapping() {
869        // An empty custom name is treated as "no wrapping", not a dir named "".
870        assert_eq!(
871            WrapInDirectory::Name(String::new()).directory_name("fallback"),
872            None
873        );
874    }
875
876    #[test]
877    fn wrap_in_directory_custom_name_overrides_default() {
878        assert_eq!(
879            WrapInDirectory::Name("custom".into()).directory_name("fallback"),
880            Some("custom".to_string())
881        );
882    }
883
884    #[derive(Deserialize)]
885    struct WrapWrapper {
886        wrap_in_directory: WrapInDirectory,
887    }
888
889    #[test]
890    fn wrap_in_directory_deserializes_bool_and_string() {
891        let b: WrapWrapper = serde_yaml_ng::from_str("wrap_in_directory: true").unwrap();
892        assert_eq!(b.wrap_in_directory, WrapInDirectory::Bool(true));
893        let s: WrapWrapper = serde_yaml_ng::from_str("wrap_in_directory: dist").unwrap();
894        assert_eq!(s.wrap_in_directory, WrapInDirectory::Name("dist".into()));
895    }
896
897    #[test]
898    fn wrap_in_directory_rejects_non_scalar() {
899        // A list is neither a bool nor a string — the hand-written deserializer
900        // must error rather than coerce.
901        let r: Result<WrapWrapper, _> = serde_yaml_ng::from_str("wrap_in_directory:\n  - a\n");
902        assert!(r.is_err());
903    }
904
905    // --- parse_octal_mode --------------------------------------------------
906
907    #[test]
908    fn parse_octal_mode_accepts_common_forms() {
909        assert_eq!(parse_octal_mode("0755"), Some(0o755));
910        assert_eq!(parse_octal_mode("0o755"), Some(0o755));
911        assert_eq!(parse_octal_mode("0O755"), Some(0o755));
912        assert_eq!(parse_octal_mode("755"), Some(0o755));
913        // Bare "0o"/"0O" with nothing after → the cleaned string is empty and
914        // is normalized to "0".
915        assert_eq!(parse_octal_mode("0o"), Some(0));
916        assert_eq!(parse_octal_mode("0"), Some(0));
917    }
918
919    #[test]
920    fn parse_octal_mode_rejects_non_octal() {
921        // 8 and 9 are not octal digits.
922        assert_eq!(parse_octal_mode("0o899"), None);
923        assert_eq!(parse_octal_mode("garbage"), None);
924    }
925
926    // --- ChecksumConfig::resolve_combined_name_template (static precedence) --
927
928    #[test]
929    fn resolve_combined_name_template_prefers_crate_then_global_then_default() {
930        let crate_cfg = ChecksumConfig {
931            name_template: Some("crate.txt".into()),
932            ..Default::default()
933        };
934        let global_cfg = ChecksumConfig {
935            name_template: Some("global.txt".into()),
936            ..Default::default()
937        };
938        // Crate value wins over global and default.
939        assert_eq!(
940            ChecksumConfig::resolve_combined_name_template(Some(&crate_cfg), Some(&global_cfg)),
941            "crate.txt"
942        );
943        // With no crate override, global wins.
944        let bare = ChecksumConfig::default();
945        assert_eq!(
946            ChecksumConfig::resolve_combined_name_template(Some(&bare), Some(&global_cfg)),
947            "global.txt"
948        );
949        // Neither set → the canonical default.
950        assert_eq!(
951            ChecksumConfig::resolve_combined_name_template(None, None),
952            ChecksumConfig::DEFAULT_NAME_TEMPLATE
953        );
954    }
955
956    // --- ChecksumSplitFormat ----------------------------------------------
957
958    #[test]
959    fn checksum_split_format_defaults_to_bare() {
960        assert_eq!(ChecksumSplitFormat::default(), ChecksumSplitFormat::Bare);
961    }
962
963    #[test]
964    fn checksum_split_format_deserializes_lowercase() {
965        assert_eq!(
966            serde_yaml_ng::from_str::<ChecksumSplitFormat>("bare").unwrap(),
967            ChecksumSplitFormat::Bare
968        );
969        assert_eq!(
970            serde_yaml_ng::from_str::<ChecksumSplitFormat>("coreutils").unwrap(),
971            ChecksumSplitFormat::Coreutils
972        );
973        assert!(serde_yaml_ng::from_str::<ChecksumSplitFormat>("Coreutils").is_err());
974    }
975
976    // --- ExtraFileSpec::allow_empty ---------------------------------------
977
978    #[test]
979    fn extra_file_spec_allow_empty_only_true_for_detailed_opt_in() {
980        // Bare glob form is always fail-fast (allow_empty == false).
981        let bare: ExtraFileSpec = serde_yaml_ng::from_str("dist/*.sig").unwrap();
982        assert!(!bare.allow_empty());
983        // Detailed with allow_empty: true opts in.
984        let opt_in: ExtraFileSpec =
985            serde_yaml_ng::from_str("glob: keys/*.pub\nallow_empty: true").unwrap();
986        assert!(opt_in.allow_empty());
987        assert_eq!(opt_in.glob(), "keys/*.pub");
988        // Detailed defaulting allow_empty stays false.
989        let default_off: ExtraFileSpec = serde_yaml_ng::from_str("glob: docs/*.pdf").unwrap();
990        assert!(!default_off.allow_empty());
991    }
992
993    // --- ArchiveFileSpec PartialEq<&str> ----------------------------------
994
995    #[test]
996    fn archive_file_spec_str_eq_matches_glob_only() {
997        assert!(ArchiveFileSpec::Glob("README.md".into()) == "README.md");
998        assert!(ArchiveFileSpec::Glob("README.md".into()) != "other");
999        // The Detailed variant never equals a bare string.
1000        let detailed = ArchiveFileSpec::Detailed {
1001            src: "README.md".into(),
1002            dst: None,
1003            info: None,
1004            strip_parent: None,
1005        };
1006        assert!(detailed != "README.md");
1007    }
1008
1009    // --- ArchiveConfig deprecation folds ----------------------------------
1010
1011    #[test]
1012    fn archive_config_folds_singular_format_into_formats() {
1013        // The deprecated `format: tar.gz` singular folds into `formats`.
1014        let c: ArchiveConfig = serde_yaml_ng::from_str("format: tar.gz").unwrap();
1015        assert_eq!(c.formats.as_deref().unwrap(), ["tar.gz"]);
1016    }
1017
1018    #[test]
1019    fn archive_config_folds_deprecated_builds_into_ids() {
1020        let c: ArchiveConfig = serde_yaml_ng::from_str("ids: [keep]\nbuilds: [legacy]").unwrap();
1021        let ids = c.ids.unwrap();
1022        assert!(ids.contains(&"keep".to_string()));
1023        assert!(ids.contains(&"legacy".to_string()));
1024    }
1025
1026    #[test]
1027    fn archive_config_defaults_id_to_default() {
1028        // A parse->serialise round-trip must be stable, so `id` materializes
1029        // to "default" when omitted.
1030        let c: ArchiveConfig = serde_yaml_ng::from_str("name_template: x").unwrap();
1031        assert_eq!(c.id.as_deref(), Some("default"));
1032        // An explicit id is preserved verbatim.
1033        let named: ArchiveConfig = serde_yaml_ng::from_str("id: bins").unwrap();
1034        assert_eq!(named.id.as_deref(), Some("bins"));
1035    }
1036
1037    #[test]
1038    fn format_override_folds_singular_format() {
1039        let o: FormatOverride = serde_yaml_ng::from_str("os: windows\nformat: zip").unwrap();
1040        assert_eq!(o.os, "windows");
1041        assert_eq!(o.formats.as_deref().unwrap(), ["zip"]);
1042    }
1043
1044    // --- ContentSource PartialEq ------------------------------------------
1045
1046    #[test]
1047    fn content_source_partial_eq_by_variant_and_payload() {
1048        assert_eq!(
1049            ContentSource::Inline("a".into()),
1050            ContentSource::Inline("a".into())
1051        );
1052        assert_ne!(
1053            ContentSource::Inline("a".into()),
1054            ContentSource::Inline("b".into())
1055        );
1056        // Same string but different variant must not compare equal.
1057        assert_ne!(
1058            ContentSource::Inline("a".into()),
1059            ContentSource::FromFile {
1060                from_file: "a".into()
1061            }
1062        );
1063        // FromUrl equality includes the headers map.
1064        let mut h = HashMap::new();
1065        h.insert("Accept".to_string(), "text/plain".to_string());
1066        let with_headers = ContentSource::FromUrl {
1067            from_url: "u".into(),
1068            headers: Some(h.clone()),
1069        };
1070        assert_eq!(
1071            with_headers,
1072            ContentSource::FromUrl {
1073                from_url: "u".into(),
1074                headers: Some(h),
1075            }
1076        );
1077        assert_ne!(
1078            with_headers,
1079            ContentSource::FromUrl {
1080                from_url: "u".into(),
1081                headers: None,
1082            }
1083        );
1084    }
1085
1086    #[test]
1087    fn content_source_from_url_deserializes_headers() {
1088        let cs: ContentSource = serde_yaml_ng::from_str(
1089            "from_url: https://example.com/h.md\nheaders:\n  X-Token: abc\n",
1090        )
1091        .unwrap();
1092        match cs {
1093            ContentSource::FromUrl { from_url, headers } => {
1094                assert_eq!(from_url, "https://example.com/h.md");
1095                assert_eq!(headers.unwrap().get("X-Token").unwrap(), "abc");
1096            }
1097            other => panic!("expected FromUrl, got {other:?}"),
1098        }
1099    }
1100
1101    // --- TemplatedExtraFile -----------------------------------------------
1102
1103    #[test]
1104    fn templated_extra_file_parses_and_defaults() {
1105        let full: TemplatedExtraFile = serde_yaml_ng::from_str(
1106            "src: NOTES.tera\ndst: \"{{ ProjectName }}-NOTES.txt\"\nmode: \"0644\"",
1107        )
1108        .unwrap();
1109        assert_eq!(full.src, "NOTES.tera");
1110        assert_eq!(full.dst.as_deref(), Some("{{ ProjectName }}-NOTES.txt"));
1111        assert_eq!(full.mode.as_deref(), Some("0644"));
1112        // Only `src` is required; dst/mode default to None.
1113        let minimal: TemplatedExtraFile = serde_yaml_ng::from_str("src: NOTES.tera").unwrap();
1114        assert!(minimal.dst.is_none());
1115        assert!(minimal.mode.is_none());
1116        // Unknown fields are rejected.
1117        assert!(serde_yaml_ng::from_str::<TemplatedExtraFile>("src: x\nbogus: y").is_err());
1118    }
1119}