use crate::error::Error;
use crate::event::{Event, ScalarStyle};
use crate::pos::{Pos, Span};
use std::borrow::Cow;
pub enum StepResult<'input> {
Continue,
Yield(Result<(Event<'input>, Span), Error>),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IterState {
BeforeStream,
BetweenDocs,
InDocument,
Done,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MappingPhase {
Key,
Value,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CollectionEntry {
Sequence(usize, bool),
Mapping(usize, MappingPhase, bool),
}
impl CollectionEntry {
pub const fn indent(self) -> usize {
match self {
Self::Sequence(col, _) | Self::Mapping(col, _, _) => col,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FlowMappingPhase {
Key,
Value,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PendingAnchor<'input> {
Standalone(&'input str),
Inline(&'input str),
}
impl<'input> PendingAnchor<'input> {
pub const fn name(self) -> &'input str {
match self {
Self::Standalone(n) | Self::Inline(n) => n,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PendingTag<'input> {
Standalone(Cow<'input, str>),
Inline(Cow<'input, str>),
}
impl<'input> PendingTag<'input> {
pub fn into_cow(self) -> Cow<'input, str> {
match self {
Self::Standalone(c) | Self::Inline(c) => c,
}
}
}
pub enum ConsumedMapping<'input> {
ExplicitKey {
had_key_inline: bool,
},
ImplicitKey {
key_value: std::borrow::Cow<'input, str>,
key_style: ScalarStyle,
key_span: Span,
},
InlineImplicitMappingError { pos: Pos },
QuotedKeyError { pos: Pos, message: String },
}
#[cfg(test)]
mod tests {
use super::{PendingAnchor, PendingTag};
use std::borrow::Cow;
#[test]
fn pending_anchor_standalone_carries_name() {
let a = PendingAnchor::Standalone("myanchor");
assert_eq!(a.name(), "myanchor");
assert!(matches!(a, PendingAnchor::Standalone("myanchor")));
}
#[test]
fn pending_anchor_inline_carries_name() {
let a = PendingAnchor::Inline("inlineanchor");
assert_eq!(a.name(), "inlineanchor");
assert!(matches!(a, PendingAnchor::Inline("inlineanchor")));
}
#[test]
fn pending_anchor_none_matches_none_arm() {
let a: Option<PendingAnchor<'_>> = None;
assert!(a.is_none());
assert!(!matches!(a, Some(PendingAnchor::Standalone(_))));
assert!(!matches!(a, Some(PendingAnchor::Inline(_))));
}
#[test]
fn pending_tag_standalone_carries_value() {
let tag = PendingTag::Standalone(Cow::Borrowed("tag:yaml.org,2002:str"));
assert!(matches!(&tag, PendingTag::Standalone(c) if c.as_ref() == "tag:yaml.org,2002:str"));
assert_eq!(tag.into_cow().as_ref(), "tag:yaml.org,2002:str");
}
#[test]
fn pending_tag_inline_carries_value() {
let tag = PendingTag::Inline(Cow::Borrowed("!mytag"));
assert!(matches!(&tag, PendingTag::Inline(c) if c.as_ref() == "!mytag"));
assert_eq!(tag.into_cow().as_ref(), "!mytag");
}
}