use std::borrow::{Borrow, Cow};
use std::ops::Deref;
use super::{Anchor, DirectiveKind, Event, Tag, TagSource};
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Sourced<'a, E = Event<'a>> {
event: E,
source: &'a str,
}
impl<'a, E: Borrow<Event<'a>>> Sourced<'a, E> {
pub(crate) const fn new(event: E, source: &'a str) -> Self {
Self { event, source }
}
#[must_use]
#[doc(hidden)]
pub fn event(&self) -> &Event<'a> {
self.event.borrow()
}
#[must_use]
pub fn into_event(self) -> E {
self.event
}
#[must_use]
pub const fn source(&self) -> &'a str {
self.source
}
#[must_use]
pub fn text(&self) -> Option<&'a str> {
self.event().source(self.source)
}
#[must_use]
pub fn value(&self) -> Option<Cow<'a, str>> {
self.event().value(self.source)
}
#[must_use]
pub fn anchor(&self) -> Option<Anchor<'a>> {
self.event().anchor(self.source)
}
#[must_use]
pub fn tag(&self) -> Option<Tag<'a>> {
self.event().tag(self.source)
}
#[must_use]
pub fn tag_source(&self) -> Option<TagSource<'a>> {
self.event().tag_source(self.source)
}
#[must_use]
pub fn alias_name(&self) -> Option<&'a str> {
self.event().alias_name(self.source)
}
#[must_use]
pub fn directive(&self) -> Option<DirectiveKind<'a>> {
self.event().directive(self.source)
}
}
impl<'a, E: Borrow<Event<'a>>> Deref for Sourced<'a, E> {
type Target = Event<'a>;
fn deref(&self) -> &Event<'a> {
self.event.borrow()
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct EventList<'a> {
source: &'a str,
events: Vec<Event<'a>>,
}
impl<'a> EventList<'a> {
pub(crate) const fn new(source: &'a str, events: Vec<Event<'a>>) -> Self {
Self { source, events }
}
#[doc(hidden)]
#[must_use]
pub const fn from_parts_unchecked(source: &'a str, events: Vec<Event<'a>>) -> Self {
Self { source, events }
}
#[must_use]
pub const fn source(&self) -> &'a str {
self.source
}
#[must_use]
pub fn events(&self) -> &[Event<'a>] {
&self.events
}
#[must_use]
pub fn get_sourced(&self, index: usize) -> Option<Sourced<'a, &Event<'a>>> {
self.events
.get(index)
.map(|event| Sourced::new(event, self.source))
}
pub fn sourced(&self) -> SourcedIter<'_, 'a> {
SourcedIter {
events: self.events.iter(),
source: self.source,
}
}
#[must_use]
pub fn into_events(self) -> Vec<Event<'a>> {
self.events
}
}
impl<'a> Deref for EventList<'a> {
type Target = [Event<'a>];
fn deref(&self) -> &[Event<'a>] {
&self.events
}
}
#[derive(Clone, Debug)]
pub struct SourcedIter<'l, 'a> {
events: core::slice::Iter<'l, Event<'a>>,
source: &'a str,
}
impl<'l, 'a> Iterator for SourcedIter<'l, 'a> {
type Item = Sourced<'a, &'l Event<'a>>;
fn next(&mut self) -> Option<Self::Item> {
let source = self.source;
self.events.next().map(|event| Sourced::new(event, source))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.events.size_hint()
}
}
impl DoubleEndedIterator for SourcedIter<'_, '_> {
fn next_back(&mut self) -> Option<Self::Item> {
let source = self.source;
self.events
.next_back()
.map(|event| Sourced::new(event, source))
}
}
impl ExactSizeIterator for SourcedIter<'_, '_> {}
impl core::iter::FusedIterator for SourcedIter<'_, '_> {}
impl<'a> IntoIterator for EventList<'a> {
type Item = Sourced<'a>;
type IntoIter = IntoSourced<'a>;
fn into_iter(self) -> IntoSourced<'a> {
IntoSourced {
events: self.events.into_iter(),
source: self.source,
}
}
}
#[derive(Clone, Debug)]
pub struct IntoSourced<'a> {
events: std::vec::IntoIter<Event<'a>>,
source: &'a str,
}
impl<'a> Iterator for IntoSourced<'a> {
type Item = Sourced<'a>;
fn next(&mut self) -> Option<Sourced<'a>> {
let source = self.source;
self.events.next().map(|event| Sourced::new(event, source))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.events.size_hint()
}
}
impl DoubleEndedIterator for IntoSourced<'_> {
fn next_back(&mut self) -> Option<Self::Item> {
let source = self.source;
self.events
.next_back()
.map(|event| Sourced::new(event, source))
}
}
impl ExactSizeIterator for IntoSourced<'_> {}
impl core::iter::FusedIterator for IntoSourced<'_> {}