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,20 +6,25 @@ use std::{
use crate::error::Error;
pub trait Parse {
pub trait AsParse {
fn as_parse(&self) -> &dyn Parse;
fn as_parse_mut(&mut self) -> &mut dyn Parse;
}
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 Parse: AsParse {
fn parse(&mut self, path: &str, src: &[u8], dst: &mut Vec<u8>) -> Result<(), Error>;
}
impl<'a, P: Parse + 'static> Into<Box<dyn Parse>> for (P,) {
fn into(self) -> Box<dyn Parse> {
Box::new(self.0)
}
}
pub trait AndThenParser<P> {
fn and_then(&mut self, parser: P) -> &Self;
}
pub struct Parser<'a> {
steps: Vec<&'a mut dyn Parse>,
}
@ -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(())
}
}
}