use alloc::string::{String, ToString};
use alloc::vec::Vec;
use crate::node::SlotRole;
use crate::serialize::drivers::{SpecIndex, StateIndex};
use crate::serialize::error::SerialValueError;
use crate::serialize::value::SerialValue;
use crate::source::{Span, TextContent};
use super::source::WireSpan;
use super::{
data_variant, expect_data_variant, expect_unit_variant, read_variant, unit_variant, FieldReader,
FieldWriter, FromSerialValue, ToSerialValue,
};
impl ToSerialValue for Span {
fn to_serial_value(&self) -> Result<SerialValue, SerialValueError> {
let mut writer = FieldWriter::with_capacity(2);
writer.field("start", &self.start())?;
writer.field("end", &self.end())?;
Ok(writer.finish())
}
}
impl FromSerialValue for Span {
fn from_serial_value(value: &SerialValue) -> Result<Self, SerialValueError> {
let reader = FieldReader::new(value, "Span", &["start", "end"])?;
let start: usize = reader.field("start")?;
let end: usize = reader.field("end")?;
if start > end {
return Err(SerialValueError::Custom(alloc::format!(
"Invalid byte range {start}..{end}: the start may not exceed the end"
)));
}
Ok(Span::new(start, end))
}
}
impl ToSerialValue for TextContent {
fn to_serial_value(&self) -> Result<SerialValue, SerialValueError> {
match self {
TextContent::Spanned(span) => Ok(data_variant("spanned", span.to_serial_value()?)),
TextContent::Owned(text) => Ok(data_variant("owned", SerialValue::Str(text.to_string()))),
}
}
}
impl FromSerialValue for TextContent {
fn from_serial_value(value: &SerialValue) -> Result<Self, SerialValueError> {
let (name, payload) = read_variant(value, "TextContent", &["spanned", "owned"])?;
match name {
"spanned" => Ok(TextContent::Spanned(Span::from_serial_value(expect_data_variant("spanned", payload)?)?)),
"owned" => {
let text = String::from_serial_value(expect_data_variant("owned", payload)?)?;
Ok(TextContent::Owned(text.into_boxed_str()))
}
_ => unreachable!("read_variant rejects other names"),
}
}
}
impl ToSerialValue for SlotRole {
fn to_serial_value(&self) -> Result<SerialValue, SerialValueError> {
let name = match self {
SlotRole::Content => "content",
SlotRole::Attached => "attached",
SlotRole::Hidden => "hidden",
};
Ok(unit_variant(name))
}
}
impl FromSerialValue for SlotRole {
fn from_serial_value(value: &SerialValue) -> Result<Self, SerialValueError> {
let (name, payload) = read_variant(value, "SlotRole", &["content", "attached", "hidden"])?;
expect_unit_variant(name, payload)?;
Ok(match name {
"content" => SlotRole::Content,
"attached" => SlotRole::Attached,
_ => SlotRole::Hidden,
})
}
}
#[derive(Debug, Clone, ToSerialValue, FromSerialValue)]
pub(crate) struct WireTree {
#[serial(name = "nodes")]
pub(crate) nodes: Vec<WireNode>,
#[serial(name = "annotations")]
pub(crate) annotations: Option<Vec<SerialValue>>,
}
#[derive(Debug, Clone, ToSerialValue, FromSerialValue)]
pub(crate) struct WireNode {
#[serial(name = "kind")]
pub(crate) kind: WireNodeKind,
#[serial(name = "span")]
pub(crate) span: WireSpan,
#[serial(name = "state")]
pub(crate) state: StateIndex,
#[serial(name = "ext")]
pub(crate) ext: SerialValue,
#[serial(name = "children")]
pub(crate) children: WireRange,
}
#[derive(Debug, Clone, ToSerialValue, FromSerialValue)]
pub(crate) enum WireNodeKind {
#[serial(name = "chars")]
Chars {
#[serial(name = "content")]
content: TextContent,
},
#[serial(name = "group")]
Group {
#[serial(name = "group_type")]
group_type: SerialValue,
#[serial(name = "open")]
open: TextContent,
#[serial(name = "close")]
close: TextContent,
},
#[serial(name = "callable")]
Callable {
#[serial(name = "callable_type")]
callable_type: SerialValue,
#[serial(name = "name")]
name: String,
#[serial(name = "spec")]
spec: SpecIndex,
#[serial(name = "arguments")]
arguments: Vec<WireArgument>,
#[serial(name = "slots")]
slots: Vec<WireSlot>,
#[serial(name = "invocation_syntax")]
invocation_syntax: SerialValue,
},
#[serial(name = "comment")]
Comment {
#[serial(name = "start")]
start: TextContent,
#[serial(name = "content")]
content: TextContent,
#[serial(name = "post_space")]
post_space: TextContent,
},
#[serial(name = "list")]
List,
}
#[derive(Debug, Clone, PartialEq, Eq, ToSerialValue, FromSerialValue)]
pub(crate) struct WireArgument {
#[serial(name = "region")]
pub(crate) region: Option<WireRegion>,
#[serial(name = "ext")]
pub(crate) ext: Option<SerialValue>,
#[serial(name = "spec_payload")]
pub(crate) spec_payload: Option<SerialValue>,
}
#[derive(Debug, Clone, PartialEq, Eq, ToSerialValue, FromSerialValue)]
pub(crate) struct WireSlot {
#[serial(name = "name")]
pub(crate) name: Option<String>,
#[serial(name = "region")]
pub(crate) region: WireRegion,
#[serial(name = "role")]
pub(crate) role: SlotRole,
#[serial(name = "ext")]
pub(crate) ext: SerialValue,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, ToSerialValue, FromSerialValue)]
pub(crate) struct WireRegion {
#[serial(name = "children")]
pub(crate) children: WireRange,
#[serial(name = "content")]
pub(crate) content: WireContent,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, ToSerialValue, FromSerialValue)]
pub(crate) enum WireContent {
#[serial(name = "in_region")]
InRegion {
#[serial(name = "start")]
start: u32,
#[serial(name = "end")]
end: u32,
},
#[serial(name = "in_children_of")]
InChildrenOf {
#[serial(name = "node")]
node: u32,
#[serial(name = "start")]
start: u32,
#[serial(name = "end")]
end: u32,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, ToSerialValue, FromSerialValue)]
pub(crate) struct WireRange {
#[serial(name = "start")]
pub(crate) start: u32,
#[serial(name = "end")]
pub(crate) end: u32,
}