sdf_metadata/metadata/states/
state.rs1use crate::{
2 wit::states::State,
3 util::{validate::SimpleValidate, validation_error::ValidationError},
4};
5
6impl State {
7 pub fn name(&self) -> &str {
8 match self {
9 State::Typed(state_typed) => &state_typed.name,
10 State::Reference(ref_state) => &ref_state.name,
11 State::System(system_state) => &system_state.name,
12 }
13 }
14
15 pub fn is_owned(&self) -> bool {
16 matches!(self, State::Typed(_))
17 }
18}
19
20impl SimpleValidate for State {
21 fn validate(&self) -> Result<(), ValidationError> {
22 match self {
23 State::Typed(state_typed) => state_typed.validate(),
24 State::Reference(ref_state) => ref_state.validate(),
25 State::System(system_state) => system_state.validate(),
26 }
27 }
28}
29
30#[cfg(test)]
31mod test {
32 use crate::{
33 wit::{
34 io::TypeRef,
35 metadata::{SdfKeyedState, SdfKeyedStateValue},
36 operator::StateTyped,
37 states::{StateRef, SystemState},
38 },
39 util::{validate::SimpleValidate, validation_error::ValidationError},
40 };
41
42 #[test]
43 fn test_validate_asserts_typed_state_is_resolved() {
44 let state = StateTyped {
45 name: "state".to_string(),
46 type_: SdfKeyedState {
47 key: TypeRef {
48 name: "string".to_string(),
49 },
50 value: SdfKeyedStateValue::Unresolved(TypeRef {
51 name: "my-state-value".to_string(),
52 }),
53 },
54 };
55
56 assert_eq!(
57 state.validate(),
58 Err(ValidationError::new(
59 "Internal Error: typed state value should be resolved before validation. Please contact support"
60 ))
61 );
62 }
63
64 #[test]
65 fn test_validate_validates_ref_state_values_are_not_empty() {
66 let state = StateRef {
67 name: "state".to_string(),
68 ref_service: "".to_string(),
69 };
70
71 assert_eq!(
72 state.validate(),
73 Err(ValidationError::new(
74 "service name missing for state reference. state reference must be of the form <service>.<state>"
75 ))
76 );
77 }
78
79 #[test]
80 fn test_validate_validates_system_state_values_are_not_empty() {
81 let state = SystemState {
82 name: "".to_string(),
83 system: "".to_string(),
84 };
85
86 assert_eq!(
87 state.validate(),
88 Err(ValidationError::new(
89 "empty system state found. state name and system cannot be empty"
90 ))
91 );
92 }
93}