use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub enum ExtractionMode {
#[default]
Standard,
SharedState,
}
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Serialize,
Deserialize,
bitcode::Encode,
bitcode::Decode,
)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum StateOperation {
Read,
Assign,
ReadModifyWrite,
Update,
Delete,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, bitcode::Encode, bitcode::Decode)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum StateRoot {
Import {
import_index: u32,
},
Global {
name: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, bitcode::Encode, bitcode::Decode)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct StateAccessFact {
pub root: StateRoot,
pub path: Vec<String>,
pub operation: StateOperation,
pub span_start: u32,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct StateSite {
#[serde(serialize_with = "crate::serde_path::serialize")]
pub path: PathBuf,
pub line: usize,
pub column: usize,
pub offset: u32,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct StateAction {
pub description: String,
pub auto_fixable: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct ImportedStateWrite {
pub id: String,
pub writer: StateSite,
pub declaration: StateSite,
pub binding: String,
pub property: Vec<String>,
pub operation: StateOperation,
pub actions: Vec<StateAction>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum UnknownStateRelation {
Unknown,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct GlobalStateAccess {
pub site: StateSite,
pub property: Vec<String>,
pub operation: StateOperation,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct GlobalStateGroup {
pub id: String,
pub root: String,
pub slot: String,
pub reads: Vec<GlobalStateAccess>,
pub writes: Vec<GlobalStateAccess>,
pub same_runtime: UnknownStateRelation,
pub execution_order: UnknownStateRelation,
pub actions: Vec<StateAction>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct SharedStateInventory {
#[cfg_attr(feature = "schema", schemars(range(min = 1, max = 1)))]
pub version: u32,
pub complete: bool,
pub truncated: bool,
pub imported_writes: Vec<ImportedStateWrite>,
pub globals: Vec<GlobalStateGroup>,
}
pub(crate) fn merge_inventory(
target: &mut Option<SharedStateInventory>,
other: Option<SharedStateInventory>,
) {
let Some(other) = other else { return };
let Some(target) = target else {
*target = Some(other);
return;
};
target.complete &= other.complete;
target.truncated |= other.truncated;
target.imported_writes.extend(other.imported_writes);
target
.imported_writes
.sort_by(|left, right| left.id.cmp(&right.id));
target.imported_writes.dedup();
target.globals.extend(other.globals);
target.globals.sort_by(|left, right| left.id.cmp(&right.id));
target.globals.dedup();
}
impl SharedStateInventory {
pub fn retain_sites(&mut self, keep: impl Fn(&StateSite) -> bool) {
self.imported_writes.retain(|write| keep(&write.writer));
self.globals.retain(|group| {
group
.reads
.iter()
.chain(&group.writes)
.any(|access| keep(&access.site))
});
}
}
impl StateOperation {
#[must_use]
pub const fn label(self) -> &'static str {
match self {
Self::Read => "read",
Self::Assign => "assign",
Self::ReadModifyWrite => "read_modify_write",
Self::Update => "update",
Self::Delete => "delete",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn typed_state_sites_normalize_paths_without_changing_property_keys() {
let write = ImportedStateWrite {
id: "observation".to_owned(),
writer: StateSite {
path: r"src\writer.ts".into(),
line: 2,
column: 3,
offset: 8,
},
declaration: StateSite {
path: r"src\owner.ts".into(),
line: 1,
column: 1,
offset: 0,
},
binding: "state".to_owned(),
property: vec![r"literal\key".to_owned(), "a.b".to_owned()],
operation: StateOperation::Assign,
actions: Vec::new(),
};
let json = serde_json::to_value(&write).unwrap();
assert_eq!(json["writer"]["path"], "src/writer.ts");
assert_eq!(json["declaration"]["path"], "src/owner.ts");
assert_eq!(json["property"], serde_json::json!([r"literal\key", "a.b"]));
assert_eq!(json["writer"]["offset"], 8);
}
}