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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
use std::marker::PhantomData;
use std::sync::Arc;

use anyhow::Result;
use async_trait::async_trait;
use futures::lock::Mutex;

use crate::packet::{Direction, Packet, PacketProcessingSession, PacketProcessor};
use crate::read_postgresql_packet;


#[derive(Debug, Clone)]
pub struct AuthenticationContext {
    pub username: Option<String>,
    pub authenticated: bool,
}
impl AuthenticationContext {
    pub fn new() -> AuthenticationContext {
        AuthenticationContext { username: None, authenticated: false }
    }
}

pub trait WithAuthenticationContext {
    fn authinfo(&mut self) -> &mut AuthenticationContext;
}

#[derive(Debug, Clone)]
pub struct DefaultContext {
    pub authinfo: AuthenticationContext,
}
impl WithAuthenticationContext for DefaultContext {
    fn authinfo(&mut self) -> &mut AuthenticationContext {
        &mut self.authinfo
    }
}
impl DefaultContext {
    pub fn new() -> Self {
        DefaultContext {
            authinfo: AuthenticationContext::new(),
        }
    }
}

// #[async_trait]
pub trait Parser<T, C> {
    fn parse(&self, packet: &Packet, context: &mut C) -> Result<T>;
}

pub trait Filter<T> {
    fn filter(&self, message: &T) -> bool;
}

pub trait Transformer<T, C> {
    fn transform(&self, message: &T, context: &C) -> Result<T>;
}

pub trait Encoder<T> {
    fn encode(&self, message: &T) -> Result<Packet>;
}

// #[async_trait]
pub trait Reporter<T, C> {
    fn report(&self, message: &T, direction: Direction, context: &C) -> Result<()>;
}

#[derive(Clone)]
pub struct RuleSetProcessor<
        T,
        P : Parser<T, C> + Clone,
        F : Filter<T> + Clone,
        X : Transformer<T, C> + Clone,
        E : Encoder<T> + Clone,
        R : Reporter<T, C> + Clone,
        C,
        CC : Fn() -> C,
> {
    pub parser: Box<P>,
    pub filter: Box<F>,
    pub transformer: Box<X>,
    pub encoder: Box<E>,
    pub reporter: Box<R>,
    packet_type: PhantomData<T>,
    // context_type: PhantomData<C>,
    create_context: CC,
    // router: Router<T>
}

impl<T,P,F,X,E,R,C,CC> PacketProcessor for RuleSetProcessor<T,P,F,X,E,R,C,CC> where
    T : Clone + Send + Sync + 'static,
    P : Parser<T,C> + Clone + Send + Sync + 'static,
    F : Filter<T> + Clone + Send + Sync + 'static,
    X : Transformer<T, C> + Clone + Send + Sync + 'static,
    E : Encoder<T> + Clone + Send + Sync + 'static,
    R : Reporter<T, C> + Clone + Send + Sync + 'static,
    C : Clone + Send + Sync + 'static,
    CC : Fn() -> C,
{
    fn start_session(&self) -> Arc<Mutex<dyn PacketProcessingSession + Send>> {
        let context = (self.create_context)();
        let session = RuleSetSession::new(
            self.parser.as_ref(),
            self.filter.as_ref(),
            self.transformer.as_ref(),
            self.encoder.as_ref(),
            self.reporter.as_ref(),
            &context,
        );
        Arc::new(Mutex::new(session))
    }
}

pub struct RuleSetSession<
    T,
    P : Parser<T,C> + Clone,
    F : Filter<T> + Clone,
    X : Transformer<T, C> + Clone,
    E : Encoder<T> + Clone,
    R : Reporter<T, C> + Clone,
    C : Clone,
> {
    pub parser: Box<P>,
    pub filter: Box<F>,
    pub transformer: Box<X>,
    pub encoder: Box<E>,
    pub reporter: Box<R>,
    packet_type: PhantomData<T>,
    context: Box<C>,
}

pub trait PacketTransformer {
    type PacketType;
    fn transform(&self, packet: &Self::PacketType) -> Self::PacketType;
}

