tamer: obj::xmle::xir: Extract ElemWrap into ir::xir::iter

main
Mike Gerwitz 2021-10-11 09:34:17 -04:00
parent de62a2acbc
commit 1a2f6bd209
3 changed files with 50 additions and 22 deletions

View File

@ -38,6 +38,7 @@ use std::convert::{TryFrom, TryInto};
use std::fmt::Display;
use std::ops::Deref;
pub mod iter;
pub mod pred;
pub mod tree;
pub mod writer;

View File

@ -0,0 +1,47 @@
// XIR stream iterators
//
// Copyright (C) 2014-2021 Ryan Specialty Group, LLC.
//
// This file is part of TAME.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//! XIR [`Token`] stream iterators.
use super::Token;
use std::iter::{once, Chain, Once};
/// An iterator that wraps a [`Token`] iterator in an element.
///
/// This introduces an opening and closing token before and after the
/// iterator.
pub struct ElemWrapIter<I: Iterator<Item = Token>>(
Chain<Chain<Once<Token>, I>, Once<Token>>,
);
impl<I: Iterator<Item = Token>> ElemWrapIter<I> {
#[inline]
pub fn new(open: Token, inner: I, close: Token) -> Self {
Self(once(open).chain(inner).chain(once(close)))
}
}
impl<I: Iterator<Item = Token>> Iterator for ElemWrapIter<I> {
type Item = Token;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}

View File

@ -24,13 +24,13 @@ use crate::{
asg::{
IdentKind, IdentObject, IdentObjectData, Sections, SectionsIter,
},
xir::{AttrValue, QName, Text, Token},
xir::{iter::ElemWrapIter, AttrValue, QName, Text, Token},
},
ld::LSPAN,
sym::{st::*, SymbolId},
};
use arrayvec::ArrayVec;
use std::iter::{once, Chain, Once};
use std::iter::Chain;
use std::{array, collections::hash_set};
qname_const! {
@ -327,26 +327,6 @@ impl<'a, T: IdentObjectData> Iterator for FragmentIter<'a, T> {
}
}
pub struct ElemWrapIter<I: Iterator<Item = Token>>(
Chain<Chain<Once<Token>, I>, Once<Token>>,
);
impl<I: Iterator<Item = Token>> ElemWrapIter<I> {
#[inline]
fn new(open: Token, inner: I, close: Token) -> Self {
Self(once(open).chain(inner).chain(once(close)))
}
}
impl<I: Iterator<Item = Token>> Iterator for ElemWrapIter<I> {
type Item = Token;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
/// Iterator that lazily lowers `xmle` object files into XIR.
///
/// This serves primarily to encapsulate the nasty iterator type without