use core::fmt;
use alloc::boxed::Box;
use alloc::sync::Arc;
use alloc::vec::Vec;
use crate::node::{ArgumentExt, BuildId, ContentNodes, SlotExt, SlotRole};
use crate::spec::ArgumentSpec;
use crate::state::Lang;
pub struct RestagedArgument<L: Lang> {
pub(super) spec: Arc<ArgumentSpec<L>>,
pub(super) provided: Option<ProvidedRegion<L>>,
}
pub(super) struct ProvidedRegion<L: Lang> {
pub(super) nodes: Vec<BuildId>,
pub(super) content: ContentNodes,
pub(super) ext: Option<ArgumentExt<L>>,
}
impl<L: Lang> RestagedArgument<L> {
pub fn provided(
spec: Arc<ArgumentSpec<L>>,
nodes: Vec<BuildId>,
content: ContentNodes,
ext: ArgumentExt<L>,
) -> RestagedArgument<L> {
RestagedArgument {
spec,
provided: Some(ProvidedRegion { nodes, content, ext: Some(ext) }),
}
}
pub fn absent(spec: Arc<ArgumentSpec<L>>) -> RestagedArgument<L> {
RestagedArgument { spec, provided: None }
}
pub fn spec(&self) -> &Arc<ArgumentSpec<L>> {
&self.spec
}
pub fn is_provided(&self) -> bool {
self.provided.is_some()
}
pub fn nodes(&self) -> &[BuildId] {
self.provided.as_ref().map_or(&[], |region| ®ion.nodes)
}
pub fn content(&self) -> Option<&ContentNodes> {
self.provided.as_ref().map(|region| ®ion.content)
}
pub fn ext(&self) -> Option<&ArgumentExt<L>> {
self.provided.as_ref().and_then(|region| region.ext.as_ref())
}
}
impl<L: Lang> fmt::Debug for RestagedArgument<L> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut s = f.debug_struct("RestagedArgument");
s.field("spec", &self.spec);
match &self.provided {
Some(region) => s
.field("nodes", ®ion.nodes)
.field("content", ®ion.content)
.field("ext", ®ion.ext),
None => s.field("provided", &false),
};
s.finish()
}
}
pub struct RestagedSlot<L: Lang> {
pub(super) name: Option<Box<str>>,
pub(super) role: SlotRole,
pub(super) nodes: Vec<BuildId>,
pub(super) content: ContentNodes,
pub(super) ext: SlotExt<L>,
}
impl<L: Lang> RestagedSlot<L> {
pub fn new(
name: impl Into<Box<str>>,
role: SlotRole,
nodes: Vec<BuildId>,
content: ContentNodes,
ext: SlotExt<L>,
) -> RestagedSlot<L> {
RestagedSlot { name: Some(name.into()), role, nodes, content, ext }
}
pub fn new_unnamed(
role: SlotRole,
nodes: Vec<BuildId>,
content: ContentNodes,
ext: SlotExt<L>,
) -> RestagedSlot<L> {
RestagedSlot { name: None, role, nodes, content, ext }
}
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}
pub fn role(&self) -> SlotRole {
self.role
}
pub fn nodes(&self) -> &[BuildId] {
&self.nodes
}
pub fn content(&self) -> &ContentNodes {
&self.content
}
pub fn ext(&self) -> &SlotExt<L> {
&self.ext
}
}
impl<L: Lang> fmt::Debug for RestagedSlot<L> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RestagedSlot")
.field("name", &self.name)
.field("role", &self.role)
.field("nodes", &self.nodes)
.field("content", &self.content)
.field("ext", &self.ext)
.finish()
}
}