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    pub annotations: Annotations,
43}
44#[derive(Debug, Serialize)]
45pub struct Project {
46    #[serde(skip)]
47    root: PathBuf,
48    pub files: BasedOn,
49    pub nodes: BTreeMap<ArtifactName, Node>,
50}
51/// Definition files a proposed operation changes, with their snapshot bases. A new
52/// destination has no current text. Paths are project-relative, exactly as in `BasedOn`.
53#[derive(Debug, Clone, Serialize)]
54pub struct Footprint {
55    #[serde(with = "crate::platform::path_serde::map")]
56    pub files: BTreeMap<PathBuf, Option<String>>,
57}
58
59/// Compute the exact write set from a `read_project` snapshot without writing files,
60/// acquiring edit locks or creating state. This is the same candidate edit as `apply`.
61/// Use its files as `BasedOn` and to guard unsaved Source drafts; destinations and
62/// reference rewrites belong to the library. Disk edits after the snapshot are still
63/// detected by `apply`'s compare-and-swap, never silently adopted as fresh bases.
64pub fn footprint(project: &Project, operation: &Operation) -> Result<Footprint, Refusal> {
65    let sources = project
66        .files
67        .iter()
68        .filter_map(|(file, source)| {
69            source
70                .as_ref()
71                .map(|source| (project.root.join(file), source.clone()))
72        })
73        .collect::<BTreeMap<_, _>>();
74    let proposed = proposal::propose(&project.root, &sources, operation.clone())?;
75    discovery::read_sources(project.root.clone(), &proposed)?;
76    let files = proposal::changes(&sources, &proposed)
77        .into_keys()
78        .map(|file| {
79            let base = sources.get(&file).cloned();
80            (
81                file.strip_prefix(&project.root)
82                    .expect("project definition")
83                    .to_owned(),
84                base,
85            )
86        })
87        .collect();
88    Ok(Footprint { files })
89}
90
91/// A file's exact content is its optimistic base; `identity` is SHA-256 for view clients.
92/// Invalid annotation values are ignored for display and described in their error fields.
93pub fn read_project(project: &Path) -> Result<Project, Refusal> {
94    let root = crate::platform::canonicalize(project).map_err(io)?;
95    let sources = discovery::sources(&root)?;
96    let mut config = discovery::read_sources(root.clone(), &sources)?;
97    discovery::inspect_targets(&mut config);
98    let files = sources
99        .into_iter()
100        .map(|(file, source)| {
101            (
102                file.strip_prefix(&root)
103                    .expect("definition below project")
104                    .to_owned(),
105                Some(source),
106            )
107        })
108        .collect::<BasedOn>();
109    let nodes = config
110        .artifacts
111        .iter()
112        .map(|(name, artifact)| {
113            let definition = artifact.declaration_path();
114            let source = files[&definition].as_deref().expect("definition text");
115            let identity = crate::types::Sha256Digest::of(source.as_bytes());
116            (
117                name.clone(),
118                Node {
119                    definition,
120                    identity,
121                    target: artifact
122                        .authored_path
123                        .clone()
124                        .expect("parsed definition PATH"),
125                    path: artifact.path.local().unwrap_or(Path::new("")).to_owned(),
126                    kind: artifact.kind(),
127                    target_missing: artifact.target_error.is_some(),
128                    annotations: annotations::read(source, &config, name),
129                },
130            )
131        })
132        .collect();
133    Ok(Project { root, files, nodes })
134}