1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use crate::stream::ByteStream;
use std::{error, fmt};
use std::marker::PhantomData;

pub trait Matcher<T> {
    fn do_match(&self, bs: &mut ByteStream) -> Result<T, MatchError>;

    fn boxed(self) -> Box<dyn Matcher<T>>
    where
        Self: Sized + 'static,
    {
        Box::new(self)
    }

    fn then<U, That>(self, that: That) -> Chain<Self, That>
    where
        Self: Sized,
        That: Matcher<U>,
    {
        Chain(self, that)
    }

    fn then_with<U, F, N>(self, f: F) -> Expose<Self, F>
    where
        Self: Sized,
        F: Fn(&T) -> N + 'static,
        N: Matcher<U>,
    {
        Expose { context: self, next: f }
    }

    fn map<U, F>(self, f: F) -> Map<Self, T, F>
    where
        Self: Sized,
        F: Fn(T) -> U + 'static,
    {
        Map {
            prev: self,
            mapper: f,
            phantom: PhantomData::<T>,
        }
    }

    fn then_map<U, That, F, V>(self, that: That, f: F) -> Map<Chain<Self, That>, (T, U), F>
    where
        Self: Sized,
        That: Matcher<U>,
        F: Fn((T, U)) -> V + 'static,
    {
        self.then(that).map(f)
    }
}

impl<T, F> Matcher<T> for F where F: Fn(&mut ByteStream) -> Result<T, MatchError> {
    fn do_match(&self, bs: &mut ByteStream) -> Result<T, MatchError> {
        (self)(bs)
    }
}

impl<T> Matcher<T> for Box<dyn Matcher<T>> {
    fn do_match(&self, bs: &mut ByteStream) -> Result<T, MatchError> {
        (**self).do_match(bs)
    }
}

// Chain

pub struct Chain<M, N>(M, N);

impl<M, N, T, U> Matcher<(T, U)> for Chain<M, N> where M: Matcher<T>, N: Matcher<U> {
    fn do_match(&self, bs: &mut ByteStream) -> Result<(T, U), MatchError> {
        let t = self.0.do_match(bs)?;
        let u = self.1.do_match(bs)?;
        Ok((t, u))
    }
}

// Expose

pub struct Expose<M, F>{
    context: M,
    next: F,
}

impl<M, F, N, T, U> Matcher<(T, U)> for Expose<M, F>
where
    M: Matcher<T>,
    F: Fn(&T) -> N + 'static,
    N: Matcher<U>,
{
    fn do_match(&self, bs: &mut ByteStream) -> Result<(T, U), MatchError> {
        let t = self.context.do_match(bs)?;
        let g = (self.next)(&t);
        let u = g.do_match(bs)?;
        Ok((t, u))
    }
}

// Map

pub struct Map<M, T, F> {
    prev: M,
    mapper: F,
    phantom: PhantomData<T>,
}

impl<M, T, U, F> Matcher<U> for Map<M, T, F>
where
    M: Matcher<T>,
    F: Fn(T) -> U + 'static,
{
    fn do_match(&self, bs: &mut ByteStream) -> Result<U, MatchError> {
        let t = self.prev.do_match(bs)?;
        let u = (self.mapper)(t);
        Ok(u)
    }
}

pub fn unit<T: 'static, F: Fn() -> T + 'static>(f: F) -> impl Matcher<T> {
    move |_: &mut ByteStream| {
        let t = f();
        Ok(t)
    }
}

#[derive(Debug)]
pub struct MatchError {
    offset: usize,
    message: String,
}

impl MatchError {
    pub fn unexpected(offset: usize, got: String, expected: String) -> MatchError {
        MatchError {
            offset,
            message: format!(
                "MatchError at offset {} expected '{}' but got '{}'",
                offset, expected, got
            ),
        }
    }

    pub fn over_capacity(offset: usize, available: usize, requested: usize) -> MatchError {
        MatchError {
            offset,
            message: format!(
                "MatchError at offset {}, requested {} bytes, but buffer has only {}",
                offset, requested, available
            ),
        }
    }
}

impl fmt::Display for MatchError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(&self.message)
    }
}

impl error::Error for MatchError {
    fn description(&self) -> &str {
        "MatchError"
    }
    fn cause(&self) -> Option<&dyn error::Error> {
        None
    }
}