extern crate alloc;
use alloc::borrow::Cow;
use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FieldLocationHint {
#[default]
KeyValue,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldKey<'de> {
pub name: Option<Cow<'de, str>>,
pub location: FieldLocationHint,
pub doc: Option<Vec<Cow<'de, str>>>,
pub tag: Option<Cow<'de, str>>,
}
impl<'de> FieldKey<'de> {
pub fn new(name: impl Into<Cow<'de, str>>, location: FieldLocationHint) -> Self {
Self {
name: Some(name.into()),
location,
doc: None,
tag: None,
}
}
pub fn with_doc(
name: impl Into<Cow<'de, str>>,
location: FieldLocationHint,
doc: Vec<Cow<'de, str>>,
) -> Self {
Self {
name: Some(name.into()),
location,
doc: if doc.is_empty() { None } else { Some(doc) },
tag: None,
}
}
pub fn tagged(
tag: impl Into<Cow<'de, str>>,
location: FieldLocationHint,
) -> Self {
Self {
name: None,
location,
doc: None,
tag: Some(tag.into()),
}
}
pub fn tagged_with_doc(
tag: impl Into<Cow<'de, str>>,
location: FieldLocationHint,
doc: Vec<Cow<'de, str>>,
) -> Self {
Self {
name: None,
location,
doc: if doc.is_empty() { None } else { Some(doc) },
tag: Some(tag.into()),
}
}
pub fn unit(location: FieldLocationHint) -> Self {
Self {
name: None,
location,
doc: None,
tag: Some(Cow::Borrowed("")),
}
}
pub fn unit_with_doc(location: FieldLocationHint, doc: Vec<Cow<'de, str>>) -> Self {
Self {
name: None,
location,
doc: if doc.is_empty() { None } else { Some(doc) },
tag: Some(Cow::Borrowed("")),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ContainerKind {
Object,
Array,
}
impl ContainerKind {
pub const fn name(self) -> &'static str {
match self {
ContainerKind::Object => "object",
ContainerKind::Array => "array",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ValueTypeHint {
Null,
Bool,
Number,
String,
Bytes,
Sequence,
Map,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ScalarValue<'de> {
Unit,
Null,
Bool(bool),
Char(char),
I64(i64),
U64(u64),
I128(i128),
U128(u128),
F64(f64),
Str(Cow<'de, str>),
Bytes(Cow<'de, [u8]>),
}
#[derive(Clone, PartialEq)]
pub enum ParseEvent<'de> {
StructStart(ContainerKind),
StructEnd,
FieldKey(FieldKey<'de>),
OrderedField,
SequenceStart(ContainerKind),
SequenceEnd,
Scalar(ScalarValue<'de>),
VariantTag(Option<&'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(),
}
}
}