impl<T,P,F,X,E,R,C,CC> RuleSetProcessor<T,P,F,X,E,R,C,CC> where
    T : Clone,
    P : Parser<T,C> + Clone,
    F : Filter<T> + Clone,
    X : Transformer<T, C> + Clone,
    E : Encoder<T> + Clone,
    R : Reporter<T, C> + Clone,
    CC : Fn() -> C + Clone,
{
    pub fn new(
        parser: &P,
        filter: &F,
        transformer: &X,
        encoder: &E,
        reporter: &R,
        create_context: &CC,
    ) -> RuleSetProcessor<T,P,F,X,E,R,C,CC> {
        RuleSetProcessor {
            parser: Box::new(parser.clone()),
            filter: Box::new(filter.clone()),
            transformer: Box::new(transformer.clone()),
            encoder: Box::new(encoder.clone()),
            reporter: Box::new(reporter.clone()),
            packet_type: PhantomData,
            create_context: create_context.clone(),
        }
    }

    pub fn with_reporter<RN>(&self, new_reporter: &RN) -> RuleSetProcessor<T, P, F, X, E, RN, C, CC>
        where RN : Reporter<T, C> + Clone
    {
        RuleSetProcessor::new(
            &self.parser.clone(),
            &self.filter.clone(),
            &self.transformer.clone(),
            &self.encoder.clone(),
            new_reporter,
            &self.create_context,
        )
    }

    pub fn with_filter<FN>(&self, new_filter: &FN) -> RuleSetProcessor<T, P, FN, X, E, R, C, CC>
        where FN : Filter<T> + Clone
    {
        RuleSetProcessor::new(
            &self.parser.clone(),
            new_filter,
            &self.transformer.clone(),
            &self.encoder.clone(),
            &self.reporter.clone(),
            &self.create_context,
        )
    }

    pub fn with_transformer<XN>(&self, new_transformer: &XN) -> RuleSetProcessor<T, P, F, XN, E, R, C, CC>
        where XN : Transformer<T, C> + Clone
    {
        RuleSetProcessor::new(
            &self.parser,
            &self.filter,
            new_transformer,
            &self.encoder,
            &self.reporter,
            &self.create_context,
        )
    }
}

impl<T,P,F,X,E,R,C> RuleSetSession<T,P,F,X,E,R,C> where
    T : Clone,
    P : Parser<T,C> + Clone,
    F : Filter<T> + Clone,
    X : Transformer<T, C> + Clone,
    E : Encoder<T> + Clone,
    R : Reporter<T, C> + Clone,
    C : Clone,
{
    pub fn new(
        parser: &P,
        filter: &F,
        transformer: &X,
        encoder: &E,
        reporter: &R,
        context: &C,
    ) -> RuleSetSession<T,P,F,X,E,R,C> {
        RuleSetSession {
            parser: Box::new(parser.clone()),
            filter: Box::new(filter.clone()),
            transformer: Box::new(transformer.clone()),
            encoder: Box::new(encoder.clone()),
            reporter: Box::new(reporter.clone()),
            packet_type: PhantomData,
            context: Box::new(context.clone()),
        }
    }
}


#[async_trait]
impl<T,P,F,X,E,R,C> PacketProcessingSession for RuleSetSession<T, P, F, X, E, R, C> where
    T : Clone + Send + Sync,
    P : Parser<T,C> + Clone + Send + Sync,
    F : Filter<T> + Clone + Send + Sync,
    X : Transformer<T, C> + Clone + Send + Sync,
    E : Encoder<T> + Clone + Send + Sync,
    R : Reporter<T, C> + Clone + Send + Sync,
    C : Clone + Send + Sync,
{
    fn parse(&self, packet_buf: &mut Vec<u8>) -> Result<Option<Packet>> {
        // FIXME: assuming Postgres type packets
        read_postgresql_packet(packet_buf)
    }

    fn process_incoming(&mut self, packet: &Packet) -> Result<Option<Packet>> {
        let parsed = self.parser.parse(packet, &mut self.context)?;
        self.reporter.report(&parsed, Direction::Forward, &self.context)?;
        if self.filter.filter(&parsed) {
            let transformed = self.transformer.transform(&parsed, &self.context)?;
            let encoded = self.encoder.encode(&transformed)?;
            Ok(Some(encoded))
        } else {
            Ok(None)
        }
    }

    fn process_outgoing(&mut self, packet: &Packet) -> Result<Option<Packet>> {
        self.reporter.report(
            &self.parser.parse(packet, &mut self.context)?,
            Direction::Backward,
            &self.context
        )?;
        Ok(Some(packet.clone()))
    }
}

