use typesayer_types::field::{FieldType, FieldValue};
#[derive(Debug, Clone, PartialEq)]
pub enum ParseEvent {
StreamStart { output_schema_hash: String },
FieldStart { path: String, field_type: FieldType },
ValueAppend { path: String, delta: String },
ValueSet { path: String, value: FieldValue },
ListOpen { path: String },
ElementAdded {
path: String,
index: usize,
value: FieldValue,
},
ListClose { path: String, value: FieldValue },
ObjectOpen { path: String },
EntryAdded {
path: String,
key: String,
value: FieldValue,
},
ObjectClose { path: String, value: FieldValue },
FieldComplete { path: String, value: FieldValue },
StreamComplete { output: FieldValue },
StreamError {
path: Option<String>,
message: String,
},
}
impl ParseEvent {
#[must_use]
pub const fn event_type(&self) -> &'static str {
match self {
Self::StreamStart { .. } => "stream_start",
Self::FieldStart { .. } => "field_start",
Self::ValueAppend { .. } => "value_append",
Self::ValueSet { .. } => "value_set",
Self::ListOpen { .. } => "list_open",
Self::ElementAdded { .. } => "element_added",
Self::ListClose { .. } => "list_close",
Self::ObjectOpen { .. } => "object_open",
Self::EntryAdded { .. } => "entry_added",
Self::ObjectClose { .. } => "object_close",
Self::FieldComplete { .. } => "field_complete",
Self::StreamComplete { .. } => "stream_complete",
Self::StreamError { .. } => "stream_error",
}
}
#[must_use]
pub const fn is_durable(&self) -> bool {
match self {
Self::ValueAppend { .. }
| Self::ValueSet { .. }
| Self::ElementAdded { .. }
| Self::EntryAdded { .. } => false,
Self::StreamStart { .. }
| Self::FieldStart { .. }
| Self::ListOpen { .. }
| Self::ListClose { .. }
| Self::ObjectOpen { .. }
| Self::ObjectClose { .. }
| Self::FieldComplete { .. }
| Self::StreamComplete { .. }
| Self::StreamError { .. } => true,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn event_type_distinct_per_variant() {
let events = [
ParseEvent::StreamStart {
output_schema_hash: String::new(),
},
ParseEvent::FieldStart {
path: "/x".into(),
field_type: FieldType::String,
},
ParseEvent::ValueAppend {
path: "/x".into(),
delta: String::new(),
},
ParseEvent::ValueSet {
path: "/x".into(),
value: FieldValue::Null,
},
ParseEvent::ListOpen { path: "/x".into() },
ParseEvent::ElementAdded {
path: "/x".into(),
index: 0,
value: FieldValue::Null,
},
ParseEvent::ListClose {
path: "/x".into(),
value: FieldValue::List(vec![]),
},
ParseEvent::ObjectOpen { path: "/x".into() },
ParseEvent::EntryAdded {
path: "/x".into(),
key: String::new(),
value: FieldValue::Null,
},
ParseEvent::ObjectClose {
path: "/x".into(),
value: FieldValue::Object(std::collections::BTreeMap::new()),
},
ParseEvent::FieldComplete {
path: "/x".into(),
value: FieldValue::Null,
},
ParseEvent::StreamComplete {
output: FieldValue::Object(std::collections::BTreeMap::new()),
},
ParseEvent::StreamError {
path: None,
message: String::new(),
},
];
let types: std::collections::HashSet<_> =
events.iter().map(ParseEvent::event_type).collect();
assert_eq!(
types.len(),
events.len(),
"every variant must have a unique event_type"
);
}
#[test]
fn durability_split_matches_design() {
assert!(
!ParseEvent::ValueAppend {
path: "/x".into(),
delta: String::new()
}
.is_durable()
);
assert!(
!ParseEvent::ValueSet {
path: "/x".into(),
value: FieldValue::Null
}
.is_durable()
);
assert!(
!ParseEvent::ElementAdded {
path: "/x".into(),
index: 0,
value: FieldValue::Null
}
.is_durable()
);
assert!(
!ParseEvent::EntryAdded {
path: "/x".into(),
key: String::new(),
value: FieldValue::Null,
}
.is_durable()
);
for ev in [
ParseEvent::StreamStart {
output_schema_hash: String::new(),
},
ParseEvent::FieldStart {
path: "/x".into(),
field_type: FieldType::String,
},
ParseEvent::ListOpen { path: "/x".into() },
ParseEvent::ListClose {
path: "/x".into(),
value: FieldValue::List(vec![]),
},
ParseEvent::ObjectOpen { path: "/x".into() },
ParseEvent::ObjectClose {
path: "/x".into(),
value: FieldValue::Object(std::collections::BTreeMap::new()),
},
ParseEvent::FieldComplete {
path: "/x".into(),
value: FieldValue::Null,
},
ParseEvent::StreamComplete {
output: FieldValue::Object(std::collections::BTreeMap::new()),
},
ParseEvent::StreamError {
path: None,
message: String::new(),
},
] {
assert!(ev.is_durable(), "expected durable: {ev:?}");
}
}
}