#![allow(clippy::large_enum_variant, clippy::result_large_err)]
mod attribute;
mod block;
mod body;
pub use self::attribute::{Attribute, AttributeMut};
pub use self::block::{Block, BlockBuilder, BlockLabel};
pub use self::body::{
Attributes, AttributesMut, Blocks, BlocksMut, Body, BodyBuilder, IntoAttributes, IntoBlocks,
IntoIter, Iter, IterMut,
};
use crate::{Decor, Decorate, Span};
use std::ops::Range;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Structure {
Attribute(Attribute),
Block(Block),
}
impl Structure {
pub fn is_attribute(&self) -> bool {
self.as_attribute().is_some()
}
pub fn is_block(&self) -> bool {
self.as_block().is_some()
}
pub fn into_attribute(self) -> Result<Attribute, Structure> {
match self {
Structure::Attribute(attr) => Ok(attr),
Structure::Block(_) => Err(self),
}
}
pub fn as_attribute(&self) -> Option<&Attribute> {
match self {
Structure::Attribute(attr) => Some(attr),
Structure::Block(_) => None,
}
}
pub fn as_attribute_mut(&mut self) -> Option<&mut Attribute> {
match self {
Structure::Attribute(attr) => Some(attr),
Structure::Block(_) => None,
}
}
pub fn into_block(self) -> Result<Block, Structure> {
match self {
Structure::Block(block) => Ok(block),
Structure::Attribute(_) => Err(self),
}
}
pub fn as_block(&self) -> Option<&Block> {
match self {
Structure::Block(block) => Some(block),
Structure::Attribute(_) => None,
}
}
pub fn as_block_mut(&mut self) -> Option<&mut Block> {
match self {
Structure::Block(block) => Some(block),
Structure::Attribute(_) => None,
}
}
pub(crate) fn despan(&mut self, input: &str) {
match self {
Structure::Attribute(attr) => attr.despan(input),
Structure::Block(block) => block.despan(input),
}
}
}
impl From<Attribute> for Structure {
fn from(value: Attribute) -> Self {
Structure::Attribute(value)
}
}
impl From<Block> for Structure {
fn from(value: Block) -> Self {
Structure::Block(value)
}
}
forward_decorate_impl!(Structure => { Attribute, Block });
forward_span_impl!(Structure => { Attribute, Block });
pub struct StructureMut<'a> {
structure: &'a mut Structure,
}
impl<'a> StructureMut<'a> {
pub(crate) fn new(structure: &'a mut Structure) -> StructureMut<'a> {
StructureMut { structure }
}
pub fn is_attribute(&self) -> bool {
self.as_attribute().is_some()
}
pub fn is_block(&self) -> bool {
self.as_block().is_some()
}
pub fn as_attribute(&self) -> Option<&Attribute> {
self.structure.as_attribute()
}
pub fn as_attribute_mut(&mut self) -> Option<AttributeMut<'_>> {
self.structure.as_attribute_mut().map(AttributeMut::new)
}
pub fn as_block(&self) -> Option<&Block> {
self.structure.as_block()
}
pub fn as_block_mut(&mut self) -> Option<&mut Block> {
self.structure.as_block_mut()
}
}
impl Decorate for StructureMut<'_> {
fn decor(&self) -> &Decor {
self.structure.decor()
}
fn decor_mut(&mut self) -> &mut Decor {
self.structure.decor_mut()
}
}
impl Span for StructureMut<'_> {
fn span(&self) -> Option<Range<usize>> {
self.structure.span()
}
}