#[derive(Debug, Clone)]
pub struct NoContext {}
impl NoContext {
    fn new() -> NoContext {
        NoContext {}
    }
}

impl<T,P,E,CC> RuleSetProcessor<T, P, NoFilter<T>, NoTransform<T>, E, NoReport<T>, NoContext, CC> where
    T : Clone + Sync,
    P : Parser<T,NoContext> + Clone,
    E : Encoder<T> + Clone,
    CC : Fn() -> NoContext,
{
    pub fn minimal(parser: &P, encoder: &E) -> RuleSetProcessor<
        T,
        P,
        NoFilter<T>,
        NoTransform<T>,
        E,
        NoReport<T>,
        NoContext,
        impl Fn() -> NoContext,
    > {
        RuleSetProcessor::new(
            parser,
            &NoFilter::new(),
            &NoTransform::new(),
            encoder,
            &NoReport::new(),
            &NoContext::new,
        )
    }
}


#[derive(Clone)]
pub struct NoParserEncoder {}
impl NoParserEncoder {
    pub fn new() -> NoParserEncoder {
        NoParserEncoder {}
    }
}
#[async_trait]
impl Parser<Packet,NoContext> for NoParserEncoder {
    fn parse(&self, packet: &Packet, _context: &mut NoContext) -> Result<Packet> {
        Ok(packet.clone())
    }
}
impl Encoder<Packet> for NoParserEncoder {
    fn encode(&self, message: &Packet) -> Result<Packet> {
        Ok(message.clone())
    }
}

#[derive(Clone)]
pub struct NoFilter<T> {
    message_type: PhantomData<T>
}
impl<T> Filter<T> for NoFilter<T> {
    fn filter(&self, _message: &T) -> bool {
        return true;
    }
}
impl<T> NoFilter<T> {
    pub fn new() -> NoFilter<T> {
        NoFilter { message_type: PhantomData }
    }
}

#[derive(Clone)]
pub struct NoTransform<T> {
    message_type: PhantomData<T>
}
impl<T, C> Transformer<T, C> for NoTransform<T> where T : Clone {
    fn transform(&self, message: &T, _context: &C) -> Result<T> {
        Ok(message.clone())
    }
}
impl<T> NoTransform<T> {
    pub fn new() -> NoTransform<T> {
        NoTransform { message_type: PhantomData }
    }
}

#[derive(Clone)]
pub struct NoReport<T: Sync> {
    message_type: PhantomData<T>
}
#[async_trait]
impl<T: Sync, C> Reporter<T, C> for NoReport<T> {
    fn report(&self, _message: &T, _direction: Direction, _context: &C) -> Result<()> {
        Ok(())
    }
}
impl<T: Sync> NoReport<T> {
    pub fn new() -> NoReport<T> {
        NoReport { message_type: PhantomData }
    }
}


pub trait Encodable {
    fn encode(&self) -> Result<Packet>;
}

#[derive(Clone)]
pub struct MessageEncoder<T: Encodable> {
    message_type: PhantomData<T>
}
impl<T> Encoder<T> for MessageEncoder<T> where T : Encodable {
    fn encode(&self, message: &T) -> Result<Packet> {
        message.encode()
    }
}
impl<T> MessageEncoder<T> where T : Encodable {
    pub fn new() -> MessageEncoder<T> {
        MessageEncoder { message_type: PhantomData }
    }
}