mod contract;
mod node;
mod sourced;
use std::borrow::Cow;
use std::marker::PhantomData;
use oneyaml_core::{Diagnostic, Span};
#[cfg(doc)]
use oneyaml_core::Severity;
pub use contract::{ContractError, Violation, check_contract, check_tiling};
pub use node::NodeInfo;
pub use sourced::{EventList, IntoSourced, Sourced, SourcedIter};
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Event<'a> {
kind: EventKind,
span: Span,
source: PhantomData<&'a str>,
}
impl<'a> Event<'a> {
#[must_use]
pub const fn new(kind: EventKind, span: Span) -> Self {
Self {
kind,
span,
source: PhantomData,
}
}
#[must_use]
pub fn scalar(
span: Span,
style: ScalarStyle,
props: Properties,
block: Option<BlockHeader>,
) -> Self {
let info = NodeInfo::new(span.start, props, block);
Self::new(EventKind::Scalar { style, info }, span)
}
#[must_use]
pub fn mapping_start(span: Span, style: CollectionStyle, props: Properties) -> Self {
let info = NodeInfo::new(span.start, props, None);
Self::new(EventKind::MappingStart { style, info }, span)
}
#[must_use]
pub fn sequence_start(span: Span, style: CollectionStyle, props: Properties) -> Self {
let info = NodeInfo::new(span.start, props, None);
Self::new(EventKind::SequenceStart { style, info }, span)
}
#[must_use]
pub const fn kind(&self) -> &EventKind {
&self.kind
}
#[must_use]
pub fn into_kind(self) -> EventKind {
self.kind
}
#[must_use]
pub const fn span(&self) -> Span {
self.span
}
#[must_use]
pub(crate) fn source<'s>(&self, source: &'s str) -> Option<&'s str> {
source.get(self.span.start..self.span.end)
}
#[must_use]
pub const fn is_trivia(&self) -> bool {
matches!(self.kind, EventKind::Trivia(_))
}
#[must_use]
pub const fn is_error(&self) -> bool {
matches!(self.kind, EventKind::Error(_))
}
#[must_use]
pub const fn is_warning(&self) -> bool {
matches!(self.kind, EventKind::Warning(_))
}
#[must_use]
pub fn diagnostic(&self) -> Option<&Diagnostic> {
match &self.kind {
EventKind::Error(d) | EventKind::Warning(d) => Some(d),
_ => None,
}
}
pub(crate) fn set_info(&mut self, new: NodeInfo) {
if let EventKind::Scalar { info, .. }
| EventKind::MappingStart { info, .. }
| EventKind::SequenceStart { info, .. } = &mut self.kind
{
*info = new;
}
}
#[must_use]
pub const fn node_info(&self) -> Option<&NodeInfo> {
match &self.kind {
EventKind::Scalar { info, .. }
| EventKind::MappingStart { info, .. }
| EventKind::SequenceStart { info, .. } => Some(info),
_ => None,
}
}
#[must_use]
pub fn properties(&self) -> Option<Properties> {
self.node_info().map(|i| i.properties(self.span.start))
}
#[must_use]
pub(crate) fn anchor(&self, source: &'a str) -> Option<Anchor<'a>> {
crate::parser::anchor_at(source, self.properties()?.anchor?)
}
#[must_use]
pub(crate) fn tag(&self, source: &'a str) -> Option<Tag<'a>> {
self.tag_source(source)?.resolve()
}
#[must_use]
pub(crate) fn tag_source(&self, source: &'a str) -> Option<TagSource<'a>> {
let props = self.properties()?;
crate::parser::tag_source_at(source, props.tag?, props.tag_prefix)
}
#[must_use]
pub fn block_header(&self) -> Option<BlockHeader> {
match &self.kind {
EventKind::Scalar { info, .. } => info.block_header(self.span.start),
_ => None,
}
}
#[must_use]
pub(crate) fn value(&self, source: &'a str) -> Option<Cow<'a, str>> {
let EventKind::Scalar { style, .. } = self.kind else {
return None;
};
crate::parser::scalar_value(source, self.span, style, self.block_header())
}
#[must_use]
pub(crate) fn alias_name(&self, source: &'a str) -> Option<&'a str> {
match self.kind {
EventKind::Alias => self.source(source)?.strip_prefix('*'),
_ => None,
}
}
#[must_use]
pub(crate) fn directive(&self, source: &'a str) -> Option<DirectiveKind<'a>> {
match self.kind {
EventKind::Directive(_) => crate::parser::directive_at(source, self.span),
_ => None,
}
}
}
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum EventKind {
StreamStart,
StreamEnd,
Directive(Directive),
#[non_exhaustive]
DocumentStart {
explicit: bool,
},
#[non_exhaustive]
DocumentEnd {
explicit: bool,
},
#[non_exhaustive]
MappingStart {
style: CollectionStyle,
info: NodeInfo,
},
MappingEnd,
#[non_exhaustive]
SequenceStart {
style: CollectionStyle,
info: NodeInfo,
},
SequenceEnd,
#[non_exhaustive]
Scalar {
style: ScalarStyle,
info: NodeInfo,
},
Alias,
Trivia(TriviaKind),
Warning(Box<Diagnostic>),
Error(Box<Diagnostic>),
}
impl EventKind {
#[must_use]
pub const fn document_start(explicit: bool) -> Self {
Self::DocumentStart { explicit }
}
#[must_use]
pub const fn document_end(explicit: bool) -> Self {
Self::DocumentEnd { explicit }
}
#[must_use]
pub const fn mapping_start(style: CollectionStyle) -> Self {
Self::MappingStart {
style,
info: NodeInfo::none(),
}
}
#[must_use]
pub const fn sequence_start(style: CollectionStyle) -> Self {
Self::SequenceStart {
style,
info: NodeInfo::none(),
}
}
#[must_use]
pub const fn scalar(style: ScalarStyle) -> Self {
Self::Scalar {
style,
info: NodeInfo::none(),
}
}
#[must_use]
pub const fn plain() -> Self {
Self::scalar(ScalarStyle::Plain)
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum CollectionStyle {
Block,
Flow,
FlowPair,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ScalarStyle {
Plain,
SingleQuoted,
DoubleQuoted,
Literal,
Folded,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Chomping {
Strip,
Clip,
Keep,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct BlockHeader {
pub chomping: Chomping,
pub indent_indicator: Option<u8>,
pub content_indent: usize,
pub span: Span,
}
impl BlockHeader {
#[must_use]
pub const fn new(
chomping: Chomping,
indent_indicator: Option<u8>,
content_indent: usize,
span: Span,
) -> Self {
Self {
chomping,
indent_indicator,
content_indent,
span,
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Properties {
pub anchor: Option<usize>,
pub tag: Option<usize>,
pub tag_prefix: Option<usize>,
}
impl Properties {
#[must_use]
pub const fn new(anchor: Option<usize>, tag: Option<usize>, tag_prefix: Option<usize>) -> Self {
Self {
anchor,
tag,
tag_prefix,
}
}
#[must_use]
pub const fn none() -> Self {
Self::new(None, None, None)
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.anchor.is_none() && self.tag.is_none()
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Anchor<'a> {
pub name: &'a str,
pub span: Span,
}
impl<'a> Anchor<'a> {
#[must_use]
pub const fn new(name: &'a str, span: Span) -> Self {
Self { name, span }
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TagForm {
Verbatim,
Shorthand,
NonSpecific,
}
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Tag<'a> {
pub form: TagForm,
pub handle: &'a str,
pub suffix: &'a str,
pub resolved: Cow<'a, str>,
pub span: Span,
}
impl<'a> Tag<'a> {
#[must_use]
pub const fn new(
form: TagForm,
handle: &'a str,
suffix: &'a str,
resolved: Cow<'a, str>,
span: Span,
) -> Self {
Self {
form,
handle,
suffix,
resolved,
span,
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug)]
pub struct TagSource<'a> {
pub form: TagForm,
pub handle: &'a str,
pub suffix: &'a str,
pub span: Span,
src: &'a str,
at: Span,
prefix: Option<usize>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct TagKey<'a> {
form: TagForm,
handle: &'a str,
suffix: &'a str,
prefix: Option<(usize, usize)>,
}
impl<'a> TagSource<'a> {
pub(crate) const fn new(
form: TagForm,
handle: &'a str,
suffix: &'a str,
span: Span,
src: &'a str,
prefix: Option<usize>,
) -> Self {
Self {
form,
handle,
suffix,
span,
src,
at: span,
prefix,
}
}
#[must_use]
pub fn key(&self) -> TagKey<'a> {
TagKey {
form: self.form,
handle: self.handle,
suffix: self.suffix,
prefix: self.prefix.map(|p| (self.src.as_ptr().addr(), p)),
}
}
#[must_use]
pub fn resolve(&self) -> Option<Tag<'a>> {
crate::parser::resolve_tag(self)
}
#[must_use]
pub fn prefix_key(&self) -> Option<TagKey<'a>> {
self.prefix.map(|p| TagKey {
form: TagForm::Shorthand,
handle: "",
suffix: "",
prefix: Some((self.src.as_ptr().addr(), p)),
})
}
#[must_use]
pub fn directive_prefix(&self) -> Option<Cow<'a, [u8]>> {
crate::parser::directive_prefix(self)
}
#[must_use]
pub fn default_prefix(&self) -> Option<&'static str> {
if self.form != TagForm::Shorthand || self.prefix.is_some() {
return None;
}
match self.handle {
"!" => Some("!"),
"!!" => Some("tag:yaml.org,2002:"),
_ => None,
}
}
#[must_use]
pub fn decoded_suffix(&self) -> Cow<'a, [u8]> {
crate::parser::decoded_suffix(self)
}
pub(crate) const fn src(&self) -> &'a str {
self.src
}
pub(crate) const fn at(&self) -> Span {
self.at
}
pub(crate) const fn prefix(&self) -> Option<usize> {
self.prefix
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Directive {
Yaml,
Tag,
Reserved,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DirectiveKind<'a> {
#[non_exhaustive]
Yaml {
version: &'a str,
},
#[non_exhaustive]
Tag {
handle: &'a str,
prefix: &'a str,
},
#[non_exhaustive]
Reserved {
name: &'a str,
params: &'a str,
},
}
impl<'a> DirectiveKind<'a> {
#[must_use]
pub const fn yaml(version: &'a str) -> Self {
Self::Yaml { version }
}
#[must_use]
pub const fn tag(handle: &'a str, prefix: &'a str) -> Self {
Self::Tag { handle, prefix }
}
#[must_use]
pub const fn reserved(name: &'a str, params: &'a str) -> Self {
Self::Reserved { name, params }
}
#[must_use]
pub const fn directive(&self) -> Directive {
match self {
Self::Yaml { .. } => Directive::Yaml,
Self::Tag { .. } => Directive::Tag,
Self::Reserved { .. } => Directive::Reserved,
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TriviaKind {
ByteOrderMark,
Whitespace,
LineBreak,
Comment,
Indicator(Indicator),
Anchor,
Tag,
BlockScalarHeader,
DocumentEndMarker,
Skipped,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Indicator {
SequenceEntry,
MappingKey,
MappingValue,
FlowEntry,
}
impl Indicator {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::SequenceEntry => "-",
Self::MappingKey => "?",
Self::MappingValue => ":",
Self::FlowEntry => ",",
}
}
}
#[cfg(test)]
mod tests;