make depend::Expr generic over Parseables

This commit is contained in:
John Turner
2025-10-29 12:50:07 +00:00
parent bdd1188409
commit b5765118fe
3 changed files with 18 additions and 13 deletions

View File

@@ -1,4 +1,4 @@
use crate::{atom::Atom, useflag::UseFlag};
use crate::useflag::UseFlag;
pub mod parsers;
@@ -9,10 +9,10 @@ pub enum Conditional {
}
#[derive(Clone, Debug)]
pub enum Expr {
Atom(Atom),
AllOf(Vec<Expr>),
AnyOf(Vec<Expr>),
OneOf(Vec<Expr>),
ConditionalGroup(Conditional, Vec<Expr>),
pub enum Expr<T> {
Element(T),
AllOf(Vec<Self>),
AnyOf(Vec<Self>),
OneOf(Vec<Self>),
ConditionalGroup(Conditional, Vec<Self>),
}

View File

@@ -2,12 +2,14 @@ use mon::{Parser, tag, whitespace1};
use crate::{
Parseable,
atom::Atom,
depend::{Conditional, Expr},
useflag::UseFlag,
};
impl<'a> Parseable<'a, &'a str> for Expr {
impl<'a, T> Parseable<'a, &'a str> for Expr<T>
where
T: Parseable<'a, &'a str>,
{
type Parser = impl Parser<&'a str, Output = Self>;
fn parser() -> Self::Parser {
@@ -38,8 +40,8 @@ impl<'a> Parseable<'a, &'a str> for Expr {
)
.map(|(conditional, exprs)| Expr::ConditionalGroup(conditional, exprs));
Atom::parser()
.map(|atom| Expr::Atom(atom))
T::parser()
.map(|e| Expr::Element(e))
.or(conditional_group)
.or(any_of_group)
.or(all_of_group)
@@ -68,13 +70,15 @@ mod test {
use mon::input::InputIter;
use crate::atom::Atom;
use super::*;
#[test]
fn test_expr() {
let it = InputIter::new("flag? ( || ( foo/bar foo/bar ) )");
Expr::parser()
Expr::<Atom>::parser()
.separated_list(whitespace1(), 0..)
.check_finished(it)
.unwrap();