Skip to main content

artifactize_core/config/edit/
snapshot.rs

1//! Project snapshots and pure projections of their layout and edit footprint.
2
3use super::*;
4
5#[derive(Debug, Default, Serialize, Deserialize)]
6pub struct Layout {
7    pub nodes: BTreeMap<PlacementKey, [f64; 2]>,
8}
9/// Branch-current places. The top level is hand-placed when a plain node key exists;
10/// a box is hand-placed when a `whole/part` key exists. No layout file is read.
11pub fn read_layout(project: &Path) -> Result<Layout, Refusal> {
12    let read = read_project(project)?;
13    let mut layout = Layout::default();
14    for (name, node) in read.nodes {
15        if let Some(at) = node.annotations.at {
16            layout.nodes.insert(PlacementKey::Node(name.clone()), at);
17        }
18        for (part, at) in node.annotations.parts {
19            layout.nodes.insert(
20                PlacementKey::Part {
21                    whole: name.clone(),
22                    part,
23                },
24                at,
25            );
26        }
27    }
28    Ok(layout)
29}
30#[derive(Debug, Serialize)]
31#[serde(rename_all = "camelCase")]
32pub struct Node {
33    #[serde(with = "crate::platform::path_serde")]
34    pub definition: PathBuf,
35    pub identity: crate::types::Sha256Digest,
36    /// Authored PATH, including its kind-bearing trailing slash or URI.
37    pub target: String,
38    #[serde(with = "crate::platform::path_serde")]
39    pub path: PathBuf,
40    pub kind: ArtifactKind,
41    pub target_missing: bool,
42    #[serde(skip)]
43    pub parts: Vec<ArtifactName>,
44    pub annotations: Annotations,
45}
46#[derive(Debug, Serialize)]
47pub struct Project {
48    #[serde(skip)]
49    root: PathBuf,
50    pub files: BasedOn,
51    pub nodes: BTreeMap<ArtifactName, Node>,
52}
53/// Definition files a proposed operation changes, with their snapshot bases. A new
54/// destination has no current text. Paths are project-relative, exactly as in `BasedOn`.
55#[derive(Debug, Clone, Serialize)]
56pub struct Footprint {
57    #[serde(with = "crate::platform::path_serde::map")]
58    pub files: BTreeMap<PathBuf, Option<String>>,
59}
60
61/// Compute the exact write set from a `read_project` snapshot without writing files,
62/// acquiring edit locks or creating state. This is the same candidate edit as `apply`.
63/// Use its files as `BasedOn` and to guard unsaved Source drafts; destinations and
64/// reference rewrites belong to the library. Disk edits after the snapshot are still
65/// detected by `apply`'s compare-and-swap, never silently adopted as fresh bases.
66pub fn footprint(project: &Project, operation: &Operation) -> Result<Footprint, Refusal> {
67    let sources = project
68        .files
69        .iter()
70        .filter_map(|(file, source)| {
71            source
72                .as_ref()
73                .map(|source| (project.root.join(file), source.clone()))
74        })
75        .collect::<BTreeMap<_, _>>();
76    let proposed = proposal::propose(&project.root, &sources, operation.clone())?;
77    discovery::read_sources(project.root.clone(), &proposed)?;
78    let files = proposal::changes(&sources, &proposed)
79        .into_keys()
80        .map(|file| {
81            let base = sources.get(&file).cloned();
82            (
83                file.strip_prefix(&project.root)
84                    .expect("project definition")
85                    .to_owned(),
86                base,
87            )
88        })
89        .collect();
90    Ok(Footprint { files })
91}
92
93/// A file's exact content is its optimistic base; `identity` is SHA-256 for view clients.
94/// Invalid annotation values are ignored for display and described in their error fields.
95pub fn read_project(project: &Path) -> Result<Project, Refusal> {
96    let root = crate::platform::canonicalize(project).map_err(io)?;
97    let sources = discovery::sources(&root)?;
98    let mut config = discovery::read_sources(root.clone(), &sources)?;
99    discovery::inspect_targets(&mut config);
100    let files = sources
101        .into_iter()
102        .map(|(file, source)| {
103            (
104                file.strip_prefix(&root)
105                    .expect("definition below project")
106                    .to_owned(),
107                Some(source),
108            )
109        })
110        .collect::<BasedOn>();
111    let nodes = config
112        .artifacts
113        .iter()
114        .map(|(name, artifact)| {
115            let definition = artifact.declaration_path();
116            let source = files[&definition].as_deref().expect("definition text");
117            let identity = crate::types::Sha256Digest::of(source.as_bytes());
118            (
119                name.clone(),
120                Node {
121                    definition,
122                    identity,
123                    target: artifact
124                        .authored_path
125                        .clone()
126                        .expect("parsed definition PATH"),
127                    path: artifact.path.local().unwrap_or(Path::new("")).to_owned(),
128                    kind: artifact.kind(),
129                    target_missing: artifact.target_error.is_some(),
130                    parts: artifact.parts.iter().cloned().collect(),
131                    annotations: annotations::read(source, &config, name),
132                },
133            )
134        })
135        .collect();
136    Ok(Project { root, files, nodes })
137}