1use serde::{Deserialize, Serialize};
4use std::path::PathBuf;
5
6#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
8pub enum ExtractionMode {
9 #[default]
11 Standard,
12 SharedState,
14}
15
16#[derive(
18 Debug,
19 Clone,
20 Copy,
21 PartialEq,
22 Eq,
23 PartialOrd,
24 Ord,
25 Serialize,
26 Deserialize,
27 bitcode::Encode,
28 bitcode::Decode,
29)]
30#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
31#[serde(rename_all = "snake_case")]
32pub enum StateOperation {
33 Read,
35 Assign,
37 ReadModifyWrite,
39 Update,
41 Delete,
43}
44
45#[derive(Debug, Clone, PartialEq, Eq, Serialize, bitcode::Encode, bitcode::Decode)]
47#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
48#[serde(tag = "kind", rename_all = "snake_case")]
49pub enum StateRoot {
50 Import {
52 import_index: u32,
54 },
55 Global {
57 name: String,
59 },
60}
61
62#[derive(Debug, Clone, PartialEq, Eq, Serialize, bitcode::Encode, bitcode::Decode)]
64#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
65pub struct StateAccessFact {
66 pub root: StateRoot,
68 pub path: Vec<String>,
70 pub operation: StateOperation,
72 pub span_start: u32,
74}
75
76#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
78#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
79#[serde(deny_unknown_fields)]
80pub struct StateSite {
81 #[serde(serialize_with = "crate::serde_path::serialize")]
83 pub path: PathBuf,
84 pub line: usize,
86 pub column: usize,
88 pub offset: u32,
90}
91
92#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
94#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
95#[serde(deny_unknown_fields)]
96pub struct StateAction {
97 pub description: String,
99 pub auto_fixable: bool,
101}
102
103#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
105#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
106#[serde(deny_unknown_fields)]
107pub struct ImportedStateWrite {
108 pub id: String,
110 pub writer: StateSite,
112 pub declaration: StateSite,
114 pub binding: String,
116 pub property: Vec<String>,
118 pub operation: StateOperation,
120 pub actions: Vec<StateAction>,
122}
123
124#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
126#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
127#[serde(rename_all = "snake_case")]
128pub enum UnknownStateRelation {
129 Unknown,
131}
132
133#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
135#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
136#[serde(deny_unknown_fields)]
137pub struct GlobalStateAccess {
138 pub site: StateSite,
140 pub property: Vec<String>,
142 pub operation: StateOperation,
144}
145
146#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
148#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
149#[serde(deny_unknown_fields)]
150pub struct GlobalStateGroup {
151 pub id: String,
153 pub root: String,
155 pub slot: String,
157 pub reads: Vec<GlobalStateAccess>,
159 pub writes: Vec<GlobalStateAccess>,
161 pub same_runtime: UnknownStateRelation,
163 pub execution_order: UnknownStateRelation,
165 pub actions: Vec<StateAction>,
167}
168
169#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
171#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
172#[serde(deny_unknown_fields)]
173pub struct SharedStateInventory {
174 #[cfg_attr(feature = "schema", schemars(range(min = 1, max = 1)))]
176 pub version: u32,
177 pub complete: bool,
179 pub truncated: bool,
181 pub imported_writes: Vec<ImportedStateWrite>,
183 pub globals: Vec<GlobalStateGroup>,
185}
186
187pub(crate) fn merge_inventory(
189 target: &mut Option<SharedStateInventory>,
190 other: Option<SharedStateInventory>,
191) {
192 let Some(other) = other else { return };
193 let Some(target) = target else {
194 *target = Some(other);
195 return;
196 };
197 target.complete &= other.complete;
198 target.truncated |= other.truncated;
199 target.imported_writes.extend(other.imported_writes);
200 target
201 .imported_writes
202 .sort_by(|left, right| left.id.cmp(&right.id));
203 target.imported_writes.dedup();
204 target.globals.extend(other.globals);
206 target.globals.sort_by(|left, right| left.id.cmp(&right.id));
207 target.globals.dedup();
208}
209
210impl SharedStateInventory {
211 pub fn retain_sites(&mut self, keep: impl Fn(&StateSite) -> bool) {
213 self.imported_writes.retain(|write| keep(&write.writer));
214 self.globals.retain(|group| {
215 group
216 .reads
217 .iter()
218 .chain(&group.writes)
219 .any(|access| keep(&access.site))
220 });
221 }
222}
223
224impl StateOperation {
225 #[must_use]
227 pub const fn label(self) -> &'static str {
228 match self {
229 Self::Read => "read",
230 Self::Assign => "assign",
231 Self::ReadModifyWrite => "read_modify_write",
232 Self::Update => "update",
233 Self::Delete => "delete",
234 }
235 }
236}
237
238#[cfg(test)]
239mod tests {
240 use super::*;
241
242 #[test]
243 fn typed_state_sites_normalize_paths_without_changing_property_keys() {
244 let write = ImportedStateWrite {
245 id: "observation".to_owned(),
246 writer: StateSite {
247 path: r"src\writer.ts".into(),
248 line: 2,
249 column: 3,
250 offset: 8,
251 },
252 declaration: StateSite {
253 path: r"src\owner.ts".into(),
254 line: 1,
255 column: 1,
256 offset: 0,
257 },
258 binding: "state".to_owned(),
259 property: vec![r"literal\key".to_owned(), "a.b".to_owned()],
260 operation: StateOperation::Assign,
261 actions: Vec::new(),
262 };
263 let json = serde_json::to_value(&write).unwrap();
264 assert_eq!(json["writer"]["path"], "src/writer.ts");
265 assert_eq!(json["declaration"]["path"], "src/owner.ts");
266 assert_eq!(json["property"], serde_json::json!([r"literal\key", "a.b"]));
267 assert_eq!(json["writer"]["offset"], 8);
268 }
269}