parse convert

This commit is contained in:
KitsuneCafe 2024-02-05 04:18:56 -05:00
parent 36530dc69a
commit bfb20374d4

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;
}
fn as_dyn(&self) -> &dyn Parse where Self: Sized {
impl<P: Parse> AsParse for P {
fn as_parse(&self) -> &dyn Parse {
self
}
fn as_parse_mut(&mut self) -> &mut dyn Parse {
self
}
}
impl<P: Parse + 'static> Into<Box<dyn Parse>> for (P,) {
fn into(self) -> Box<dyn Parse> {
Box::new(self.0)
}
pub trait Parse {
fn parse(&mut self, path: &str, src: &[u8], dst: &mut Vec<u8>) -> Result<(), Error>;
}
pub struct Parser<'a> {
@ -29,8 +34,8 @@ 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());
}
}