Compare commits
4 commits
4d50a1d287
...
7839db8b06
Author | SHA1 | Date | |
---|---|---|---|
7839db8b06 | |||
c0ae1a2229 | |||
bfb20374d4 | |||
36530dc69a |
51
src/roxy.rs
51
src/roxy.rs
|
@ -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(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue