Skip to main content

release_kit/
projection.rs

1//! The one pure target-specific projection over this binary's embedded
2//! sources.
3//!
4//! [`Projection::compute`] takes a [`ProjectionInput`], values alone, and
5//! answers the complete candidate artifact tree this installed binary
6//! would land in one target: every destination with its ownership
7//! [`Kind`], its [`Placement`], the complete proposed bytes, the rendered
8//! region where the destination is a marked region, and the embedded
9//! source paths it was rendered from. Staging and production landing
10//! share it byte for byte, so what an agent studies in a stage is what a
11//! landing writes.
12//!
13//! This module reads snippets and blocks through `src/embedded.rs`
14//! directly and reads nothing else: no target file, no environment, no
15//! Git state, no clock, no registry, no network. Whatever a projection
16//! needs from the target arrives as [`TargetEvidence`], gathered before
17//! construction by [`evidence::gather`], which lives in its own file so a
18//! source scan can hold this one to the pure boundary.
19//!
20//! Every pure piece of the landing model has one implementation here: the
21//! kind table, the token substitution, the block templating, the splice
22//! and marker judgments, and the Nix crate-shape judgment. Which embedded
23//! source belongs to which capability, and whether a capability is
24//! available at the target's dimensions, is the catalog's answer in
25//! `crate::profile::catalog`, and this module renders what the catalog
26//! selects. `src/landing.rs` re-exports the pieces under their old names.
27//!
28//! One input type, one compute function, one candidate shape: staging and
29//! every landing verb consume this one projection.
30
31pub mod evidence;
32
33use std::collections::BTreeMap;
34
35use serde::{Deserialize, Serialize};
36
37use crate::embedded;
38use crate::error::RkError;
39pub use crate::landing::manifest::Provider;
40use crate::landing::{CheckoutMode, Params};
41use crate::profile::ReleaseMode;
42use crate::profile::catalog::{self, Availability, Selection, Status};
43
44/// The complete input to one projection: the resolved landing parameters
45/// and the typed evidence read from the target beforehand.
46#[derive(Debug, Clone, PartialEq, Eq)]
47pub struct ProjectionInput {
48    /// The resolved landing parameters.
49    pub params: Params,
50    /// What the target already holds, as values.
51    pub evidence: TargetEvidence,
52}
53
54/// What a projection needs to know about the target, gathered before the
55/// projection runs and carried as values.
56#[derive(Debug, Clone, PartialEq, Eq, Default)]
57#[allow(
58    clippy::struct_excessive_bools,
59    reason = "each field is one independent fact read off the target, not a state a smaller type could carry"
60)]
61pub struct TargetEvidence {
62    /// The complete existing document at each block destination that
63    /// exists on disk, keyed by destination. An absent key is an absent
64    /// file.
65    pub documents: BTreeMap<String, Vec<u8>>,
66    /// The crate facts the Nix seed relies on.
67    pub crate_shape: CrateShape,
68    /// Whether `flake.nix` is present at the target, a link included.
69    pub flake_nix_present: bool,
70    /// Whether `flake.lock` is present at the target, a link included.
71    pub flake_lock_present: bool,
72    /// Whether the receipt already records `flake.nix`: a pair release-kit
73    /// landed is its own and is never withheld.
74    pub flake_recorded: bool,
75    /// Whether `.gitlab-ci.yml` is present at the target, a link included.
76    pub root_pipeline_present: bool,
77    /// Whether the receipt already records `.gitlab-ci.yml`: a root
78    /// pipeline release-kit landed is its own and is never withheld.
79    pub root_pipeline_recorded: bool,
80}
81
82impl TargetEvidence {
83    /// The existing document at one block destination.
84    #[must_use]
85    pub fn document(&self, destination: &str) -> Option<&[u8]> {
86        self.documents.get(destination).map(Vec::as_slice)
87    }
88}
89
90/// The structural facts of the target's crate that the seeded Nix
91/// package expression and the seed flake's smoke check rely on.
92#[derive(Debug, Clone, PartialEq, Eq, Default)]
93pub struct CrateShape {
94    /// The text of the target's `Cargo.toml`, or `None` where none reads.
95    pub cargo_toml: Option<String>,
96    /// Whether `Cargo.lock` is a file at the target.
97    pub cargo_lock: bool,
98    /// Whether `src/main.rs` is a file at the target.
99    pub main_rs: bool,
100}
101
102/// The complete candidate artifact tree for one target.
103///
104/// A value, never a record: it carries no operation, no decision, and no
105/// apply state, and it is not serializable. A consumer renders it again
106/// from scratch rather than reading a saved copy.
107#[derive(Debug, Clone, PartialEq, Eq)]
108pub struct Projection {
109    /// Every capability the catalog answered for this target, in catalog
110    /// order: selected, withheld, or omitted with its reason.
111    pub capabilities: Vec<Selection>,
112    /// Every candidate, sorted by destination.
113    pub candidates: Vec<Candidate>,
114    /// The destinations the target's own state withholds, each with its
115    /// one reason and, where one exists, the operator's one edit. A
116    /// destination no selected capability ships is absent, never
117    /// omitted.
118    pub omissions: Vec<Omission>,
119    /// The block destinations whose existing document offers the block no
120    /// place, a target-side defect staging explains and landing refuses.
121    pub collisions: Vec<Collision>,
122    /// Why the recorded code scanning provider's licence condition refuses
123    /// this target, or `None` where no condition applies or the licence
124    /// satisfies it. A landing verb refuses on it and writes nothing; `rk
125    /// status` reports it as a warning and still exits 0, because the
126    /// target is not broken and the operator owns the licensing decision.
127    pub licence_refusal: Option<String>,
128    /// Why a recorded capability cannot run at this target at all, which is a
129    /// receipt nothing can honour rather than a decision anyone made. Empty on
130    /// every target whose parameters came through resolution, because
131    /// resolution refuses these before they are recorded.
132    pub record_defects: Vec<String>,
133}
134
135/// One proposed destination.
136#[derive(Debug, Clone, PartialEq, Eq)]
137pub struct Candidate {
138    /// The capability that lands it.
139    pub capability: &'static str,
140    /// The destination, relative to the target root.
141    pub destination: String,
142    /// Who owns the bytes after landing.
143    pub kind: Kind,
144    /// The whole file, or the one marked region.
145    pub placement: Placement,
146    /// The complete proposed destination bytes. For a region this is the
147    /// complete spliced document, computed from the existing document in
148    /// the evidence, so a staged view equals what production writes.
149    pub bytes: Vec<u8>,
150    /// The rendered block alone for a region destination: what the
151    /// receipt digests. `None` for a whole file.
152    pub region: Option<Vec<u8>>,
153    /// The embedded source paths the candidate was rendered from, each
154    /// carrying its distribution root as the first segment.
155    pub sources: Vec<String>,
156}
157
158/// How a candidate occupies its destination.
159#[derive(Debug, Clone, Copy, PartialEq, Eq)]
160pub enum Placement {
161    /// The candidate is the whole file.
162    Whole,
163    /// The candidate is the one marked region between these markers; the
164    /// bytes outside them belong to the target.
165    Region {
166        /// The opening marker.
167        begin: &'static str,
168        /// The closing marker.
169        end: &'static str,
170    },
171}
172
173/// One destination withheld from this target, with why.
174#[derive(Debug, Clone, PartialEq, Eq)]
175pub struct Omission {
176    /// The destination that stays out.
177    pub destination: String,
178    /// The reason, stated once per destination.
179    pub reason: String,
180    /// The one edit the operator makes to activate what release-kit could
181    /// not, where the omission is a withheld activation rather than a
182    /// shape the target cannot take.
183    pub action: Option<String>,
184}
185
186/// One block destination the target's document cannot take.
187#[derive(Debug, Clone, PartialEq, Eq)]
188pub struct Collision {
189    /// The destination whose document offers the block no place.
190    pub destination: String,
191    /// The reason, for staging to explain and landing to refuse with.
192    pub reason: String,
193}
194
195impl Projection {
196    /// The complete candidate tree for `input`, from this binary's
197    /// embedded sources alone.
198    ///
199    /// # Errors
200    ///
201    /// A source defect in this binary: an unknown technology or an
202    /// unsupported pair as [`RkError::Usage`], and as [`RkError::Other`] a
203    /// destination two sources ship, a snippet the kind table does not
204    /// classify, or a block this binary does not embed.
205    pub fn compute(input: &ProjectionInput) -> Result<Self, RkError> {
206        Self::compute_over(&embedded_snippets(), input)
207    }
208
209    /// [`Self::compute`] over an explicit snippet list, whose paths carry
210    /// the `snippets/` root; the embedded tree in production, an injected
211    /// one under test.
212    #[allow(
213        clippy::too_many_lines,
214        reason = "one pass selects, renders, splices, and withholds; splitting it would separate a withheld destination from the selection that offered it"
215    )]
216    fn compute_over(files: &[(String, &[u8])], input: &ProjectionInput) -> Result<Self, RkError> {
217        let params = &input.params;
218        let evidence = &input.evidence;
219        let availability = Availability::over(
220            files
221                .iter()
222                .filter_map(|(path, _)| path.strip_prefix("snippets/").map(str::to_owned))
223                .collect(),
224        );
225        let mut capabilities = catalog::select(params, &availability);
226        let mut candidates: Vec<Candidate> = Vec::new();
227        for selection in &capabilities {
228            if !selection.lands() {
229                continue;
230            }
231            for source in &selection.sources {
232                let destination = source
233                    .splitn(3, '/')
234                    .nth(2)
235                    .ok_or_else(|| {
236                        anyhow::anyhow!(
237                            "{source}: a snippet path has a zone, a forge, and a destination"
238                        )
239                    })?
240                    .to_owned();
241                let path = format!("snippets/{source}");
242                let bytes = files
243                    .iter()
244                    .find(|(candidate, _)| *candidate == path)
245                    .map(|(_, bytes)| *bytes)
246                    .ok_or_else(|| {
247                        anyhow::anyhow!(
248                            "{path}: the catalog selected a source the tree does not carry"
249                        )
250                    })?;
251                if let Some(existing) = candidates
252                    .iter()
253                    .find(|candidate| candidate.destination == destination)
254                {
255                    return Err(anyhow::anyhow!(
256                        "{} and {} both ship {destination}: {} and {path}; the embedded sources are defective",
257                        existing.capability,
258                        selection.id,
259                        existing.sources.join(", ")
260                    )
261                    .into());
262                }
263                let kind = kind_of(&destination).ok_or_else(|| {
264                    anyhow::anyhow!(
265                        "the embedded sources do not classify {destination}; the kind table is stale"
266                    )
267                })?;
268                let rendered = match kind {
269                    Kind::Rendered => render(bytes, params),
270                    Kind::Seeded | Kind::State => bytes.to_vec(),
271                };
272                candidates.push(Candidate {
273                    capability: selection.id,
274                    destination,
275                    kind,
276                    placement: Placement::Whole,
277                    bytes: rendered,
278                    region: None,
279                    sources: vec![path],
280                });
281            }
282        }
283        let mut collisions = Vec::new();
284        for destination in BLOCK_DESTINATIONS {
285            let (template, sources) = block_template(destination, params.checkout_mode())?;
286            if let Some(whole) = candidates
287                .iter()
288                .find(|candidate| candidate.destination == destination)
289            {
290                return Err(anyhow::anyhow!(
291                    "{destination} is both a whole file from {} and a marked region from {}; the embedded sources are defective",
292                    whole.sources.join(", "),
293                    sources.join(", ")
294                )
295                .into());
296            }
297            let region = render(template.as_bytes(), params);
298            let (begin, end) = block_markers(destination).ok_or_else(|| {
299                anyhow::anyhow!("{destination} is a block destination with no markers")
300            })?;
301            match propose_document(destination, evidence.document(destination), &region) {
302                Ok(bytes) => candidates.push(Candidate {
303                    capability: catalog::GUARDS,
304                    destination: destination.to_owned(),
305                    kind: Kind::Rendered,
306                    placement: Placement::Region { begin, end },
307                    bytes,
308                    region: Some(region),
309                    sources,
310                }),
311                Err(reason) => collisions.push(Collision {
312                    destination: destination.to_owned(),
313                    reason,
314                }),
315            }
316        }
317        let mut omissions = Vec::new();
318        if let Some((set, reason)) = nix_withholding(params.nix_packaging(), evidence)
319            && candidates
320                .iter()
321                .any(|candidate| candidate.capability == catalog::PACKAGING_NIX)
322        {
323            candidates.retain(|candidate| {
324                if set.contains(&candidate.destination.as_str()) {
325                    omissions.push(Omission {
326                        destination: candidate.destination.clone(),
327                        reason: reason.clone(),
328                        action: None,
329                    });
330                    false
331                } else {
332                    true
333                }
334            });
335            withhold(&mut capabilities, catalog::PACKAGING_NIX, &reason, None);
336        }
337        // The release-less GitLab root pipeline: where the target owns a
338        // root pipeline the receipt does not attribute, the fragment still
339        // lands as the gate's prerequisite, and the activation is the
340        // operator's one include line rather than a refusal of the whole
341        // landing.
342        let title_root = candidates.iter().position(|candidate| {
343            candidate.capability == catalog::TITLE_CHECK
344                && candidate.destination == GITLAB_ROOT_PIPELINE
345        });
346        if let Some(index) = title_root
347            && evidence.root_pipeline_present
348            && !evidence.root_pipeline_recorded
349        {
350            let candidate = candidates.remove(index);
351            let reason = format!(
352                "the target owns {GITLAB_ROOT_PIPELINE}, so the release-less root pipeline that would activate the title gate stays out; the fragment lands as the gate's prerequisite"
353            );
354            let action = format!(
355                "add the include to the target's own {GITLAB_ROOT_PIPELINE}: include:\n  - local: {GITLAB_TITLE_FRAGMENT}"
356            );
357            omissions.push(Omission {
358                destination: candidate.destination,
359                reason: reason.clone(),
360                action: Some(action.clone()),
361            });
362            withhold(
363                &mut capabilities,
364                catalog::TITLE_CHECK,
365                &reason,
366                Some(action),
367            );
368        }
369        candidates.sort_by(|a, b| a.destination.cmp(&b.destination));
370        omissions.sort_by(|a, b| a.destination.cmp(&b.destination));
371        // The licence judgment belongs to a workflow this landing will
372        // actually write. A provider the target's dimensions cannot run is
373        // already an unavailable optional capability, reported and omitted,
374        // and refusing it on its terms would refuse a landing over a file
375        // that was never going to land.
376        let scanning_selected = capabilities.iter().any(|selection| {
377            selection.id == catalog::CODE_SCANNING && selection.status == Status::Selected
378        });
379        let licence_refusal = scanning_selected
380            .then(|| {
381                code_scanning_licence_refusal(
382                    params.code_scanning(),
383                    params.driver(),
384                    &evidence.crate_shape,
385                )
386            })
387            .flatten();
388        // A scanner the target's dimensions cannot run is an unavailable
389        // optional capability, which the catalog already reports and this
390        // projection already omits. Naming it a record defect too would
391        // make a landing this binary itself wrote fail its own check.
392        let mut record_defects: Vec<String> = Vec::new();
393        // A recorded automatic release whose automation this release cannot
394        // land is a receipt nothing can honour: named here, so a
395        // record-only reader sees it.
396        if params.release_mode() == ReleaseMode::Automatic
397            && let Some(selection) = capabilities
398                .iter()
399                .find(|selection| selection.id == catalog::RELEASE_AUTOMATION)
400            && matches!(selection.status, Status::Unavailable | Status::Unknown)
401            && let Some(reason) = &selection.reason
402        {
403            record_defects.push(reason.clone());
404        }
405        Ok(Self {
406            capabilities,
407            candidates,
408            omissions,
409            collisions,
410            licence_refusal,
411            record_defects,
412        })
413    }
414
415    /// Why the selected release automation cannot land, where the profile
416    /// asked for one this release does not carry at its dimensions. An
417    /// apply refuses on it before any write; a preview reports it.
418    #[must_use]
419    pub fn release_unavailable(&self) -> Option<&str> {
420        self.capabilities
421            .iter()
422            .find(|selection| {
423                selection.id == catalog::RELEASE_AUTOMATION
424                    && matches!(selection.status, Status::Unavailable | Status::Unknown)
425            })
426            .and_then(|selection| selection.reason.as_deref())
427    }
428
429    /// The one selection for a capability.
430    #[must_use]
431    pub fn capability(&self, id: &str) -> Option<&Selection> {
432        self.capabilities
433            .iter()
434            .find(|selection| selection.id == id)
435    }
436}
437
438/// Mark one capability withheld with its reason and the operator's edit.
439fn withhold(capabilities: &mut [Selection], id: &str, reason: &str, action: Option<String>) {
440    if let Some(selection) = capabilities.iter_mut().find(|selection| selection.id == id) {
441        selection.status = Status::Withheld;
442        selection.reason = Some(reason.to_owned());
443        selection.action = action;
444    }
445}
446
447/// The GitLab root pipeline, the one destination a release-less landing
448/// withholds where the target owns it.
449pub const GITLAB_ROOT_PIPELINE: &str = ".gitlab-ci.yml";
450
451/// The GitLab title fragment the root pipeline includes.
452pub const GITLAB_TITLE_FRAGMENT: &str = ".gitlab/ci/mr-title.yml";
453
454/// Every snippet this binary embeds, as `(path, bytes)` with the path
455/// carrying the `snippets/` root, sorted by path.
456fn embedded_snippets() -> Vec<(String, &'static [u8])> {
457    embedded::walk(&embedded::SNIPPETS)
458        .into_iter()
459        .map(|(path, bytes)| (format!("snippets/{path}"), bytes))
460        .collect()
461}
462
463/// Every `(driver, forge)` pair at which the release automation is
464/// available, in path order.
465#[must_use]
466pub fn supported_pairs() -> Vec<(String, String)> {
467    Availability::embedded()
468        .automation_tuples()
469        .into_iter()
470        .filter_map(|entry| {
471            entry
472                .split_once(", ")
473                .map(|(driver, forge)| (driver.to_owned(), forge.to_owned()))
474        })
475        .collect()
476}
477
478/// Who owns a landed file's bytes after landing.
479#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
480#[serde(rename_all = "lowercase")]
481pub enum Kind {
482    /// release-kit owns it: a newer binary re-renders it, and a target
483    /// edit is a conflict.
484    Rendered,
485    /// The target owns it: a starting point the project tunes, reported
486    /// and never rewritten.
487    Seeded,
488    /// The release automation owns it: never written after the first
489    /// landing, never compared.
490    State,
491}
492
493impl Kind {
494    /// The wire and report form.
495    #[must_use]
496    pub const fn as_str(self) -> &'static str {
497        match self {
498            Self::Rendered => "rendered",
499            Self::Seeded => "seeded",
500            Self::State => "state",
501        }
502    }
503}
504
505/// The declared classification: every landable destination and its kind.
506/// The workflow and pipeline files carry the release automation and the
507/// OIDC permission, so release-kit owns them; the tool configurations are
508/// per-project judgment; the two state files are rewritten by the release
509/// automation itself.
510const KINDS: [(&str, Kind); 20] = [
511    (".github/workflows/release-plz.yml", Kind::Rendered),
512    (".github/workflows/release-please.yml", Kind::Rendered),
513    (".github/workflows/release.yml", Kind::Rendered),
514    (".github/workflows/pr-title.yml", Kind::Rendered),
515    (".github/workflows/scorecard.yml", Kind::Rendered),
516    (".github/workflows/code-scanning-codeql.yml", Kind::Rendered),
517    (
518        ".github/workflows/code-scanning-semgrep.yml",
519        Kind::Rendered,
520    ),
521    (".gitlab/ci/code-scanning-semgrep.yml", Kind::Rendered),
522    (".gitlab-ci.yml", Kind::Rendered),
523    ("SECURITY.md", Kind::Rendered),
524    (".gitlab/ci/mr-title.yml", Kind::Rendered),
525    ("release-plz.toml", Kind::Seeded),
526    ("dist-workspace.toml", Kind::Seeded),
527    ("release-please-config.json", Kind::Seeded),
528    ("cliff.toml", Kind::Seeded),
529    ("nix/package.nix", Kind::Seeded),
530    ("flake.nix", Kind::Seeded),
531    (".release-please-manifest.json", Kind::State),
532    ("VERSION", Kind::State),
533    ("flake.lock", Kind::State),
534];
535
536/// The destinations of the opt-in Nix capability, present in a projection
537/// only where the landing's `nix` parameter is on.
538///
539/// The parameter is recorded, so `status`, `upgrade`, and `adopt` can
540/// reconstruct whether these files are supposed to exist: an absent file
541/// under `nix = false` is not wanted, never drifted.
542///
543/// The capability lands no workflow, on either forge, and each forge's
544/// reason is its own. On GitHub a job gates the merge only inside the
545/// workflow the required check needs, and that workflow is the target's
546/// own. On GitLab the merge check is the whole pipeline, and a target's
547/// jobs live in the child pipeline the rendered parent triggers, which the
548/// target owns. The bindings serve the job for both.
549pub const NIX_DESTINATIONS: [&str; 3] = ["nix/package.nix", "flake.nix", "flake.lock"];
550
551/// The subset a target with a flake of its own keeps out: the seed pair,
552/// whose files would sit beside a flake release-kit did not author.
553///
554/// The seeded package expression is not in it: it lands either way, as
555/// the starting point the target integrates by hand.
556pub const NIX_WITHHOLDABLE: [&str; 2] = ["flake.nix", "flake.lock"];
557
558/// The destinations of the opt-in Scorecard capability, present in a
559/// projection only where the landing's `scorecard` parameter is on.
560///
561/// The parameter is recorded for the reason [`NIX_DESTINATIONS`] states:
562/// an absent file under `scorecard = false` is not wanted, never drifted.
563/// The capability is GitHub's alone, so the shared GitLab zone ships no
564/// counterpart and a GitLab landing projects nothing for it, whatever the
565/// parameter says. A Scorecard run needs no forge setting on a public
566/// repository, so the capability adds no setup step and cannot conflict
567/// with `forge-setup:every-supported-forge-runs-every-step`.
568///
569/// The file is `rendered`: its bytes carry the recorded trunk and nothing a
570/// target is expected to tune, so release-kit owns them and an edit is
571/// drift.
572pub const SCORECARD_DESTINATIONS: [&str; 1] = [".github/workflows/scorecard.yml"];
573
574/// Every destination the opt-in code scanning capability can land, against
575/// the provider that lands it.
576///
577/// One source owns one destination, so the provider is in the name: a
578/// landing writes exactly the entry its recorded provider names and the
579/// forge ships. Switching provider retires one destination and adds
580/// another, which `landing:a-dropped-file-stays` already answers.
581///
582/// `codeql` is GitHub's own analyzer and has no GitLab entry, so a GitLab
583/// landing that names it lands nothing; the catalog reports that pair
584/// unavailable by name, as it reports a binding outside
585/// [`CODE_SCANNING_TECHS`], and the landing omits the destination and
586/// proceeds.
587pub const CODE_SCANNING_DESTINATIONS: [(&str, Provider); 3] = [
588    (
589        ".github/workflows/code-scanning-codeql.yml",
590        Provider::CodeQl,
591    ),
592    (
593        ".github/workflows/code-scanning-semgrep.yml",
594        Provider::Semgrep,
595    ),
596    (".gitlab/ci/code-scanning-semgrep.yml", Provider::Semgrep),
597];
598
599/// The bindings that ship a code scanning workflow.
600///
601/// A scanner reads one language: the `CodeQL` arm fixes `languages: rust` and
602/// both Semgrep arms name the `p/rust` ruleset, so the sources live in the
603/// rust pairs rather than in a forge's technology-independent shared zone.
604/// A landing for any other binding reports the capability unavailable by
605/// name and omits its destinations, rather than writing nothing silently.
606pub const CODE_SCANNING_TECHS: [&str; 1] = ["rust"];
607
608/// Why the named code scanning provider cannot run at this
609/// `(technology, forge)`, or `None` where it can or none is named.
610///
611/// One owner for the question, because two callers ask it and they must not
612/// disagree. The reason is informational: it becomes the catalog's
613/// `Unavailable` status, and nothing refuses on it. An optional capability
614/// this release cannot build at the target's dimensions is reported and
615/// omitted, which is what
616/// `project-profile:an-operation-refuses-only-what-it-requires` states.
617///
618/// A receipt asks the same question and gets the same answer, because
619/// `Params::from_record` cannot fail and a hand-edited or foreign receipt
620/// reaches `rk status` through it: without this, a receipt naming a provider
621/// whose pair ships nothing would project nothing, name nothing, and read as
622/// clean. It is a report about the pair, not a record defect, so a landing
623/// this binary wrote still answers its own check.
624#[must_use]
625pub fn code_scanning_incompatibility(
626    provider: Option<Provider>,
627    driver: Option<&str>,
628    forge: Option<&str>,
629) -> Option<String> {
630    let named = provider?;
631    let Some(driver) = driver else {
632        return Some(format!(
633            "a scanner reads the release driver's language, and the profile names no automatic release driver; the bindings that carry one are: {}",
634            CODE_SCANNING_TECHS.join(", ")
635        ));
636    };
637    if !CODE_SCANNING_TECHS.contains(&driver) {
638        return Some(format!(
639            "the {driver} binding ships no code scanning workflow; a scanner reads one language, and the bindings that carry one are: {}",
640            CODE_SCANNING_TECHS.join(", ")
641        ));
642    }
643    let Some(forge) = forge else {
644        return Some(
645            "a code scanning workflow runs at a forge, and the profile names none".to_owned(),
646        );
647    };
648    if named == Provider::CodeQl && forge != "github" {
649        return Some(format!(
650            "codeql is GitHub's own analyzer and the {forge} pair ships no workflow for it; pass --code-scanning semgrep"
651        ));
652    }
653    None
654}
655
656/// The SPDX identifiers this convention recognizes as OSI-approved, sorted.
657///
658/// Lowercase, because the comparison is case-insensitive: SPDX asks for that
659/// and cargo accepts any casing.
660///
661/// A closed list rather than a parse of the OSI register: the register moves
662/// and this binary reads no network, so an identifier absent here is
663/// unrecognized rather than rejected, and the refusal says so. Every entry
664/// is an OSI-approved licence that appears on published Rust crates.
665const OSI_APPROVED: [&str; 18] = [
666    "0bsd",
667    "agpl-3.0",
668    "agpl-3.0-only",
669    "agpl-3.0-or-later",
670    "apache-2.0",
671    "bsd-2-clause",
672    "bsd-3-clause",
673    "bsl-1.0",
674    "epl-2.0",
675    "gpl-2.0",
676    "gpl-2.0-only",
677    "gpl-2.0-or-later",
678    "gpl-3.0",
679    "gpl-3.0-only",
680    "gpl-3.0-or-later",
681    "isc",
682    "mit",
683    "mpl-2.0",
684];
685
686/// Every SPDX exception identifier, lowercase.
687///
688/// SPDX requires the right operand of `WITH` to be a `<license-exception-id>`,
689/// so an operand outside this list makes the expression malformed and the
690/// licence unread. The whole register rather than a subset, because a subset
691/// refuses a legitimate crate: `GPL-2.0-only WITH GCC-exception-2.0` names a
692/// real exception, and a reader carrying only the newer `GCC-exception-3.1`
693/// would refuse it.
694///
695/// An exception grants permission rather than withdrawing it, so none of these
696/// changes whether the left operand is OSI-approved. It is validated because a
697/// reader that cannot parse the expression has not read the licence.
698///
699/// Taken from the SPDX license-list-data exception register, 86 identifiers, on
700/// 2026-09-15. A later addition upstream is a patch here, and the refusal names
701/// the operand it did not recognize.
702const SPDX_EXCEPTIONS: [&str; 86] = [
703    "389-exception",
704    "asterisk-exception",
705    "asterisk-linking-protocols-exception",
706    "autoconf-exception-2.0",
707    "autoconf-exception-3.0",
708    "autoconf-exception-generic",
709    "autoconf-exception-generic-3.0",
710    "autoconf-exception-macro",
711    "bison-exception-1.24",
712    "bison-exception-2.2",
713    "bootloader-exception",
714    "cgal-linking-exception",
715    "classpath-exception-2.0",
716    "classpath-exception-2.0-short",
717    "clisp-exception-2.0",
718    "cryptsetup-openssl-exception",
719    "digia-qt-lgpl-exception-1.1",
720    "digirule-foss-exception",
721    "ecos-exception-2.0",
722    "erlang-otp-linking-exception",
723    "fawkes-runtime-exception",
724    "fltk-exception",
725    "fmt-exception",
726    "font-exception-2.0",
727    "freertos-exception-2.0",
728    "gcc-exception-2.0",
729    "gcc-exception-2.0-note",
730    "gcc-exception-3.1",
731    "gmsh-exception",
732    "gnat-exception",
733    "gnome-examples-exception",
734    "gnu-compiler-exception",
735    "gnu-javamail-exception",
736    "google-patent-webm",
737    "gpl-3.0-389-ds-base-exception",
738    "gpl-3.0-interface-exception",
739    "gpl-3.0-linking-exception",
740    "gpl-3.0-linking-source-exception",
741    "gpl-cc-1.0",
742    "gstreamer-exception-2005",
743    "gstreamer-exception-2008",
744    "harbour-exception",
745    "i2p-gpl-java-exception",
746    "independent-modules-exception",
747    "kicad-libraries-exception",
748    "kvirc-openssl-exception",
749    "lgpl-3.0-linking-exception",
750    "libpri-openh323-exception",
751    "libtool-exception",
752    "linux-syscall-note",
753    "llgpl",
754    "llvm-exception",
755    "lzma-exception",
756    "mif-exception",
757    "mxml-exception",
758    "nokia-qt-exception-1.1",
759    "ocaml-lgpl-linking-exception",
760    "occt-exception-1.0",
761    "openjdk-assembly-exception-1.0",
762    "openvpn-openssl-exception",
763    "pcre2-exception",
764    "polyparse-exception",
765    "ps-or-pdf-font-exception-20170817",
766    "qpl-1.0-inria-2004-exception",
767    "qt-gpl-exception-1.0",
768    "qt-lgpl-exception-1.1",
769    "qwt-exception-1.0",
770    "romic-exception",
771    "rrdtool-floss-exception-2.0",
772    "rsync-linking-exception",
773    "sane-exception",
774    "shl-2.0",
775    "shl-2.1",
776    "simple-library-usage-exception",
777    "spelling-provider-lgpl-exception",
778    "sqlitestudio-openssl-exception",
779    "stunnel-exception",
780    "swi-exception",
781    "swift-exception",
782    "texinfo-exception",
783    "u-boot-exception-2.0",
784    "ubdl-exception",
785    "universal-foss-exception-1.0",
786    "vsftpd-openssl-exception",
787    "wxwindows-exception-3.1",
788    "x11vnc-openssl-exception",
789];
790
791/// Whether `identifier` is in `list`, matched the way SPDX asks for it.
792///
793/// Without regard to case: SPDX states that an identifier "should be matched
794/// in a case-insensitive manner", and cargo accepts `license = "mit"` without
795/// complaint, so a real crate can carry any casing and a case-sensitive
796/// comparison would refuse a licence it recognizes.
797fn listed(list: &[&str], identifier: &str) -> bool {
798    let lowered = identifier.to_ascii_lowercase();
799    list.contains(&lowered.as_str())
800}
801
802/// One token of an SPDX licence expression.
803#[derive(Debug, Clone, Copy, PartialEq, Eq)]
804enum Token<'a> {
805    /// An opening parenthesis.
806    Open,
807    /// A closing parenthesis.
808    Close,
809    /// The conjunction: the codebase is offered under both terms at once.
810    And,
811    /// The disjunction: the reader chooses one term.
812    Or,
813    /// The exception operator, whose right side names an exception rather
814    /// than a licence.
815    With,
816    /// A licence identifier, a licence reference, or an exception
817    /// identifier. Which one it is depends on its position.
818    Identifier(&'a str),
819}
820
821/// The tokens of one SPDX expression, or `None` where a character can begin
822/// no token.
823///
824/// The identifier alphabet is SPDX's own plus `:`, which a
825/// `DocumentRef-...:LicenseRef-...` reference carries. A reference
826/// tokenizes and then simply matches no approved identifier, which is the
827/// honest answer: it names a licence whose text lives outside the register.
828fn tokenize(expression: &str) -> Option<Vec<Token<'_>>> {
829    let mut tokens = Vec::new();
830    let bytes = expression.as_bytes();
831    let mut at = 0;
832    while at < bytes.len() {
833        let byte = bytes[at];
834        if byte.is_ascii_whitespace() {
835            at += 1;
836            continue;
837        }
838        if byte == b'(' {
839            tokens.push(Token::Open);
840            at += 1;
841            continue;
842        }
843        if byte == b')' {
844            tokens.push(Token::Close);
845            at += 1;
846            continue;
847        }
848        let start = at;
849        while at < bytes.len() {
850            let byte = bytes[at];
851            if byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'-' | b'+' | b':') {
852                at += 1;
853            } else {
854                break;
855            }
856        }
857        if at == start {
858            return None;
859        }
860        let word = &expression[start..at];
861        tokens.push(match word {
862            "AND" => Token::And,
863            "OR" => Token::Or,
864            "WITH" => Token::With,
865            other => Token::Identifier(other),
866        });
867    }
868    Some(tokens)
869}
870
871/// A recursive-descent reader over one tokenized SPDX expression.
872///
873/// It answers two questions in one pass, and both must hold: whether the
874/// expression is well formed, and whether every licence identifier in it is
875/// OSI-approved. A malformed expression answers `None` rather than falling
876/// back on the identifiers it happened to contain, because an expression
877/// nobody can parse states no terms at all.
878struct Spdx<'a> {
879    tokens: &'a [Token<'a>],
880    at: usize,
881}
882
883impl<'a> Spdx<'a> {
884    /// The token at the cursor, without consuming it.
885    fn peek(&self) -> Option<Token<'a>> {
886        self.tokens.get(self.at).copied()
887    }
888
889    /// The token at the cursor, consumed.
890    fn bump(&mut self) -> Option<Token<'a>> {
891        let token = self.peek()?;
892        self.at += 1;
893        Some(token)
894    }
895
896    /// One expression: operands joined by `AND` and `OR`, each operand a
897    /// parenthesized expression or a simple licence.
898    ///
899    /// Both operators require every operand to be approved. A disjunction
900    /// offers the reader a choice, so one unapproved term is a term the
901    /// reader may take.
902    fn expression(&mut self) -> Option<bool> {
903        let mut approved = self.operand()?;
904        while matches!(self.peek(), Some(Token::And | Token::Or)) {
905            self.bump();
906            approved = self.operand()? && approved;
907        }
908        Some(approved)
909    }
910
911    /// One operand: a parenthesized expression, or a licence identifier
912    /// optionally carrying `WITH` and an exception identifier.
913    ///
914    /// A `+` suffix reads as the bare identifier, which is what the
915    /// deprecated `GPL-3.0+` form means. The operand after `WITH` must be an
916    /// exception identifier this release recognizes, and it changes no
917    /// verdict: an exception grants permission rather than withdrawing it, so
918    /// which licence the codebase is offered under is answered by the left
919    /// operand alone.
920    fn operand(&mut self) -> Option<bool> {
921        match self.bump()? {
922            Token::Open => {
923                let inner = self.expression()?;
924                (self.bump()? == Token::Close).then_some(inner)
925            }
926            Token::Identifier(name) => {
927                let identifier = name.strip_suffix('+').unwrap_or(name);
928                let approved = listed(&OSI_APPROVED, identifier);
929                if self.peek() == Some(Token::With) {
930                    self.bump();
931                    // SPDX requires an exception identifier here, so an
932                    // operand this release does not recognize leaves the
933                    // expression malformed and the licence unread.
934                    match self.bump()? {
935                        Token::Identifier(exception) if listed(&SPDX_EXCEPTIONS, exception) => {}
936                        _ => return None,
937                    }
938                }
939                Some(approved)
940            }
941            Token::And | Token::Or | Token::With | Token::Close => None,
942        }
943    }
944}
945
946/// Whether one SPDX expression is well formed and every licence in it is
947/// OSI-approved.
948///
949/// Both conditions, and the pair is the point. A malformed expression is
950/// refused rather than read for the identifiers it contains, because
951/// `MIT OR` names one licence and no complete offer, and accepting it would
952/// land a workflow whose provider's terms nothing established.
953#[must_use]
954pub fn licence_is_osi_approved(expression: &str) -> bool {
955    let Some(tokens) = tokenize(expression) else {
956        return false;
957    };
958    let mut reader = Spdx {
959        tokens: &tokens,
960        at: 0,
961    };
962    let Some(approved) = reader.expression() else {
963        return false;
964    };
965    approved && reader.at == tokens.len()
966}
967
968/// Why the code scanning capability's licence condition refuses this
969/// target, or `None` where no condition applies or the licence satisfies
970/// it.
971///
972/// Only `codeql` carries a condition: its terms cover an open-source
973/// codebase, and a run over anything else needs a paid seat, so a landing
974/// that guessed would write a licence violation. Semgrep CE carries none,
975/// and is the fallback the refusal names.
976///
977/// The licence is read where the binding declares it. For `rust` that is
978/// the `license` field of `Cargo.toml`. A manifest that names a
979/// `license-file` instead states no identifier this judgment can read, so
980/// the pair refuses rather than guessing at the file's contents.
981#[must_use]
982pub fn code_scanning_licence_refusal(
983    provider: Option<Provider>,
984    driver: Option<&str>,
985    shape: &CrateShape,
986) -> Option<String> {
987    if provider != Some(Provider::CodeQl) {
988        return None;
989    }
990    if driver != Some("rust") {
991        return Some(format!(
992            "the {} binding declares no licence field this release reads, and codeql's terms cover an open-source codebase alone; land --code-scanning semgrep, which carries no licence condition",
993            driver.unwrap_or("release-less")
994        ));
995    }
996    let fallback = "land --code-scanning semgrep, which carries no licence condition";
997    let Some(text) = shape.cargo_toml.as_deref() else {
998        return Some(format!(
999            "the target has no readable Cargo.toml, so the licence codeql's terms depend on cannot be read; {fallback}"
1000        ));
1001    };
1002    let Ok(table) = text.parse::<toml::Table>() else {
1003        return Some(format!(
1004            "the target's Cargo.toml does not parse, so the licence codeql's terms depend on cannot be read; {fallback}"
1005        ));
1006    };
1007    let licence = table
1008        .get("package")
1009        .and_then(toml::Value::as_table)
1010        .and_then(|package| package.get("license"))
1011        .and_then(toml::Value::as_str);
1012    match licence {
1013        None => Some(format!(
1014            "the target's Cargo.toml declares no license field, and codeql's terms cover an open-source codebase alone; declare one, or {fallback}"
1015        )),
1016        Some(expression) if !licence_is_osi_approved(expression) => Some(format!(
1017            "the target's license, {expression}, is not one this release recognizes as OSI-approved, and codeql's terms cover an open-source codebase alone; {fallback}"
1018        )),
1019        Some(_) => None,
1020    }
1021}
1022
1023/// The declared kind of a destination, or `None` for a file the sources
1024/// does not classify.
1025#[must_use]
1026pub fn kind_of(destination: &str) -> Option<Kind> {
1027    if BLOCK_DESTINATIONS.contains(&destination) {
1028        return Some(Kind::Rendered);
1029    }
1030    KINDS
1031        .iter()
1032        .find(|(name, _)| *name == destination)
1033        .map(|(_, kind)| *kind)
1034}
1035
1036/// Every destination the embedded sources can land, in declaration order.
1037///
1038/// The whole files and the three block destinations. The classification
1039/// reads it to ask whether a destination is already present at a target.
1040pub fn destinations() -> impl Iterator<Item = &'static str> {
1041    KINDS
1042        .iter()
1043        .map(|(name, _)| *name)
1044        .chain(BLOCK_DESTINATIONS)
1045}
1046
1047/// The mechanical substitution sites in `rendered` files.
1048///
1049/// Known values, substituted identically everywhere each appears. The
1050/// owner is derived from the landing's `repo` parameter and the scope
1051/// shape from [`SCOPE_SHAPE`], so the landed bytes stay a deterministic
1052/// function of the embedded sources plus parameters.
1053pub const OWNER_TOKEN: &[u8] = b"OWNER";
1054
1055/// The repository a preview stands in for where nothing answered.
1056///
1057/// It is a placeholder, never a project path: a plan that would render
1058/// it into a target is blocked, and only a preview may carry it.
1059pub const REPO_PLACEHOLDER: &str = "OWNER";
1060
1061/// The full recorded project path, including nested namespaces.
1062pub const REPO_TOKEN: &[u8] = b"RK_REPO";
1063
1064/// The one scope shape: the title checks' regular expression.
1065pub const SCOPE_SHAPE_TOKEN: &[u8] = b"RK_SCOPE_SHAPE";
1066
1067/// The recorded release style: `trunk` arms the bot's request in the
1068/// landed release workflow, `lines` leaves every request unarmed.
1069pub const STYLE_TOKEN: &[u8] = b"RK_STYLE";
1070
1071/// The one permanent branch. A landed release trigger, ref guard, and
1072/// branch guard each name it, so a target whose trunk is not `master`
1073/// needs its own answer in its own bytes.
1074pub const TRUNK_BRANCH_TOKEN: &[u8] = b"RK_TRUNK_BRANCH";
1075
1076/// The release-line branch prefix, naming the lines a release trigger
1077/// accepts beside the trunk.
1078pub const LINE_PREFIX_TOKEN: &[u8] = b"RK_LINE_PREFIX";
1079
1080/// The same prefix, escaped for a slash-delimited regular expression.
1081///
1082/// A GitLab rule names a line that way, and a raw `release/` would close
1083/// the delimiter and break the pipeline, so the two forms are two tokens.
1084/// This one substitutes first: the plain token is its own prefix.
1085pub const LINE_PREFIX_RE_TOKEN: &[u8] = b"RK_LINE_PREFIX_RE";
1086
1087/// The three replaceable spans of a landed security policy, each as its
1088/// ordered begin and end marker.
1089///
1090/// A span is not a token. Each forge's policy carries its own authored
1091/// prose inside the markers, so a landing that answers neither security
1092/// parameter strips the markers and reproduces the file the forge's
1093/// snippet states, byte for byte and in that forge's own words. A landing
1094/// that answers one replaces the interior of the spans that fact belongs
1095/// to. The markers are HTML comments because the snippet is Markdown a
1096/// reader may open before it is ever rendered.
1097pub const SECURITY_SPANS: [(&[u8], &[u8]); 3] = [
1098    (
1099        b"<!--RK_SECURITY_CONTACT_BEGIN-->",
1100        b"<!--RK_SECURITY_CONTACT_END-->",
1101    ),
1102    (
1103        b"<!--RK_SECURITY_RESPONSE_BEGIN-->",
1104        b"<!--RK_SECURITY_RESPONSE_END-->",
1105    ),
1106    (
1107        b"<!--RK_SECURITY_DEADLINE_BEGIN-->",
1108        b"<!--RK_SECURITY_DEADLINE_END-->",
1109    ),
1110];
1111
1112/// The sentence a policy with an acknowledgment window states in place of
1113/// the forge's best-effort wording.
1114fn acknowledgment(response: &str) -> String {
1115    format!("Maintainers acknowledge a report within {response}.")
1116}
1117
1118/// What a policy with an acknowledgment window says about deadlines: the
1119/// authored sentence disclaims a response deadline, which a stated window
1120/// contradicts, so only the disclosure half survives.
1121const DISCLOSURE_ONLY: &[u8] = b"This policy commits to no disclosure deadline.";
1122
1123/// The replacement for each span under one parameter set, or `None` where
1124/// the forge's authored interior stands.
1125fn security_replacements(params: &Params) -> [Option<Vec<u8>>; 3] {
1126    let contact = (!params.security_contact().is_empty())
1127        .then(|| params.security_contact().as_bytes().to_vec());
1128    let promised = params.security_response() != crate::config::RESPONSE_DEFAULT;
1129    [
1130        contact,
1131        promised.then(|| acknowledgment(params.security_response()).into_bytes()),
1132        promised.then(|| DISCLOSURE_ONLY.to_vec()),
1133    ]
1134}
1135
1136/// One marked span replaced, or the markers alone removed.
1137///
1138/// Exactly one ordered begin and end pair is a span; anything else is a
1139/// source defect a test holds, so this leaves such bytes untouched rather
1140/// than growing a runtime failure mode into every rendered file.
1141fn replace_span(baseline: &[u8], begin: &[u8], end: &[u8], value: Option<&[u8]>) -> Vec<u8> {
1142    let ordered = find(baseline, begin)
1143        .zip(find(baseline, end))
1144        .filter(|(start, stop)| stop > start);
1145    let Some((start, stop)) = ordered else {
1146        return baseline.to_vec();
1147    };
1148    let mut out = Vec::with_capacity(baseline.len());
1149    out.extend_from_slice(&baseline[..start]);
1150    out.extend_from_slice(value.unwrap_or_else(|| &baseline[start + begin.len()..stop]));
1151    out.extend_from_slice(&baseline[stop + end.len()..]);
1152    out
1153}
1154
1155/// Substitute the landing parameters into a `rendered` file's bytes.
1156///
1157/// The repository's owner, the project path's first segment, replaces
1158/// every `OWNER` occurrence; the full path replaces `RK_REPO` last. The
1159/// one scope shape replaces the scope token, and the recorded style
1160/// replaces the style token. The scope shape rests on no parameter, so it
1161/// substitutes always. An unresolved style leaves its token standing,
1162/// which only a preview renders under: an apply refuses before reaching
1163/// here.
1164///
1165/// The trunk and the line prefix substitute from the same parameters, so
1166/// a target that renames either carries the new name in every artifact
1167/// that names it rather than in the binary's behavior alone.
1168///
1169/// The security policy's marked spans resolve last, after every token, so
1170/// a contact that happens to spell a token name lands literally rather
1171/// than being read as one more substitution site.
1172#[must_use]
1173pub fn render(baseline: &[u8], params: &Params) -> Vec<u8> {
1174    let repo = params.repo();
1175    let owner = repo.split('/').next().unwrap_or(repo);
1176    let mut out = substitute(baseline, OWNER_TOKEN, owner.as_bytes());
1177    if let Some(style) = params.style() {
1178        out = substitute(&out, STYLE_TOKEN, style.as_str().as_bytes());
1179    }
1180    out = substitute(&out, SCOPE_SHAPE_TOKEN, SCOPE_SHAPE.as_bytes());
1181    out = substitute(&out, TRUNK_BRANCH_TOKEN, params.trunk().as_bytes());
1182    let escaped = params.line_prefix().replace('/', "\\/");
1183    out = substitute(&out, LINE_PREFIX_RE_TOKEN, escaped.as_bytes());
1184    out = substitute(&out, LINE_PREFIX_TOKEN, params.line_prefix().as_bytes());
1185    out = substitute(&out, REPO_TOKEN, repo.as_bytes());
1186    for ((begin, end), value) in SECURITY_SPANS.iter().zip(security_replacements(params)) {
1187        out = replace_span(&out, begin, end, value.as_deref());
1188    }
1189    out
1190}
1191
1192/// Every `token` occurrence replaced with `value`.
1193#[must_use]
1194pub fn substitute(baseline: &[u8], token: &[u8], value: &[u8]) -> Vec<u8> {
1195    let mut out = Vec::with_capacity(baseline.len());
1196    let mut rest = baseline;
1197    while let Some(at) = find(rest, token) {
1198        out.extend_from_slice(&rest[..at]);
1199        out.extend_from_slice(value);
1200        rest = &rest[at + token.len()..];
1201    }
1202    out.extend_from_slice(rest);
1203    out
1204}
1205
1206/// First occurrence of `needle` in `haystack`.
1207fn find(haystack: &[u8], needle: &[u8]) -> Option<usize> {
1208    haystack
1209        .windows(needle.len())
1210        .position(|window| window == needle)
1211}
1212
1213/// The destination the routing block splices into.
1214pub const AGENTS_DESTINATION: &str = "AGENTS.md";
1215
1216/// The block's opening marker.
1217pub const BLOCK_BEGIN: &str = "<!-- BEGIN release-kit -->";
1218
1219/// The block's closing marker.
1220pub const BLOCK_END: &str = "<!-- END release-kit -->";
1221
1222/// The destination the glossary block splices into.
1223///
1224/// The document is the target's own vocabulary, so the block shares
1225/// `AGENTS.md`'s marker pair and owns nothing outside it.
1226pub const GLOSSARY_DESTINATION: &str = "GLOSSARY.md";
1227
1228/// The destination the hook block splices into.
1229pub const HOOKS_DESTINATION: &str = ".pre-commit-config.yaml";
1230
1231/// Every block destination, in the order a landing writes them.
1232///
1233/// A block destination owns the lines between its markers and nothing
1234/// else, so every verb that asks whether a destination is block-placed
1235/// reads this one list.
1236pub const BLOCK_DESTINATIONS: [&str; 3] =
1237    [AGENTS_DESTINATION, GLOSSARY_DESTINATION, HOOKS_DESTINATION];
1238
1239/// The hook block's opening marker, a YAML comment at column zero.
1240pub const HOOKS_BEGIN: &str = "# BEGIN release-kit";
1241
1242/// The hook block's closing marker.
1243pub const HOOKS_END: &str = "# END release-kit";
1244
1245/// The top-level key the fresh hook file carries and the skills verify on
1246/// an existing one: the commit-msg and pre-push hooks run only where their
1247/// hook types are installed.
1248pub const HOOK_TYPES_LINE: &str = "default_install_hook_types: [pre-commit, commit-msg, pre-push]";
1249
1250/// The authored routing-block template.
1251pub const AGENTS_BLOCK: &str = "blocks/agents-block.md.in";
1252
1253/// The authored glossary template.
1254pub const GLOSSARY_BLOCK: &str = "blocks/glossary.md.in";
1255
1256/// The routing block's mode line, worktree form.
1257pub const AGENTS_LINE_WORKTREE: &str = "blocks/agents-line-worktree.md.in";
1258
1259/// The routing block's mode line, branches form.
1260pub const AGENTS_LINE_BRANCHES: &str = "blocks/agents-line-branches.md.in";
1261
1262/// The authored hook-block template.
1263pub const PRE_COMMIT_BLOCK: &str = "blocks/pre-commit-block.yaml.in";
1264
1265/// The worktree mode's guard entry.
1266pub const PRE_COMMIT_WORKTREE_GUARD: &str = "blocks/pre-commit-worktree-guard.yaml.in";
1267
1268/// The routing block's mode line for one checkout mode.
1269#[must_use]
1270pub const fn routing_line(mode: CheckoutMode) -> &'static str {
1271    match mode {
1272        CheckoutMode::LinkedWorktree => AGENTS_LINE_WORKTREE,
1273        CheckoutMode::MainWorktree => AGENTS_LINE_BRANCHES,
1274    }
1275}
1276
1277/// One authored block this binary embeds, as text, by its embedded path.
1278///
1279/// # Errors
1280///
1281/// [`RkError::Other`] for a block this binary does not embed or one that
1282/// is not UTF-8, both defects in the binary.
1283pub fn embedded_block(path: &str) -> Result<&'static str, RkError> {
1284    let name = path.strip_prefix("blocks/").unwrap_or(path);
1285    let file = embedded::BLOCKS
1286        .get_file(name)
1287        .ok_or_else(|| anyhow::anyhow!("{path}: this binary embeds no such block"))?;
1288    std::str::from_utf8(file.contents())
1289        .map_err(|_| anyhow::anyhow!("{path}: a block is UTF-8").into())
1290}
1291
1292/// An authored block without the one final newline the repository's
1293/// hooks enforce on every file under `blocks/`; a test in
1294/// `src/embedded.rs` holds each file to exactly one.
1295#[must_use]
1296pub fn authored(text: &str) -> &str {
1297    text.strip_suffix('\n').unwrap_or(text)
1298}
1299
1300/// The one branch grammar.
1301///
1302/// The extended regular expression the landed `rk-branch-name` hook
1303/// tests, and the same anchored language `rk worktree add` validates
1304/// before creating anything. One owner by token: `concat!` cannot
1305/// interpolate a const, so [`compose_hooks`] substitutes it for the
1306/// template's `RK_BRANCH_GRAMMAR` token.
1307pub const BRANCH_GRAMMAR: &str = r"^((build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)/[A-Za-z0-9._/-]+|([0-9]+|[A-Z][A-Z0-9]+-[0-9]+)-[A-Za-z0-9._-]+|release[-/].+)$";
1308
1309/// The one commit scope shape.
1310///
1311/// A bracket expression, lowercase, admitting the digits and `_ . / -`
1312/// beside the letters, so `area/subarea` reads as one scope. It holds the
1313/// shape of a scope and never its vocabulary: the word itself is the
1314/// author's, guided by the routing block and by the repository's own
1315/// history. One owner by token: the title checks take it as
1316/// `RK_SCOPE_SHAPE` through [`render`], and `rk message --check` reads it
1317/// directly, so the desk and the forge judge one language.
1318pub const SCOPE_SHAPE: &str = "[a-z0-9._/-]+";
1319
1320/// Whether one scope matches [`SCOPE_SHAPE`].
1321///
1322/// The predicate and the pattern are one owner, so the desk's judgment
1323/// cannot drift from the forge's: `rk message --check` calls this, the
1324/// title checks render the pattern, and a test holds the two equal over
1325/// every ASCII character.
1326#[must_use]
1327pub fn scope_is_shaped(scope: &str) -> bool {
1328    !scope.is_empty()
1329        && scope.chars().all(|c| {
1330            c.is_ascii_lowercase() || c.is_ascii_digit() || matches!(c, '_' | '.' | '/' | '-')
1331        })
1332}
1333
1334/// The routing block from its authored template and the mode's one
1335/// orientation line.
1336///
1337/// Markers included, without a trailing newline and with its scope token
1338/// unrendered. Everything but the substituted line, the agent-boundary
1339/// line included, is byte-identical across modes.
1340#[must_use]
1341pub fn compose_routing(template: &str, line: &str) -> String {
1342    authored(template).replacen("RK_WORKFLOW_LINE", authored(line), 1)
1343}
1344
1345/// The glossary block from its authored template: markers included and
1346/// without a trailing newline. It carries no token and no mode, so the
1347/// same bytes land in every target.
1348#[must_use]
1349pub fn compose_glossary(template: &str) -> String {
1350    authored(template).to_owned()
1351}
1352
1353/// The hook block from its authored template, with the worktree mode's
1354/// guard entry where `guard` carries one.
1355///
1356/// `Some` is the worktree mode: the block carries the location guard and
1357/// names the sweep-skip pair. `None` is the branches mode: no guard entry
1358/// at all, never an entry that reads local state to decide whether to
1359/// enforce. The one branch grammar substitutes from [`BRANCH_GRAMMAR`].
1360/// Markers included, without a trailing newline and with its scope token
1361/// unrendered.
1362#[must_use]
1363pub fn compose_hooks(template: &str, guard: Option<&str>) -> String {
1364    let (guard, skip) = guard.map_or_else(
1365        || (String::new(), "no-commit-to-branch"),
1366        |entry| {
1367            (
1368                format!("{}\n", authored(entry)),
1369                "no-commit-to-branch,rk-worktree-location",
1370            )
1371        },
1372    );
1373    authored(template)
1374        .replacen("RK_BRANCH_GRAMMAR", BRANCH_GRAMMAR, 1)
1375        .replacen("RK_SWEEP_SKIP", skip, 1)
1376        .replacen("RK_WORKTREE_GUARD", &guard, 1)
1377}
1378
1379/// The routing block for one checkout mode, from this binary's embedded
1380/// templates.
1381///
1382/// # Errors
1383///
1384/// A block this binary does not embed, a defect in the binary.
1385pub fn routing_block(mode: CheckoutMode) -> Result<String, RkError> {
1386    Ok(compose_routing(
1387        embedded_block(AGENTS_BLOCK)?,
1388        embedded_block(routing_line(mode))?,
1389    ))
1390}
1391
1392/// The glossary block from this binary's embedded template.
1393///
1394/// # Errors
1395///
1396/// A block this binary does not embed, a defect in the binary.
1397pub fn glossary_block() -> Result<String, RkError> {
1398    Ok(compose_glossary(embedded_block(GLOSSARY_BLOCK)?))
1399}
1400
1401/// The hook block for one checkout mode, from this binary's embedded
1402/// templates.
1403///
1404/// # Errors
1405///
1406/// A block this binary does not embed, a defect in the binary.
1407pub fn hooks_block(mode: CheckoutMode) -> Result<String, RkError> {
1408    let guard = match mode {
1409        CheckoutMode::LinkedWorktree => Some(embedded_block(PRE_COMMIT_WORKTREE_GUARD)?),
1410        CheckoutMode::MainWorktree => None,
1411    };
1412    Ok(compose_hooks(embedded_block(PRE_COMMIT_BLOCK)?, guard))
1413}
1414
1415/// The unrendered block for one block destination under one checkout mode,
1416/// with the embedded source paths it was composed from.
1417fn block_template(destination: &str, mode: CheckoutMode) -> Result<(String, Vec<String>), RkError> {
1418    match destination {
1419        AGENTS_DESTINATION => Ok((
1420            routing_block(mode)?,
1421            vec![AGENTS_BLOCK.to_owned(), routing_line(mode).to_owned()],
1422        )),
1423        GLOSSARY_DESTINATION => Ok((glossary_block()?, vec![GLOSSARY_BLOCK.to_owned()])),
1424        HOOKS_DESTINATION => {
1425            let mut sources = vec![PRE_COMMIT_BLOCK.to_owned()];
1426            if mode == CheckoutMode::LinkedWorktree {
1427                sources.push(PRE_COMMIT_WORKTREE_GUARD.to_owned());
1428            }
1429            Ok((hooks_block(mode)?, sources))
1430        }
1431        other => Err(anyhow::anyhow!("{other} is not a block destination").into()),
1432    }
1433}
1434
1435/// The markers of a block destination, or `None` for a whole-file one.
1436#[must_use]
1437pub fn block_markers(destination: &str) -> Option<(&'static str, &'static str)> {
1438    match destination {
1439        AGENTS_DESTINATION | GLOSSARY_DESTINATION => Some((BLOCK_BEGIN, BLOCK_END)),
1440        HOOKS_DESTINATION => Some((HOOKS_BEGIN, HOOKS_END)),
1441        _ => None,
1442    }
1443}
1444
1445/// The marked block inside a document, markers included, or `None` where
1446/// the text carries no complete block.
1447#[must_use]
1448pub fn extract_block<'a>(text: &'a str, begin: &str, end: &str) -> Option<&'a str> {
1449    let start = text.find(begin)?;
1450    let stop = text[start..].find(end)? + start + end.len();
1451    Some(&text[start..stop])
1452}
1453
1454/// The whole document's bytes after splicing a marked block into it.
1455///
1456/// A fresh file where none exists, the block replaced in place where one
1457/// is marked, appended after the target's own content otherwise:
1458/// release-kit owns the lines inside the markers, not the document. Both
1459/// markdown destinations take this shape, `AGENTS.md` and the glossary.
1460#[must_use]
1461pub fn splice_marked_block(existing: Option<&[u8]>, block: &str) -> Vec<u8> {
1462    let block = block.as_bytes();
1463    let Some(text) = existing else {
1464        return [block, b"\n"].concat();
1465    };
1466    // Bytes, never text: the document belongs to the target and a decode
1467    // that replaces one invalid sequence rewrites a byte outside the
1468    // markers, which is the one thing a block destination never does.
1469    if let Some(start) = find(text, BLOCK_BEGIN.as_bytes())
1470        && let Some(offset) = find(&text[start..], BLOCK_END.as_bytes())
1471    {
1472        let stop = start + offset + BLOCK_END.len();
1473        return [&text[..start], block, &text[stop..]].concat();
1474    }
1475    // Appending keeps every byte the target wrote, trailing blank lines
1476    // and an absent final newline included. The only addition is the
1477    // separator that opens the block's own line.
1478    let mut out = Vec::with_capacity(text.len() + block.len() + 3);
1479    out.extend_from_slice(text);
1480    if !text.ends_with(b"\n") {
1481        out.push(b'\n');
1482    }
1483    out.push(b'\n');
1484    out.extend_from_slice(block);
1485    out.push(b'\n');
1486    out
1487}
1488
1489/// The whole `.pre-commit-config.yaml` content after splicing the
1490/// rendered hook block.
1491///
1492/// A fresh file carries the hook-types key, the `repos:` key, and the
1493/// block; a marked file takes the block in place; an unmarked file takes
1494/// it directly under its `repos:` line, above the target's own hooks. An
1495/// unmarked file with no `repos:` line is refused by name: the block's
1496/// entries are list items and have nowhere honest to go.
1497///
1498/// # Errors
1499///
1500/// The reason the block has no place, for the caller's refusal to carry.
1501pub fn splice_hooks_block(existing: Option<&str>, block: &str) -> Result<String, String> {
1502    let Some(text) = existing else {
1503        return Ok(format!("{HOOK_TYPES_LINE}\n\nrepos:\n{block}\n"));
1504    };
1505    if let Some(defect) = hooks_marker_defect(text) {
1506        return Err(defect);
1507    }
1508    if let Some(found) = extract_block(text, HOOKS_BEGIN, HOOKS_END) {
1509        return Ok(text.replacen(found, block, 1));
1510    }
1511    let mut out = String::with_capacity(text.len() + block.len() + 1);
1512    let mut placed = false;
1513    for line in text.split_inclusive('\n') {
1514        out.push_str(line);
1515        if !placed && line.trim_end() == "repos:" {
1516            if !out.ends_with('\n') {
1517                out.push('\n');
1518            }
1519            out.push_str(block);
1520            out.push('\n');
1521            placed = true;
1522        }
1523    }
1524    if placed {
1525        Ok(out)
1526    } else {
1527        Err(format!(
1528            "{HOOKS_DESTINATION} exists with no repos: line, so the hook block has nowhere to land"
1529        ))
1530    }
1531}
1532
1533/// The one definition of an ill-formed block document, shared by every
1534/// splice and every reader that judges one: `None` for a whole-file
1535/// destination or a well-formed document.
1536///
1537/// Ownership must be unambiguous: exactly one begin marker paired with
1538/// exactly one end marker after it, or none of either. A second begin is
1539/// a second block, which for the hook file pre-commit would still run,
1540/// and a marker without its pair, or an end before its begin, is a block
1541/// whose extent nothing can state.
1542#[must_use]
1543pub fn marker_defect(destination: &str, text: &str) -> Option<String> {
1544    let (begin, end) = block_markers(destination)?;
1545    let begins = text.matches(begin).count();
1546    let ends = text.matches(end).count();
1547    if begins > 1 || ends > 1 {
1548        return Some(format!(
1549            "{destination} carries more than one release-kit marker pair; release-kit owns exactly one block"
1550        ));
1551    }
1552    match (text.find(begin), text.find(end)) {
1553        (Some(begin), Some(end)) if end > begin => None,
1554        (None, None) => None,
1555        _ => Some(format!(
1556            "{destination} carries an unmatched or misordered release-kit marker, so the block's extent is ambiguous"
1557        )),
1558    }
1559}
1560
1561/// [`marker_defect`] for the hook file, the destination whose entries
1562/// execute.
1563#[must_use]
1564pub fn hooks_marker_defect(text: &str) -> Option<String> {
1565    marker_defect(HOOKS_DESTINATION, text)
1566}
1567
1568/// The complete proposed document for one block destination: the
1569/// rendered `region` spliced into the `existing` document the evidence
1570/// carries, or the reason the document offers it no place.
1571fn propose_document(
1572    destination: &str,
1573    existing: Option<&[u8]>,
1574    region: &[u8],
1575) -> Result<Vec<u8>, String> {
1576    // The block is release-kit's own text. The document is the target's
1577    // bytes: the markdown splice works on them directly, and the marker
1578    // judgment decodes a copy only to count ASCII markers, which a
1579    // replacement character neither creates nor hides.
1580    let block = String::from_utf8_lossy(region).into_owned();
1581    if let Some(text) = existing
1582        && let Some(defect) = marker_defect(destination, &String::from_utf8_lossy(text))
1583    {
1584        return Err(defect);
1585    }
1586    if destination == HOOKS_DESTINATION {
1587        // The hook splice is line-based text, so a document that is not
1588        // UTF-8 has no honest place for the block: a lossy decode would
1589        // rewrite a byte outside the markers, which a region never does.
1590        let text = match existing {
1591            None => None,
1592            Some(bytes) => Some(std::str::from_utf8(bytes).map_err(|_| {
1593                format!(
1594                    "{destination} is not UTF-8, so the hook block has nowhere to land without rewriting the target's bytes"
1595                )
1596            })?),
1597        };
1598        return splice_hooks_block(text, &block).map(String::into_bytes);
1599    }
1600    Ok(splice_marked_block(existing, &block))
1601}
1602
1603/// Why the whole Nix capability stays out of a landing, or `None` where
1604/// the target's crate shape supports the seed.
1605///
1606/// The gate holds every structural prerequisite the seed relies on, not
1607/// only evaluation: the package expression reads `Cargo.toml` through
1608/// `importTOML` and throws without `../Cargo.lock`, and the seed flake's
1609/// smoke check runs the crate's binary, which only an implicit
1610/// `src/main.rs` or an explicit `[[bin]]` entry produces. A shape
1611/// missing any of these would land files that fail on their first
1612/// evaluation or first check, so the landing reports the smaller product
1613/// with the missing piece named instead.
1614#[must_use]
1615pub fn nix_unsupported_shape(shape: &CrateShape) -> Option<String> {
1616    let Some(text) = shape.cargo_toml.as_deref() else {
1617        return Some(
1618            "the target has no readable Cargo.toml, which the seeded package expression reads; no Nix file lands".to_owned(),
1619        );
1620    };
1621    let Ok(table) = text.parse::<toml::Table>() else {
1622        return Some(
1623            "the target's Cargo.toml does not parse, and the seeded package expression reads it; no Nix file lands".to_owned(),
1624        );
1625    };
1626    if !table.contains_key("package") {
1627        return Some(
1628            "the target's Cargo.toml has no [package] table; the seed supports a single crate, so no Nix file lands".to_owned(),
1629        );
1630    }
1631    if !shape.cargo_lock {
1632        return Some(
1633            "the target has no Cargo.lock, which the seeded package expression builds from; commit one, then opt in".to_owned(),
1634        );
1635    }
1636    let implicit_bin = shape.main_rs
1637        && table
1638            .get("package")
1639            .and_then(toml::Value::as_table)
1640            .and_then(|package| package.get("autobins"))
1641            .and_then(toml::Value::as_bool)
1642            != Some(false);
1643    let explicit_bins = table.get("bin").and_then(toml::Value::as_array);
1644    if explicit_bins.is_none() && !implicit_bin {
1645        return Some(
1646            "the target declares no binary — no effective src/main.rs and no [[bin]] entry — and the seed flake's smoke check runs one; no Nix file lands".to_owned(),
1647        );
1648    }
1649    // The seed's mainProgram is the first [[bin]] entry; one whose
1650    // required-features a default build does not enable produces no
1651    // executable, so the smoke check would fail on a green landing. A
1652    // requirement the default feature set covers builds normally and
1653    // passes.
1654    if let Some(bins) = explicit_bins {
1655        let required = bins
1656            .first()
1657            .and_then(toml::Value::as_table)
1658            .and_then(|bin| bin.get("required-features"))
1659            .and_then(toml::Value::as_array);
1660        if let Some(required) = required {
1661            let enabled = default_features(&table);
1662            let missing = required
1663                .iter()
1664                .filter_map(toml::Value::as_str)
1665                .any(|feature| !enabled.contains(feature));
1666            if missing {
1667                return Some(
1668                    "the target's first [[bin]] entry requires features a default build does not enable; no Nix file lands".to_owned(),
1669                );
1670            }
1671        }
1672    }
1673    None
1674}
1675
1676/// Whether any feature's list carries a `dep:name` edge, which is what
1677/// suppresses the optional dependency's implicit same-named feature.
1678fn dep_edge_suppresses(features: &toml::Table, name: &str) -> bool {
1679    let edge = format!("dep:{name}");
1680    features.values().any(|list| {
1681        list.as_array().is_some_and(|entries| {
1682            entries
1683                .iter()
1684                .filter_map(toml::Value::as_str)
1685                .any(|entry| entry == edge)
1686        })
1687    })
1688}
1689
1690/// Whether `name` is declared an optional dependency, in any of the
1691/// dependency tables a binary's build reads.
1692fn is_optional_dependency(table: &toml::Table, name: &str) -> bool {
1693    ["dependencies", "build-dependencies"]
1694        .iter()
1695        .any(|section| {
1696            table
1697                .get(*section)
1698                .and_then(toml::Value::as_table)
1699                .and_then(|dependencies| dependencies.get(name))
1700                .and_then(toml::Value::as_table)
1701                .and_then(|dependency| dependency.get("optional"))
1702                .and_then(toml::Value::as_bool)
1703                == Some(true)
1704        })
1705}
1706
1707/// The features a default build enables: the `default` feature resolved
1708/// through the `[features]` table's own enables, an approximation of
1709/// cargo's default resolution for the documented supported shapes, erring
1710/// toward withholding where the semantics run deeper. Dependency forms,
1711/// `dep:name` and weak `name?/feature`, are not feature names here and are
1712/// skipped; the closure is bounded by the table's size.
1713fn default_features(table: &toml::Table) -> std::collections::BTreeSet<String> {
1714    let Some(features) = table.get("features").and_then(toml::Value::as_table) else {
1715        return std::collections::BTreeSet::new();
1716    };
1717    let mut enabled = std::collections::BTreeSet::new();
1718    let mut queue = vec!["default".to_owned()];
1719    while let Some(name) = queue.pop() {
1720        if !enabled.insert(name.clone()) {
1721            continue;
1722        }
1723        if let Some(implies) = features.get(&name).and_then(toml::Value::as_array) {
1724            for implied in implies.iter().filter_map(toml::Value::as_str) {
1725                if implied.starts_with("dep:") || implied.contains("?/") {
1726                    // `dep:name` enables the dependency without a feature
1727                    // of this crate; a weak `name?/feature` edge enables
1728                    // nothing by itself.
1729                    continue;
1730                }
1731                if let Some((package, _)) = implied.split_once('/') {
1732                    // A strong `name/feature` edge activates this crate's
1733                    // same-named feature only for an optional dependency,
1734                    // and only where that feature exists: declared
1735                    // explicitly, or implicit and not suppressed by a
1736                    // `dep:` edge anywhere in the table. A non-optional
1737                    // dependency's edge enables a feature of the
1738                    // dependency and nothing of this crate.
1739                    let feature_exists =
1740                        features.contains_key(package) || !dep_edge_suppresses(features, package);
1741                    if is_optional_dependency(table, package) && feature_exists {
1742                        queue.push(package.to_owned());
1743                    }
1744                } else {
1745                    queue.push(implied.to_owned());
1746                }
1747            }
1748        }
1749    }
1750    enabled
1751}
1752
1753/// Why the flake half of the Nix capability stays out of this landing, or
1754/// `None` where the pair lands whole.
1755///
1756/// The pair is all-or-nothing: a target that already carries a
1757/// `flake.nix` or `flake.lock` of its own keeps its pair, because a seed
1758/// lock beside a foreign flake describes the wrong input graph. A pair
1759/// the record names is release-kit's own landing and is never withheld.
1760#[must_use]
1761pub fn flake_pair_withheld(
1762    flake_recorded: bool,
1763    flake_nix_present: bool,
1764    flake_lock_present: bool,
1765) -> Option<String> {
1766    if flake_recorded {
1767        return None;
1768    }
1769    let present: Vec<&str> = [
1770        ("flake.nix", flake_nix_present),
1771        ("flake.lock", flake_lock_present),
1772    ]
1773    .into_iter()
1774    .filter_map(|(name, present)| present.then_some(name))
1775    .collect();
1776    if present.is_empty() {
1777        return None;
1778    }
1779    Some(format!(
1780        "the target already carries {}; its flake pair stays its own",
1781        present.join(" and ")
1782    ))
1783}
1784
1785/// The Nix destinations an opted-in landing withholds at this target, with
1786/// the one reason, or `None` where the capability lands whole or `nix` is
1787/// off.
1788///
1789/// An unsupported crate shape names the whole capability, and a flake
1790/// pair of the target's own names the pair while the seeded package
1791/// expression still lands. Every landing verb shares this one judgment, so
1792/// a stage, an apply, an upgrade, and an adoption all withhold
1793/// identically.
1794#[must_use]
1795pub fn nix_withholding(
1796    nix: bool,
1797    evidence: &TargetEvidence,
1798) -> Option<(&'static [&'static str], String)> {
1799    if !nix {
1800        return None;
1801    }
1802    if let Some(reason) = nix_unsupported_shape(&evidence.crate_shape) {
1803        return Some((&NIX_DESTINATIONS[..], reason));
1804    }
1805    flake_pair_withheld(
1806        evidence.flake_recorded,
1807        evidence.flake_nix_present,
1808        evidence.flake_lock_present,
1809    )
1810    .map(|reason| (&NIX_WITHHOLDABLE[..], reason))
1811}
1812
1813#[cfg(test)]
1814mod tests {
1815    use super::{
1816        AGENTS_DESTINATION, BLOCK_BEGIN, BLOCK_DESTINATIONS, BLOCK_END, Candidate, Collision,
1817        CrateShape, GLOSSARY_DESTINATION, HOOK_TYPES_LINE, HOOKS_BEGIN, HOOKS_DESTINATION,
1818        HOOKS_END, Placement, Projection, ProjectionInput, TargetEvidence, extract_block,
1819    };
1820    use crate::landing::{Params, Style};
1821
1822    /// A supported single-crate shape, so nothing is withheld.
1823    fn supported_shape() -> CrateShape {
1824        CrateShape {
1825            cargo_toml: Some("[package]\nname = \"widget\"\nversion = \"0.1.0\"\n".to_owned()),
1826            cargo_lock: true,
1827            main_rs: true,
1828        }
1829    }
1830
1831    fn input(evidence: TargetEvidence) -> ProjectionInput {
1832        let mut params = Params::for_test("acme/widget", Some(Style::Trunk));
1833        params.set_nix_for_test(true);
1834        ProjectionInput { params, evidence }
1835    }
1836
1837    fn compute(evidence: TargetEvidence) -> Projection {
1838        Projection::compute(&input(evidence)).expect("the embedded pair projects")
1839    }
1840
1841    fn candidate<'a>(projection: &'a Projection, destination: &str) -> &'a Candidate {
1842        projection
1843            .candidates
1844            .iter()
1845            .find(|candidate| candidate.destination == destination)
1846            .expect("the destination projects")
1847    }
1848
1849    /// The bytes of a document outside its one marked region.
1850    fn outside(bytes: &[u8], begin: &str, end: &str) -> (Vec<u8>, Vec<u8>) {
1851        let text = String::from_utf8_lossy(bytes);
1852        let start = text.find(begin).expect("the begin marker is present");
1853        let stop = text[start..].find(end).expect("the end marker is present") + start + end.len();
1854        (bytes[..start].to_vec(), bytes[stop..].to_vec())
1855    }
1856
1857    #[test]
1858    fn equal_projection_inputs_yield_byte_identical_projections() {
1859        let mut documents = std::collections::BTreeMap::new();
1860        documents.insert(
1861            AGENTS_DESTINATION.to_owned(),
1862            b"# Widget\n\nOwn rules.\n".to_vec(),
1863        );
1864        let evidence = TargetEvidence {
1865            documents,
1866            crate_shape: supported_shape(),
1867            ..TargetEvidence::default()
1868        };
1869        let first = input(evidence.clone());
1870        let second = input(evidence);
1871        assert_eq!(first, second, "the inputs are values and compare equal");
1872        let a = Projection::compute(&first).expect("the pair projects");
1873        let b = Projection::compute(&second).expect("the pair projects");
1874        assert_eq!(a.candidates.len(), b.candidates.len());
1875        for (x, y) in a.candidates.iter().zip(&b.candidates) {
1876            assert_eq!(x.destination, y.destination);
1877            assert_eq!(x.kind, y.kind);
1878            assert_eq!(x.placement, y.placement);
1879            assert_eq!(x.bytes, y.bytes, "{}", x.destination);
1880            assert_eq!(x.region, y.region, "{}", x.destination);
1881            assert_eq!(x.sources, y.sources, "{}", x.destination);
1882        }
1883        assert_eq!(a, b);
1884        let destinations: Vec<&str> = a
1885            .candidates
1886            .iter()
1887            .map(|candidate| candidate.destination.as_str())
1888            .collect();
1889        let mut sorted = destinations.clone();
1890        sorted.sort_unstable();
1891        assert_eq!(destinations, sorted, "candidates sort by destination");
1892        assert!(a.omissions.is_empty(), "{:?}", a.omissions);
1893        assert!(a.collisions.is_empty(), "{:?}", a.collisions);
1894    }
1895
1896    /// The Scorecard destination is classified, gated by its parameter
1897    /// alone, and rendered: the pure projection answers the capability
1898    /// with no target read and no forge call.
1899    #[test]
1900    fn the_scorecard_destination_projects_only_under_the_opt_in() {
1901        use super::{Kind, SCORECARD_DESTINATIONS, kind_of};
1902        let destination = SCORECARD_DESTINATIONS[0];
1903        assert_eq!(kind_of(destination), Some(Kind::Rendered));
1904
1905        let project = |scorecard: bool| {
1906            let mut params = Params::for_test("acme/widget", Some(Style::Trunk));
1907            params.set_scorecard_for_test(scorecard);
1908            Projection::compute(&ProjectionInput {
1909                params,
1910                evidence: TargetEvidence::default(),
1911            })
1912            .expect("the embedded pair projects")
1913        };
1914
1915        let off = project(false);
1916        assert!(
1917            !off.candidates
1918                .iter()
1919                .any(|candidate| candidate.destination == destination),
1920            "off by default, and an absent candidate is no omission"
1921        );
1922        assert!(off.omissions.is_empty(), "{:?}", off.omissions);
1923
1924        let on = project(true);
1925        let candidate = candidate(&on, destination);
1926        assert_eq!(candidate.kind, Kind::Rendered);
1927        assert_eq!(candidate.placement, Placement::Whole);
1928        let text = String::from_utf8_lossy(&candidate.bytes);
1929        assert!(!text.contains("RK_"), "a token survived: {text}");
1930    }
1931
1932    /// The licence judgment over the expression forms a crate manifest
1933    /// uses: every operand must be recognized, a disjunction of approved
1934    /// terms passes, and one unapproved operand anywhere fails.
1935    #[test]
1936    fn the_licence_judgment_reads_every_operand() {
1937        use super::licence_is_osi_approved as approved;
1938        for expression in [
1939            "MIT",
1940            "Apache-2.0",
1941            "MIT OR Apache-2.0",
1942            "MIT AND Apache-2.0",
1943            "(MIT OR Apache-2.0) AND ISC",
1944            "Apache-2.0 WITH LLVM-exception OR MIT",
1945            "GPL-3.0+",
1946        ] {
1947            assert!(approved(expression), "{expression} is OSI-approved");
1948        }
1949        for expression in [
1950            "",
1951            "   ",
1952            "LicenseRef-proprietary",
1953            "MIT AND LicenseRef-proprietary",
1954            "SEE LICENSE IN COPYING",
1955            "CC-BY-4.0",
1956            "DocumentRef-spdx:LicenseRef-proprietary",
1957        ] {
1958            assert!(!approved(expression), "{expression} is not recognized");
1959        }
1960        // A malformed expression is refused rather than read for the
1961        // identifiers it happens to carry. Each of these names at least one
1962        // approved licence and states no complete offer, and accepting any
1963        // of them would land a workflow whose terms nothing established.
1964        for expression in [
1965            "MIT OR",
1966            "OR MIT",
1967            "MIT AND",
1968            "MIT WITH",
1969            "WITH LLVM-exception",
1970            "(MIT",
1971            "MIT)",
1972            "MIT Apache-2.0",
1973            "()",
1974            "(MIT OR Apache-2.0",
1975            "MIT OR (Apache-2.0",
1976            "MIT OR ()",
1977            "AND",
1978            "(",
1979            ")",
1980            "MIT WITH AND ISC",
1981            "MIT OR OR ISC",
1982            "MIT @ Apache-2.0",
1983        ] {
1984            assert!(!approved(expression), "{expression} is malformed");
1985        }
1986        // Well-formed nesting and a nested exception still read.
1987        for expression in [
1988            "MIT AND (Apache-2.0 OR ISC)",
1989            "((MIT))",
1990            "Apache-2.0 WITH LLVM-exception AND ISC",
1991            "(Apache-2.0 WITH LLVM-exception)",
1992        ] {
1993            assert!(approved(expression), "{expression} is well formed");
1994        }
1995        // SPDX states an identifier "should be matched in a case-insensitive
1996        // manner", and cargo accepts `license = "mit"` without complaint, so a
1997        // real crate can carry any casing and none of these may read as
1998        // unrecognized.
1999        for expression in [
2000            "mit",
2001            "MiT",
2002            "apache-2.0 OR mit",
2003            "APACHE-2.0 WITH llvm-exception",
2004        ] {
2005            assert!(
2006                approved(expression),
2007                "{expression} names an approved licence"
2008            );
2009        }
2010        // The operators are the other half of the same sentence: SPDX matches
2011        // them case-sensitively, so a lowercase one is not an operator and the
2012        // expression it appears in is malformed.
2013        for expression in [
2014            "MIT or Apache-2.0",
2015            "MIT and Apache-2.0",
2016            "MIT with LLVM-exception",
2017        ] {
2018            assert!(!approved(expression), "{expression} carries no operator");
2019        }
2020        // The operand after WITH must be an exception identifier. One this
2021        // release does not recognize leaves the expression malformed, so the
2022        // licence goes unread rather than being taken from the left operand.
2023        for expression in [
2024            "MIT WITH definitely-not-an-spdx-exception",
2025            "MIT WITH MIT",
2026            "MIT WITH Apache-2.0",
2027        ] {
2028            assert!(!approved(expression), "{expression} names no exception");
2029        }
2030        // The register is carried whole, not sampled. A subset refuses a real
2031        // crate: this one names an exception older than the version a sampled
2032        // list would have kept.
2033        for expression in [
2034            "GPL-2.0-only WITH GCC-exception-2.0",
2035            "GPL-2.0-or-later WITH Classpath-exception-2.0",
2036            "Apache-2.0 WITH Swift-exception",
2037            "GPL-3.0-only WITH Autoconf-exception-generic",
2038        ] {
2039            assert!(approved(expression), "{expression} names a real exception");
2040        }
2041    }
2042
2043    /// The compatibility question has one owner, and its answer is the
2044    /// catalog's unavailable reason: a receipt reaches `rk status` through
2045    /// `from_record`, which cannot fail, so a provider whose pair ships
2046    /// nothing is named rather than read as clean. It is a report about the
2047    /// pair, not a fault in the target, so nothing refuses on it.
2048    #[test]
2049    fn a_recorded_provider_its_pair_cannot_run_is_named() {
2050        use super::{Provider, code_scanning_incompatibility};
2051
2052        assert!(code_scanning_incompatibility(None, Some("bash"), Some("gitlab")).is_none());
2053        assert!(
2054            code_scanning_incompatibility(Some(Provider::Semgrep), Some("rust"), Some("gitlab"))
2055                .is_none()
2056        );
2057        assert!(
2058            code_scanning_incompatibility(Some(Provider::CodeQl), Some("rust"), Some("github"))
2059                .is_none()
2060        );
2061
2062        let bash =
2063            code_scanning_incompatibility(Some(Provider::Semgrep), Some("bash"), Some("github"))
2064                .expect("a binding with no scanner is named");
2065        assert!(bash.contains("bash binding"), "{bash}");
2066        let gitlab =
2067            code_scanning_incompatibility(Some(Provider::CodeQl), Some("rust"), Some("gitlab"))
2068                .expect("codeql on gitlab is named");
2069        assert!(gitlab.contains("codeql"), "{gitlab}");
2070        let release_less =
2071            code_scanning_incompatibility(Some(Provider::Semgrep), None, Some("github"))
2072                .expect("no driver is named");
2073        assert!(
2074            release_less.contains("no automatic release driver"),
2075            "{release_less}"
2076        );
2077
2078        // The projection carries the same reason, so a record-only reader sees
2079        // it without going through resolution.
2080        let mut params = Params::for_test("acme/widget", Some(Style::Trunk));
2081        params.set_code_scanning_for_test(Some(Provider::Semgrep));
2082        let clean = Projection::compute(&ProjectionInput {
2083            params: params.clone(),
2084            evidence: TargetEvidence::default(),
2085        })
2086        .expect("the pair projects");
2087        assert!(
2088            clean.record_defects.is_empty(),
2089            "{:?}",
2090            clean.record_defects
2091        );
2092    }
2093
2094    /// The code scanning capability: the provider gates its own destination,
2095    /// semgrep carries no licence condition, and codeql's condition reads the
2096    /// crate's declared licence without touching the filesystem.
2097    #[test]
2098    fn the_code_scanning_destinations_follow_the_recorded_provider() {
2099        use super::{
2100            CODE_SCANNING_DESTINATIONS, Kind, Provider, code_scanning_licence_refusal, kind_of,
2101        };
2102        for (destination, _) in CODE_SCANNING_DESTINATIONS {
2103            assert_eq!(kind_of(destination), Some(Kind::Rendered), "{destination}");
2104        }
2105
2106        let project = |provider: Option<Provider>| {
2107            let mut params = Params::for_test("acme/widget", Some(Style::Trunk));
2108            params.set_code_scanning_for_test(provider);
2109            Projection::compute(&ProjectionInput {
2110                params,
2111                evidence: TargetEvidence {
2112                    crate_shape: CrateShape {
2113                        cargo_toml: Some(
2114                            "[package]\nname = \"widget\"\nlicense = \"MIT\"\n".to_owned(),
2115                        ),
2116                        ..CrateShape::default()
2117                    },
2118                    ..TargetEvidence::default()
2119                },
2120            })
2121            .expect("the embedded pair projects")
2122        };
2123        let landed = |projection: &Projection, destination: &str| {
2124            projection
2125                .candidates
2126                .iter()
2127                .any(|candidate| candidate.destination == destination)
2128        };
2129
2130        let off = project(None);
2131        for (destination, _) in CODE_SCANNING_DESTINATIONS {
2132            assert!(!landed(&off, destination), "{destination}");
2133        }
2134        assert!(off.licence_refusal.is_none());
2135
2136        let codeql = project(Some(Provider::CodeQl));
2137        assert!(landed(
2138            &codeql,
2139            ".github/workflows/code-scanning-codeql.yml"
2140        ));
2141        assert!(!landed(
2142            &codeql,
2143            ".github/workflows/code-scanning-semgrep.yml"
2144        ));
2145        assert!(codeql.licence_refusal.is_none(), "MIT satisfies the terms");
2146
2147        let semgrep = project(Some(Provider::Semgrep));
2148        assert!(landed(
2149            &semgrep,
2150            ".github/workflows/code-scanning-semgrep.yml"
2151        ));
2152        assert!(!landed(
2153            &semgrep,
2154            ".github/workflows/code-scanning-codeql.yml"
2155        ));
2156
2157        // Semgrep carries no condition, whatever the licence says.
2158        let proprietary = CrateShape {
2159            cargo_toml: Some("[package]\nlicense = \"LicenseRef-proprietary\"\n".to_owned()),
2160            ..CrateShape::default()
2161        };
2162        assert!(
2163            code_scanning_licence_refusal(Some(Provider::Semgrep), Some("rust"), &proprietary)
2164                .is_none()
2165        );
2166        let refusal =
2167            code_scanning_licence_refusal(Some(Provider::CodeQl), Some("rust"), &proprietary)
2168                .expect("codeql refuses a licence its terms do not cover");
2169        assert!(refusal.contains("LicenseRef-proprietary"), "{refusal}");
2170        assert!(refusal.contains("semgrep"), "{refusal}");
2171        // A binding whose licence field this release does not read refuses
2172        // rather than assuming the terms are met.
2173        let bash =
2174            code_scanning_licence_refusal(Some(Provider::CodeQl), Some("bash"), &proprietary)
2175                .expect("an unread binding refuses");
2176        assert!(bash.contains("bash binding"), "{bash}");
2177    }
2178
2179    /// The pure boundary, held by a source scan over this file's
2180    /// production code: everything above the first `#[cfg(test)]`, with
2181    /// comment lines skipped. The evidence gathering that reads a target
2182    /// lives in `src/projection/evidence.rs`, which this scan does not
2183    /// cover on purpose.
2184    #[test]
2185    fn the_projection_performs_no_filesystem_git_environment_clock_registry_or_network_read() {
2186        let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src/projection.rs");
2187        let text = std::fs::read_to_string(&path).expect("the source reads");
2188        let production = text.split("#[cfg(test)]").next().unwrap_or("");
2189        let needles = [
2190            "std::fs",
2191            "std::env",
2192            "std::process",
2193            "std::time",
2194            "SystemTime",
2195            "Instant",
2196            "std::net",
2197            "Command::new",
2198            "registry::",
2199            "curl",
2200            "reqwest",
2201            "blob(",
2202        ];
2203        let mut hits = Vec::new();
2204        for (index, line) in production.lines().enumerate() {
2205            if line.trim_start().starts_with("//") {
2206                continue;
2207            }
2208            for needle in needles {
2209                if line.contains(needle) {
2210                    hits.push(format!("src/projection.rs:{}: {needle}", index + 1));
2211                }
2212            }
2213        }
2214        assert!(
2215            hits.is_empty(),
2216            "the projection reads beyond its inputs: {hits:?}"
2217        );
2218    }
2219
2220    #[test]
2221    #[allow(
2222        clippy::too_many_lines,
2223        reason = "one test walks the three marked destinations and the three unmarked shapes"
2224    )]
2225    fn marked_region_projection_preserves_every_target_byte_outside_the_markers() {
2226        let agents_before = "# Widget\n\nOperator prose above.\n\n";
2227        let agents_after = "\n\n## Our rules\n\nOperator prose below.   \n";
2228        let glossary_before = "# Glossary\n\n- `spike` is a throwaway branch.\n\n";
2229        let glossary_after = "\n\n## More terms\n\n- `own` is ours.";
2230        let hooks_before = "default_install_hook_types: [pre-commit]\n\nrepos:\n";
2231        let hooks_after =
2232            "\n  - repo: https://example.com/own\n    rev: v1\n    hooks:\n      - id: own\n";
2233        let stale = |begin: &str, end: &str| format!("{begin}\nstale block\n{end}");
2234        let mut documents = std::collections::BTreeMap::new();
2235        documents.insert(
2236            AGENTS_DESTINATION.to_owned(),
2237            format!(
2238                "{agents_before}{}{agents_after}",
2239                stale(BLOCK_BEGIN, BLOCK_END)
2240            )
2241            .into_bytes(),
2242        );
2243        documents.insert(
2244            GLOSSARY_DESTINATION.to_owned(),
2245            format!(
2246                "{glossary_before}{}{glossary_after}",
2247                stale(BLOCK_BEGIN, BLOCK_END)
2248            )
2249            .into_bytes(),
2250        );
2251        documents.insert(
2252            HOOKS_DESTINATION.to_owned(),
2253            format!(
2254                "{hooks_before}{}{hooks_after}",
2255                stale(HOOKS_BEGIN, HOOKS_END)
2256            )
2257            .into_bytes(),
2258        );
2259        let projection = compute(TargetEvidence {
2260            documents: documents.clone(),
2261            crate_shape: supported_shape(),
2262            ..TargetEvidence::default()
2263        });
2264        assert!(
2265            projection.collisions.is_empty(),
2266            "{:?}",
2267            projection.collisions
2268        );
2269        for (destination, before, after) in [
2270            (AGENTS_DESTINATION, agents_before, agents_after),
2271            (GLOSSARY_DESTINATION, glossary_before, glossary_after),
2272            (HOOKS_DESTINATION, hooks_before, hooks_after),
2273        ] {
2274            let candidate = candidate(&projection, destination);
2275            let Placement::Region { begin, end } = candidate.placement else {
2276                panic!("{destination} is a region");
2277            };
2278            let region = candidate
2279                .region
2280                .as_deref()
2281                .expect("a region carries its block");
2282            let (head, tail) = outside(&candidate.bytes, begin, end);
2283            assert_eq!(
2284                head,
2285                before.as_bytes(),
2286                "{destination}: bytes before the markers"
2287            );
2288            assert_eq!(
2289                tail,
2290                after.as_bytes(),
2291                "{destination}: bytes after the markers"
2292            );
2293            let inside = &candidate.bytes[head.len()..candidate.bytes.len() - tail.len()];
2294            assert_eq!(
2295                inside, region,
2296                "{destination}: the region is the rendered block"
2297            );
2298            let (existing_head, existing_tail) = outside(&documents[destination], begin, end);
2299            assert_eq!(head, existing_head);
2300            assert_eq!(tail, existing_tail);
2301        }
2302
2303        // Unmarked documents: the hook block lands under the owning key,
2304        // the routing block appends, and an absent file yields a fresh
2305        // document.
2306        let own_hooks =
2307            "repos:\n  - repo: https://example.com/own\n    rev: v1\n    hooks:\n      - id: own\n";
2308        let own_agents = "# Widget\n\nOwn rules.";
2309        let mut documents = std::collections::BTreeMap::new();
2310        documents.insert(HOOKS_DESTINATION.to_owned(), own_hooks.as_bytes().to_vec());
2311        documents.insert(
2312            AGENTS_DESTINATION.to_owned(),
2313            own_agents.as_bytes().to_vec(),
2314        );
2315        let projection = compute(TargetEvidence {
2316            documents,
2317            crate_shape: supported_shape(),
2318            ..TargetEvidence::default()
2319        });
2320        assert!(
2321            projection.collisions.is_empty(),
2322            "{:?}",
2323            projection.collisions
2324        );
2325        let hooks = candidate(&projection, HOOKS_DESTINATION);
2326        let hooks_text = String::from_utf8_lossy(&hooks.bytes);
2327        let region = String::from_utf8_lossy(hooks.region.as_deref().expect("a region"));
2328        assert!(
2329            hooks_text.starts_with(&format!(
2330                "repos:\n{region}\n  - repo: https://example.com/own"
2331            )),
2332            "{hooks_text}"
2333        );
2334        assert!(!hooks_text.contains(HOOK_TYPES_LINE));
2335        let agents = candidate(&projection, AGENTS_DESTINATION);
2336        assert!(agents.bytes.starts_with(own_agents.as_bytes()));
2337        assert_eq!(
2338            extract_block(
2339                &String::from_utf8_lossy(&agents.bytes),
2340                BLOCK_BEGIN,
2341                BLOCK_END
2342            )
2343            .map(str::as_bytes),
2344            agents.region.as_deref()
2345        );
2346        let glossary = candidate(&projection, GLOSSARY_DESTINATION);
2347        let region = glossary.region.as_deref().expect("a region");
2348        assert_eq!(
2349            glossary.bytes,
2350            [region, b"\n"].concat(),
2351            "an absent file is fresh"
2352        );
2353    }
2354
2355    /// A hook document that is not UTF-8 offers the block no place: the
2356    /// line-based splice would have to decode it, and a lossy decode
2357    /// rewrites a byte outside the markers. A valid document still
2358    /// splices, and the markdown destinations, spliced as bytes, take an
2359    /// invalid byte outside their markers unchanged.
2360    #[test]
2361    fn a_hook_document_that_is_not_utf8_collides_instead_of_being_rewritten() {
2362        let mut documents = std::collections::BTreeMap::new();
2363        let mut invalid = b"repos:\n# own \xff above\n".to_vec();
2364        invalid.extend_from_slice(format!("{HOOKS_BEGIN}\nstale\n{HOOKS_END}\n").as_bytes());
2365        invalid.extend_from_slice(b"  - repo: local \xff below\n");
2366        documents.insert(HOOKS_DESTINATION.to_owned(), invalid);
2367        let mut agents = b"# Widget r\xe9sum\xe9\n\n".to_vec();
2368        agents.extend_from_slice(format!("{BLOCK_BEGIN}\nstale\n{BLOCK_END}\n\n").as_bytes());
2369        agents.extend_from_slice(b"r\xe9sum\xe9\n");
2370        documents.insert(AGENTS_DESTINATION.to_owned(), agents.clone());
2371        let projection = compute(TargetEvidence {
2372            documents,
2373            crate_shape: supported_shape(),
2374            ..TargetEvidence::default()
2375        });
2376        let collided: Vec<&str> = projection
2377            .collisions
2378            .iter()
2379            .map(|c| c.destination.as_str())
2380            .collect();
2381        assert_eq!(collided, [HOOKS_DESTINATION]);
2382        assert!(
2383            projection.collisions[0].reason.contains("not UTF-8"),
2384            "{}",
2385            projection.collisions[0].reason
2386        );
2387        assert!(
2388            !projection
2389                .candidates
2390                .iter()
2391                .any(|c| c.destination == HOOKS_DESTINATION),
2392            "a colliding destination projects no candidate"
2393        );
2394        let agents = candidate(&projection, AGENTS_DESTINATION);
2395        assert!(
2396            agents.bytes.starts_with(b"# Widget r\xe9sum\xe9\n\n"),
2397            "{:?}",
2398            agents.bytes
2399        );
2400        assert!(
2401            agents.bytes.ends_with(b"\n\nr\xe9sum\xe9\n"),
2402            "{:?}",
2403            agents.bytes
2404        );
2405        assert!(
2406            !agents.bytes.contains(&0xEF),
2407            "a replacement character landed"
2408        );
2409
2410        let mut documents = std::collections::BTreeMap::new();
2411        let valid =
2412            format!("repos:\n# own above\n{HOOKS_BEGIN}\nstale\n{HOOKS_END}\n  - repo: local\n");
2413        documents.insert(HOOKS_DESTINATION.to_owned(), valid.into_bytes());
2414        let projection = compute(TargetEvidence {
2415            documents,
2416            crate_shape: supported_shape(),
2417            ..TargetEvidence::default()
2418        });
2419        assert!(
2420            projection.collisions.is_empty(),
2421            "{:?}",
2422            projection.collisions
2423        );
2424        let hooks = candidate(&projection, HOOKS_DESTINATION);
2425        let text = String::from_utf8(hooks.bytes.clone()).expect("a valid document stays text");
2426        assert!(text.starts_with("repos:\n# own above\n"), "{text}");
2427        assert!(text.ends_with("\n  - repo: local\n"), "{text}");
2428        assert!(!text.contains("stale"), "the region is replaced: {text}");
2429    }
2430
2431    /// A snippet that ships a block destination as a whole file is a
2432    /// source defect named by both sides: the snippet's source path and
2433    /// the block's template paths.
2434    #[test]
2435    fn a_whole_file_colliding_with_a_marked_region_names_both_source_paths() {
2436        let files: Vec<(String, &[u8])> = vec![
2437            ("snippets/_shared/github/SECURITY.md".to_owned(), b"policy"),
2438            ("snippets/rust/github/AGENTS.md".to_owned(), b"whole"),
2439        ];
2440        let err = Projection::compute_over(&files, &input(TargetEvidence::default()))
2441            .expect_err("a whole file at a block destination refuses");
2442        let text = err.to_string();
2443        assert!(text.contains("snippets/rust/github/AGENTS.md"), "{text}");
2444        assert!(text.contains(super::AGENTS_BLOCK), "{text}");
2445        assert!(text.contains(super::AGENTS_LINE_WORKTREE), "{text}");
2446        assert!(text.contains("embedded sources are defective"), "{text}");
2447    }
2448
2449    #[test]
2450    fn duplicate_whole_file_destinations_and_overlapping_marked_regions_refuse_with_the_conflicting_source_names()
2451     {
2452        // Two capabilities shipping one destination is a source defect
2453        // named by both sides.
2454        let files: Vec<(String, &[u8])> = vec![
2455            ("snippets/_shared/github/SECURITY.md".to_owned(), b"shared"),
2456            ("snippets/rust/github/SECURITY.md".to_owned(), b"pair"),
2457            ("snippets/rust/github/release-plz.toml".to_owned(), b"seed"),
2458        ];
2459        let err = Projection::compute_over(&files, &input(TargetEvidence::default()))
2460            .expect_err("a doubled destination refuses");
2461        let text = err.to_string();
2462        assert!(
2463            text.contains("snippets/_shared/github/SECURITY.md"),
2464            "{text}"
2465        );
2466        assert!(text.contains("snippets/rust/github/SECURITY.md"), "{text}");
2467        assert!(text.contains("embedded sources are defective"), "{text}");
2468
2469        let clean: Vec<(String, &[u8])> = vec![
2470            ("snippets/_shared/github/SECURITY.md".to_owned(), b"shared"),
2471            ("snippets/rust/github/release-plz.toml".to_owned(), b"seed"),
2472        ];
2473        let projection = Projection::compute_over(&clean, &input(TargetEvidence::default()))
2474            .expect("a clean list projects");
2475        let whole: Vec<&str> = projection
2476            .candidates
2477            .iter()
2478            .filter(|c| c.placement == Placement::Whole)
2479            .map(|c| c.destination.as_str())
2480            .collect();
2481        assert_eq!(whole, ["SECURITY.md", "release-plz.toml"]);
2482        assert_eq!(
2483            projection
2484                .candidates
2485                .iter()
2486                .find(|c| c.destination == "SECURITY.md")
2487                .map(|c| c.sources.clone()),
2488            Some(vec!["snippets/_shared/github/SECURITY.md".to_owned()])
2489        );
2490
2491        let doubled = format!("{BLOCK_BEGIN}\na\n{BLOCK_END}\n{BLOCK_BEGIN}\nb\n{BLOCK_END}\n");
2492        let unmatched = format!("repos:\n{HOOKS_BEGIN}\n  - repo: local\n");
2493        let misordered = format!("# G\n{BLOCK_END}\n{BLOCK_BEGIN}\n");
2494        let mut documents = std::collections::BTreeMap::new();
2495        documents.insert(AGENTS_DESTINATION.to_owned(), doubled.into_bytes());
2496        documents.insert(HOOKS_DESTINATION.to_owned(), unmatched.into_bytes());
2497        documents.insert(GLOSSARY_DESTINATION.to_owned(), misordered.into_bytes());
2498        let projection = compute(TargetEvidence {
2499            documents,
2500            crate_shape: supported_shape(),
2501            ..TargetEvidence::default()
2502        });
2503        let mut collided: Vec<&str> = projection
2504            .collisions
2505            .iter()
2506            .map(|Collision { destination, .. }| destination.as_str())
2507            .collect();
2508        collided.sort_unstable();
2509        let mut expected = BLOCK_DESTINATIONS.to_vec();
2510        expected.sort_unstable();
2511        assert_eq!(collided, expected);
2512        for collision in &projection.collisions {
2513            assert!(
2514                collision.reason.contains(&collision.destination),
2515                "{collision:?}"
2516            );
2517            assert!(
2518                !projection
2519                    .candidates
2520                    .iter()
2521                    .any(|candidate| candidate.destination == collision.destination),
2522                "{} collided and still projects",
2523                collision.destination
2524            );
2525        }
2526        let agents = projection
2527            .collisions
2528            .iter()
2529            .find(|c| c.destination == AGENTS_DESTINATION)
2530            .expect("the doubled document collides");
2531        assert!(agents.reason.contains("more than one"), "{}", agents.reason);
2532        let hooks = projection
2533            .collisions
2534            .iter()
2535            .find(|c| c.destination == HOOKS_DESTINATION)
2536            .expect("the unmatched document collides");
2537        assert!(hooks.reason.contains("unmatched"), "{}", hooks.reason);
2538    }
2539}