Skip to main content

spec_driven_docs/
self_depend.rs

1//! How a consumer obtains this tool, and how its pin stays fresh.
2//!
3//! A project pins this tool through the manager it already runs, at a
4//! version its manager file records. This module reads that pin, serves the
5//! fragment a manager needs to take one, and moves the pin to a newer
6//! release in one transaction. It writes into a file the project owns only
7//! where the project authorized it: the pin the sync line moves.
8//!
9//! Two independent axes decide every verdict. A manager is what the project
10//! declares its development tools in ([`manager::Manager`]). A venue is where
11//! a release of this tool is published ([`venue::Venue`]). Their cross is the
12//! matrix in [`venue`], and every pair carries a verdict there and nowhere
13//! else.
14
15pub mod fragments;
16pub mod leftovers;
17pub mod manager;
18pub mod pin;
19pub mod stamp;
20pub mod status;
21pub mod txn;
22pub mod venue;
23
24use crate::domain::manifest::CANON_SOURCE;
25
26/// The binary this crate installs, as an archive names it.
27pub const BINARY_NAME: &str = "sdd";
28
29/// The line a project's shell loader runs on every directory entry.
30pub const SYNC_LINE: &str = "sdd self-depend sync --apply || true";
31
32/// The shell loader file a project keeps at its root.
33pub const ENVRC: &str = ".envrc";
34
35/// The forge owner and repository this tool is published from, read from
36/// the one place the canon's home is declared.
37#[must_use]
38pub fn coordinates() -> (&'static str, &'static str) {
39    let path = CANON_SOURCE
40        .trim_start_matches("https://github.com/")
41        .trim_end_matches(".git");
42    path.split_once('/').unwrap_or((path, ""))
43}
44
45/// The `owner/repo` slug the forge and the flake reference use.
46#[must_use]
47pub fn slug() -> String {
48    let (owner, repo) = coordinates();
49    format!("{owner}/{repo}")
50}
51
52/// The version a release tag names, from any of the spellings a caller
53/// uses: `v0.2.16`, `0.2.16`, or the release page URL ending in the tag.
54///
55/// # Errors
56///
57/// A message naming the value where it is not a released triple.
58pub fn parse_tag(value: &str) -> Result<semver::Version, String> {
59    let tail = value.trim().rsplit('/').next().unwrap_or(value).trim();
60    let bare = tail.strip_prefix('v').unwrap_or(tail);
61    let version: semver::Version = bare
62        .parse()
63        .map_err(|_| format!("{value} is not a release tag: expected v<major>.<minor>.<patch>"))?;
64    if !version.pre.is_empty() || !version.build.is_empty() {
65        return Err(format!("{value} is not a released triple"));
66    }
67    Ok(version)
68}
69
70#[cfg(test)]
71mod tests {
72    #![allow(clippy::unwrap_used, reason = "a test panics as its failure signal")]
73
74    use super::*;
75
76    #[test]
77    fn the_coordinates_come_from_the_canon_source() {
78        let (owner, repo) = coordinates();
79        assert!(!owner.is_empty());
80        assert_eq!(repo, "spec-driven-docs");
81        assert!(CANON_SOURCE.ends_with(&slug()));
82    }
83
84    #[test]
85    fn a_tag_parses_in_every_spelling() {
86        assert_eq!(parse_tag("v0.2.16").unwrap().to_string(), "0.2.16");
87        assert_eq!(parse_tag("0.2.16").unwrap().to_string(), "0.2.16");
88        assert_eq!(
89            parse_tag("https://example.invalid/releases/tag/v1.2.3")
90                .unwrap()
91                .to_string(),
92            "1.2.3"
93        );
94        assert!(parse_tag("v1.2.3-rc.1").is_err());
95        assert!(parse_tag("latest").is_err());
96    }
97}