artifactize_core/config/edit/
snapshot.rs1use super::*;
4
5#[derive(Debug, Default, Serialize, Deserialize)]
6pub struct Layout {
7 pub nodes: BTreeMap<PlacementKey, [f64; 2]>,
8}
9pub 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 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#[derive(Debug, Clone, Serialize)]
54pub struct Footprint {
55 #[serde(with = "crate::platform::path_serde::map")]
56 pub files: BTreeMap<PathBuf, Option<String>>,
57}
58
59pub 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
91pub 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}