use std::borrow::Cow;
use oneyaml_core::Span;
use super::block_scalar::block_value;
use super::scalar::{plain_value, quoted};
use super::scan::{anchor_end, byte, decoded, percent_decode_concat, scan_tag, tag_prefix_at};
use super::stream::parse_directive;
use crate::event::{Anchor, BlockHeader, DirectiveKind, ScalarStyle, Tag, TagForm, TagSource};
pub(crate) fn anchor_at(src: &str, start: usize) -> Option<Anchor<'_>> {
if byte(src, start) != Some(b'&') {
return None;
}
let from = start.checked_add(1)?;
let end = anchor_end(src, from);
let name = src.get(from..end).filter(|n| !n.is_empty())?;
Some(Anchor::new(name, Span::new(start, end)))
}
pub(crate) fn tag_source_at(
src: &str,
start: usize,
prefix: Option<usize>,
) -> Option<TagSource<'_>> {
let t = scan_tag(src, start)?;
let handle = src.get(t.handle.0..t.handle.1)?;
let suffix = src.get(t.suffix.0..t.suffix.1)?;
let span = Span::new(start, t.end);
Some(TagSource::new(t.form, handle, suffix, span, src, prefix))
}
pub(crate) fn resolve_tag<'a>(t: &TagSource<'a>) -> Option<Tag<'a>> {
let src = t.src();
let (handle, suffix, span) = (t.handle, t.suffix, t.span);
let at = t.at();
let text = src.get(at.start..at.end)?;
let resolved = match t.form {
TagForm::NonSpecific => Cow::Borrowed("!"),
TagForm::Verbatim if suffix.contains('%') => Cow::Owned(percent_decode_concat("", suffix)?),
TagForm::Verbatim => Cow::Borrowed(suffix),
_ => {
let prefix = match (t.prefix(), handle) {
(Some(p), _) => tag_prefix_at(src, p)?,
(None, "!") => "!",
(None, "!!") => "tag:yaml.org,2002:",
(None, _) => return None,
};
if prefix == "!" && handle == "!" && !suffix.contains('%') {
Cow::Borrowed(text)
} else {
Cow::Owned(percent_decode_concat(prefix, suffix)?)
}
}
};
let (handle, suffix) = match t.form {
TagForm::Verbatim => ("", suffix),
TagForm::NonSpecific => ("!", ""),
_ => (handle, suffix),
};
Some(Tag::new(t.form, handle, suffix, resolved, span))
}
pub(crate) fn directive_prefix<'a>(t: &TagSource<'a>) -> Option<Cow<'a, [u8]>> {
let p = tag_prefix_at(t.src(), t.prefix()?)?;
Some(if p.contains('%') {
Cow::Owned(decoded(p).collect())
} else {
Cow::Borrowed(p.as_bytes())
})
}
pub(crate) fn decoded_suffix<'a>(t: &TagSource<'a>) -> Cow<'a, [u8]> {
if t.suffix.contains('%') {
Cow::Owned(decoded(t.suffix).collect())
} else {
Cow::Borrowed(t.suffix.as_bytes())
}
}
pub(crate) fn scalar_value(
src: &str,
span: Span,
style: ScalarStyle,
block: Option<BlockHeader>,
) -> Option<Cow<'_, str>> {
let text = src.get(span.start..span.end)?;
match style {
ScalarStyle::Plain if text.contains(['\n', '\r']) => {
Some(Cow::Owned(plain_value::<String>(text)))
}
ScalarStyle::Plain => Some(Cow::Borrowed(text)),
ScalarStyle::SingleQuoted | ScalarStyle::DoubleQuoted => {
let double = style == ScalarStyle::DoubleQuoted;
let f = quoted::<String>(text, 0, None, double)
.ok()
.filter(|f| f.end == text.len())?;
match f.value {
Some(owned) => Some(Cow::Owned(owned)),
None => text.get(1..f.end.saturating_sub(1)).map(Cow::Borrowed),
}
}
ScalarStyle::Literal | ScalarStyle::Folded => {
let folded = style == ScalarStyle::Folded;
block_value(src, span, block?, folded).map(Cow::Owned)
}
}
}
pub(crate) fn directive_at(src: &str, span: Span) -> Option<DirectiveKind<'_>> {
parse_directive(src, span.start)
.filter(|d| d.end == span.end)
.map(|d| d.kind)
}