Compare commits

...

4 commits

Author SHA1 Message Date
KitsuneCafe 7839db8b06 Parser::append 2024-02-05 05:04:54 -05:00
KitsuneCafe c0ae1a2229 parse convert 2024-02-05 04:27:52 -05:00
KitsuneCafe bfb20374d4 parse convert 2024-02-05 04:18:56 -05:00
KitsuneCafe 36530dc69a Parse::as_dyn 2024-02-05 03:06:23 -05:00

View file

@ -6,18 +6,23 @@ use std::{
use crate::error::Error;
pub trait Parse {
fn parse(&mut self, path: &str, src: &[u8], dst: &mut Vec<u8>) -> Result<(), Error>;
pub trait AsParse {
fn as_parse(&self) -> &dyn Parse;
fn as_parse_mut(&mut self) -> &mut dyn Parse;
}
impl<'a, P: Parse + 'static> Into<Box<dyn Parse>> for (P,) {
fn into(self) -> Box<dyn Parse> {
Box::new(self.0)
impl<P: Parse> AsParse for P {
fn as_parse(&self) -> &dyn Parse {
self
}
fn as_parse_mut(&mut self) -> &mut dyn Parse {
self
}
}
pub trait AndThenParser<P> {
fn and_then(&mut self, parser: P) -> &Self;
pub trait Parse: AsParse {
fn parse(&mut self, path: &str, src: &[u8], dst: &mut Vec<u8>) -> Result<(), Error>;
}
pub struct Parser<'a> {
@ -29,8 +34,12 @@ impl<'a> Parser<'a> {
Parser { steps: Vec::new() }
}
pub fn push<P: Into<&'a mut dyn Parse>>(&mut self, parser: P) {
self.steps.push(parser.into());
pub fn push<P: AsParse>(&mut self, parser: &'a mut P) {
self.steps.push(parser.as_parse_mut());
}
pub fn append(&mut self, parsers: &mut Vec<&'a mut dyn Parse>) {
self.steps.append(parsers);
}
}
@ -136,3 +145,27 @@ impl Roxy {
Self::mkdir_and_open(&output).and_then(|mut f| f.write_all(&buf))
}
}
#[cfg(test)]
mod tests {
use super::{Parse, Parser};
struct TestParser;
impl TestParser {
pub fn new() -> Self {
Self
}
}
impl Parse for TestParser {
fn parse(
&mut self,
path: &str,
src: &[u8],
dst: &mut Vec<u8>,
) -> Result<(), crate::error::Error> {
Ok(())
}
}
}