extern crate alloc;
use alloc::borrow::Cow;
use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FieldLocationHint {
KeyValue,
Attribute,
Text,
Child,
Property,
Argument,
Tag,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldKey<'de> {
pub name: Cow<'de, str>,
pub location: FieldLocationHint,
pub namespace: Option<Cow<'de, str>>,
}
impl<'de> FieldKey<'de> {
pub fn new(name: impl Into<Cow<'de, str>>, location: FieldLocationHint) -> Self {
Self {
name: name.into(),
location,
namespace: None,
}
}
pub fn with_namespace(mut self, namespace: impl Into<Cow<'de, str>>) -> Self {
self.namespace = Some(namespace.into());
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ContainerKind {
Object,
Array,
Element,
}
impl ContainerKind {
pub fn is_ambiguous(self) -> bool {
matches!(self, ContainerKind::Element)
}
pub fn name(self) -> &'static str {
match self {
ContainerKind::Object => "object",
ContainerKind::Array => "array",
ContainerKind::Element => "element",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ValueTypeHint {
Null,
Bool,
Number,
String,
Bytes,
Sequence,
Map,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ScalarValue<'de> {
Null,
Bool(bool),
Char(char),
I64(i64),
U64(u64),
I128(i128),
U128(u128),
F64(f64),
Str(Cow<'de, str>),
Bytes(Cow<'de, [u8]>),
StringlyTyped(Cow<'de, str>),
}
#[derive(Clone, PartialEq)]
pub enum ParseEvent<'de> {
StructStart(ContainerKind),
StructEnd,
FieldKey(FieldKey<'de>),
OrderedField,
SequenceStart(ContainerKind),
SequenceEnd,
Scalar(ScalarValue<'de>),
VariantTag(&'de str),
}
impl<'de> fmt::Debug for ParseEvent<'de> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ParseEvent::StructStart(kind) => f.debug_tuple("StructStart").field(kind).finish(),
ParseEvent::StructEnd => f.write_str("StructEnd"),
ParseEvent::FieldKey(key) => f.debug_tuple("FieldKey").field(key).finish(),
ParseEvent::OrderedField => f.write_str("OrderedField"),
ParseEvent::SequenceStart(kind) => f.debug_tuple("SequenceStart").field(kind).finish(),
ParseEvent::SequenceEnd => f.write_str("SequenceEnd"),
ParseEvent::Scalar(value) => f.debug_tuple("Scalar").field(value).finish(),
ParseEvent::VariantTag(tag) => f.debug_tuple("VariantTag").field(tag).finish(),
}
}
}