forked from gentoo-utils/gentoo-utils
make depend::Expr generic over Parseables
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
use crate::{atom::Atom, useflag::UseFlag};
|
use crate::useflag::UseFlag;
|
||||||
|
|
||||||
pub mod parsers;
|
pub mod parsers;
|
||||||
|
|
||||||
@@ -9,10 +9,10 @@ pub enum Conditional {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum Expr {
|
pub enum Expr<T> {
|
||||||
Atom(Atom),
|
Element(T),
|
||||||
AllOf(Vec<Expr>),
|
AllOf(Vec<Self>),
|
||||||
AnyOf(Vec<Expr>),
|
AnyOf(Vec<Self>),
|
||||||
OneOf(Vec<Expr>),
|
OneOf(Vec<Self>),
|
||||||
ConditionalGroup(Conditional, Vec<Expr>),
|
ConditionalGroup(Conditional, Vec<Self>),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,14 @@ use mon::{Parser, tag, whitespace1};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Parseable,
|
Parseable,
|
||||||
atom::Atom,
|
|
||||||
depend::{Conditional, Expr},
|
depend::{Conditional, Expr},
|
||||||
useflag::UseFlag,
|
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>;
|
type Parser = impl Parser<&'a str, Output = Self>;
|
||||||
|
|
||||||
fn parser() -> Self::Parser {
|
fn parser() -> Self::Parser {
|
||||||
@@ -38,8 +40,8 @@ impl<'a> Parseable<'a, &'a str> for Expr {
|
|||||||
)
|
)
|
||||||
.map(|(conditional, exprs)| Expr::ConditionalGroup(conditional, exprs));
|
.map(|(conditional, exprs)| Expr::ConditionalGroup(conditional, exprs));
|
||||||
|
|
||||||
Atom::parser()
|
T::parser()
|
||||||
.map(|atom| Expr::Atom(atom))
|
.map(|e| Expr::Element(e))
|
||||||
.or(conditional_group)
|
.or(conditional_group)
|
||||||
.or(any_of_group)
|
.or(any_of_group)
|
||||||
.or(all_of_group)
|
.or(all_of_group)
|
||||||
@@ -68,13 +70,15 @@ mod test {
|
|||||||
|
|
||||||
use mon::input::InputIter;
|
use mon::input::InputIter;
|
||||||
|
|
||||||
|
use crate::atom::Atom;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_expr() {
|
fn test_expr() {
|
||||||
let it = InputIter::new("flag? ( || ( foo/bar foo/bar ) )");
|
let it = InputIter::new("flag? ( || ( foo/bar foo/bar ) )");
|
||||||
|
|
||||||
Expr::parser()
|
Expr::<Atom>::parser()
|
||||||
.separated_list(whitespace1(), 0..)
|
.separated_list(whitespace1(), 0..)
|
||||||
.check_finished(it)
|
.check_finished(it)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use gentoo_utils::{
|
use gentoo_utils::{
|
||||||
Parseable,
|
Parseable,
|
||||||
|
atom::Atom,
|
||||||
depend::{self, Expr},
|
depend::{self, Expr},
|
||||||
};
|
};
|
||||||
use mon::{Parser, eof, input::InputIter, tag, whitespace1};
|
use mon::{Parser, eof, input::InputIter, tag, whitespace1};
|
||||||
@@ -17,7 +18,7 @@ fn parse_md5_cache() {
|
|||||||
if line.starts_with("DEPEND=") {
|
if line.starts_with("DEPEND=") {
|
||||||
eprintln!("{line}");
|
eprintln!("{line}");
|
||||||
eprintln!();
|
eprintln!();
|
||||||
Expr::parser()
|
Expr::<Atom>::parser()
|
||||||
.separated_list(whitespace1(), 0..)
|
.separated_list(whitespace1(), 0..)
|
||||||
.ignore()
|
.ignore()
|
||||||
.or(eof())
|
.or(eof())
|
||||||
|
|||||||
Reference in New Issue
Block a user