sdf_metadata/metadata/operator/
step_state.rs1use anyhow::Result;
2
3use crate::wit::{operator::StepState, states::StateTyped};
4
5impl StepState {
6 pub fn is_resolved(&self) -> bool {
7 matches!(self, Self::Resolved(_))
8 }
9 pub fn resolve(&mut self, states: &[StateTyped]) -> Result<()> {
10 match self {
11 Self::Resolved(_) => return Ok(()),
12 Self::Unresolved(imported_state) => {
13 let state = states
14 .iter()
15 .find(|state| state.name == *imported_state.name);
16 if let Some(state) = state {
17 *self = Self::Resolved(state.clone());
18 } else {
19 return Err(anyhow::anyhow!(
20 "Could not resolve state: {}",
21 imported_state.name
22 ));
23 }
24 }
25 }
26 Ok(())
27 }
28
29 pub fn name(&self) -> &str {
30 match self {
31 Self::Resolved(state) => &state.name,
32 Self::Unresolved(imported_state) => &imported_state.name,
33 }
34 }
35}
36
37#[cfg(test)]
38mod test {
39 use crate::wit::{
40 metadata::{SdfKeyedState, SdfKeyedStateValue, TypeRef},
41 operator::{StateImport, StepState},
42 states::StateTyped,
43 };
44
45 #[test]
46 fn test_resolve_step_state() {
47 let mut step_state = StepState::Unresolved(StateImport {
48 name: "my-imported-state".to_string(),
49 });
50
51 let states = vec![StateTyped {
52 name: "my-imported-state".to_string(),
53 type_: SdfKeyedState {
54 key: TypeRef {
55 name: "string".to_string(),
56 },
57 value: SdfKeyedStateValue::U32,
58 },
59 }];
60
61 step_state.resolve(&states).unwrap();
62
63 assert!(step_state.is_resolved());
64
65 if let StepState::Resolved(state) = step_state {
66 assert_eq!(state.name, "my-imported-state");
67 assert_eq!(state.type_.key.name, "string");
68 assert_eq!(state.type_.value, SdfKeyedStateValue::U32);
69 } else {
70 panic!("Expected StepState::Resolved");
71 }
72 }
73}