rpsl 0.1.1

A parser for the Routing Policy Specification Language (RPSL)
Documentation
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
use super::{error::EvaluationError, Evaluate};

/// An object capable of evaluating arbitrary RPSL expressions.
///
/// The logic for traversing and evaluating specific RPSL filter expressions is implemented
/// in [`Evaluate<R>`].
///
/// This trait provides the generic [`evaluate()`][Self::evaluate] entry-point method, which is available
/// for every `T: Evaluate<Self>`.
///
/// In addition to an implementation of this trait, client code must also provide the necessary
/// implementations of [`Resolver<I, O>`] to meet the required trait bounds.
///
/// # Examples
///
/// A hypothetical implementation that resolves names via a local database look-up:
///
/// ``` no_run
/// use ip::{Any, PrefixSet};
/// use rpsl::{
///     names, primitive,
///     expr::{
///         eval::{Evaluate, Evaluator, EvaluationError, Resolver},
///         MpFilterExpr
///     }
/// };
///
/// // The type providing expression evaluation and name resolution
/// #[derive(Debug)]
/// struct Db {
///     // ..
/// }
///
/// // A custom error type that our client code will produce
/// #[derive(Debug)]
/// enum Error {
///     # Io
///     // ..
/// }
///
/// // impl std::error::Error + From<EvaluationError> for Error ..
/// # impl std::fmt::Display for Error {
/// #     fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { unimplemented!(); }
/// # }
/// # impl std::error::Error for Error {}
/// # impl From<EvaluationError> for Error {
/// #     fn from(_: EvaluationError) -> Self { unimplemented!(); }
/// # }
///
/// impl Db {
///     // open a connection to our database...
///     fn open() -> Self {
///         // ..
///         # unimplemented!();
///     }
///
///     // look up a name in the database and return the necessary output type for the context of
///     // the request...
///     fn fetch_as<I, O>(&mut self, item: &I) -> Result<O, Error> {
///         // ..
///         # unimplemented!();
///     }
///
///     // log an error via some implementation specific mechanism...
///     fn log(&mut self, err: &(dyn std::error::Error)) {
///         // ..
///         # unimplemented!()
///     }
/// }
///
/// impl<'a> Evaluator<'a> for Db {
///
///     // we could return another type here, but for simplicity, we just preserve the `Output`
///     type Output<T> = <T as Evaluate<'a, Self>>::Output
///     where
///         T: Evaluate<'a, Self>;
///
///     // our custom error type
///     type Error = Error;
///
///     // this is where we would construct our alternate `Output<T>`
///     fn finalise<T>(&mut self, output: T::Output) -> Result<Self::Output<T>, Self::Error>
///     where
///         T: Evaluate<'a, Self>,
///     {
///         Ok(output)
///     }
///
///     // log any errors that are returned during name resolution, except `Error::Io` which
///     // we consider fatal
///     fn sink_error(&mut self, err: &(dyn std::error::Error + Send + Sync + 'static)) -> bool {
///         if let Some(Error::Io) = err.downcast_ref::<Error>() {
///             println!("database IO error, aborting...");
///             return false;
///         }
///         self.log(err);
///         true
///     }
///
/// }
///
/// // we need to provide `Resolver` impls for the resolvable names that might appear in an
/// // `MpFilterExpr`
///
/// impl Resolver<'_, names::FilterSet, MpFilterExpr> for Db {
///     // we re-use the same error type for simplicity
///     type IError = Error;
///
///     // the actual look-up is delegated to the database
///     fn resolve(&mut self, expr: &names::FilterSet) -> Result<MpFilterExpr, Self::Error> {
///         self.fetch_as(expr)
///     }
/// }
///
/// impl Resolver<'_, names::AsSet, PrefixSet<Any>> for Db {
///     type IError = Error;
///     fn resolve(&mut self, expr: &names::AsSet) -> Result<PrefixSet<Any>, Self::Error> {
///         self.fetch_as(expr)
///     }
/// }
///
/// impl Resolver<'_, names::RouteSet, PrefixSet<Any>> for Db {
///     type IError = Error;
///     fn resolve(&mut self, expr: &names::RouteSet) -> Result<PrefixSet<Any>, Self::Error> {
///         self.fetch_as(expr)
///     }
/// }
///
/// impl Resolver<'_, names::AutNum, PrefixSet<Any>> for Db {
///     type IError = Error;
///     fn resolve(&mut self, expr: &names::AutNum) -> Result<PrefixSet<Any>, Self::Error> {
///         self.fetch_as(expr)
///     }
/// }
///
/// impl Resolver<'_, primitive::PeerAs, PrefixSet<Any>> for Db {
///     type IError = Error;
///     fn resolve(&mut self, expr: &primitive::PeerAs) -> Result<PrefixSet<Any>, Self::Error> {
///         self.fetch_as(expr)
///     }
/// }
///
/// // and now we can actually evaluate an mp-filter expression..
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let filter: MpFilterExpr = "AS-FOO".parse()?;
///     let output = Db::open().evaluate(filter)?;
///     println!("{:?}", output);
///     Ok(())
/// }
/// ```
pub trait Evaluator<'a>: Sized {
    /// The type returned by [`evaluate()`][Self::evaluate] for expression type `T`.
    ///
    /// Conversion to this type at the end of the evaluation process is provided by
    /// [`finalise()`][Self::finalise].
    ///
    /// # Examples
    ///
    /// To return the same type as [`T::Output`][Evaluate::Output]:
    ///
    /// ``` no_run
    /// # struct Foo;
    /// use rpsl::expr::eval::{Evaluate, Evaluator};
    ///
    /// impl<'a> Evaluator<'a> for Foo {
    ///
    ///     type Output<T> = <T as Evaluate<'a, Self>>::Output
    ///     where
    ///         T: Evaluate<'a, Self>;
    ///
    ///     fn finalise<T>(&mut self, output: T::Output) -> Result<Self::Output<T>, Self::Error>
    ///     where
    ///         T: Evaluate<'a, Self>,
    ///     {
    ///         Ok(output)
    ///     }
    ///
    ///     // ..
    ///     # type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
    ///     # fn sink_error(&mut self, _: &(dyn std::error::Error + Send + Sync + 'static)) -> bool { true }
    /// }
    /// ```
    type Output<T>
    where
        T: Evaluate<'a, Self>;

    /// The error type produced by failed evaluations.
    type Error: From<EvaluationError>;

    /// Evaluate an RPSL expression.
    ///
    /// This method delegates the evaluation logic to [`Evaluate::<Self>::evaluate`] which is
    /// available as long as the necessary implementations of [`Resolver`] for `Self` are present.
    ///
    /// # Errors
    ///
    /// Errors encountered during evaluation are passed to [`sink_error()`][Self::sink_error],
    /// where they can either be handled or propagated up and fail the evaluation.
    ///
    /// # Examples
    ///
    /// ```
    /// # use ip::{PrefixSet, Ipv4, traits::PrefixSet as _};
    /// # use rpsl::{
    /// #     names::{FilterSet, AsSet, RouteSet, AutNum},
    /// #     primitive::PeerAs,
    /// #     expr::{eval::{Evaluator, Resolver, Evaluate}, FilterExpr}
    /// # };
    /// struct Eval();
    ///
    /// impl<'a> Evaluator<'a> for Eval {
    ///     // ..
    ///     # type Output<T> = <T as Evaluate<'a, Self>>::Output where T: Evaluate<'a, Self>;
    ///     # type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
    ///     # fn finalise<T: Evaluate<'a, Self>>(&mut self, output: T::Output) -> Result<Self::Output<T>, Self::Error> { Ok(output) }
    ///     # fn sink_error(&mut self, _: &(dyn std::error::Error + Send + Sync + 'static)) -> bool {
    ///     # true }
    /// }
    /// # macro_rules! impl_resolver {
    /// #     ( $( $input:ty => $output:ty );* $(;)? ) => { $(
    /// #         impl Resolver<'_, $input, $output> for Eval {
    /// #             type IError = std::convert::Infallible;
    /// #             fn resolve(&mut self, _: &$input) -> Result<$output, Self::IError> { unimplemented!(); }
    /// #         }
    /// #     )* }
    /// # }
    /// # impl_resolver! {
    /// #     FilterSet => FilterExpr;
    /// #     AsSet => PrefixSet<Ipv4>;
    /// #     RouteSet => PrefixSet<Ipv4>;
    /// #     AutNum => PrefixSet<Ipv4>;
    /// #     PeerAs => PrefixSet<Ipv4>;
    /// # }
    ///
    /// let filter: FilterExpr = "{10.0.0.0/8^24}^+ OR {10.0.0.0/8}".parse()?;
    /// let set = Eval().evaluate(filter)?;
    ///
    /// assert_eq!(set.ranges().count(), 2);
    ///
    /// let prefixes = (16..=24).map(|n| 2usize.pow(n)).sum::<usize>() + 1;
    /// assert_eq!(set.prefixes().count(), prefixes);
    /// # Ok::<_, Box<dyn std::error::Error + Send + Sync + 'static>>(())
    /// ```
    fn evaluate<T: Evaluate<'a, Self>>(&mut self, expr: T) -> Result<Self::Output<T>, Self::Error> {
        let output = expr.evaluate(self)?;
        self.finalise(output)
    }

    /// Perform the final conversion from the type returned by [`Evaluate::<Self>::evaluate`]
    /// (i.e. `T::Output`) into [`Self::Output<T>`].
    ///
    /// # Errors
    ///
    /// If any fatal errors are encountered during conversion then the implementation should return
    /// an [`Err`] containing a [`Self::Error`].
    /// For non-fatal errors, implementations are encouraged to make use of the error handling
    /// functionality from [`Self::collect_result`], [`Self::collect_results`] and [`Self::sink_error`].
    ///
    /// # Examples
    ///
    /// An implementation that produces a [`Vec<ip::Prefix<Any>>`]:
    ///
    /// ``` rust
    /// # use std::str::FromStr;
    /// # use ip::{PrefixSet, Prefix, Any, traits::PrefixSet as _};
    /// # use rpsl::{
    /// #     names::{FilterSet, AsSet, RouteSet, AutNum},
    /// #     primitive::PeerAs,
    /// #     expr::{eval::{Evaluator, Resolver, Evaluate}, MpFilterExpr}
    /// # };
    /// struct PrefixesEvalutor();
    ///
    /// enum MaybePrefixes {
    ///     Prefixes(Vec<Prefix<Any>>),
    ///     Other(Box<dyn std::any::Any>),
    /// }
    ///
    /// impl Evaluator<'static> for PrefixesEvalutor {
    ///
    ///     type Output<T> = MaybePrefixes
    ///     where
    ///         T: Evaluate<'static, Self>;
    ///
    ///     fn finalise<T>(&mut self, output: T::Output) -> Result<Self::Output<T>, Self::Error>
    ///     where
    ///         T: Evaluate<'static, Self>,
    ///     {
    ///         use MaybePrefixes::{Prefixes, Other};
    ///         let boxed = Box::new(output) as Box<dyn std::any::Any>;
    ///         if let Some(prefix_set) = boxed.downcast_ref::<PrefixSet<Any>>() {
    ///             Ok(Prefixes(prefix_set.prefixes().collect()))
    ///         } else {
    ///             Ok(Other(boxed))
    ///         }
    ///     }
    ///
    ///     // ..
    ///     # type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
    ///     # fn sink_error(&mut self, _: &(dyn std::error::Error + Send + Sync + 'static)) -> bool { true }
    /// }
    /// # macro_rules! impl_resolver {
    /// #     ( $( $input:ty => $output:ty );* $(;)? ) => { $(
    /// #         impl Resolver<'static, $input, $output> for PrefixesEvalutor {
    /// #             type IError = std::convert::Infallible;
    /// #             fn resolve(&mut self, _: &$input) -> Result<$output, Self::IError> { unimplemented!(); }
    /// #         }
    /// #     )* }
    /// # }
    /// # impl_resolver! {
    /// #     FilterSet => MpFilterExpr;
    /// #     AsSet => PrefixSet<Any>;
    /// #     RouteSet => PrefixSet<Any>;
    /// #     AutNum => PrefixSet<Any>;
    /// #     PeerAs => PrefixSet<Any>;
    /// # }
    ///
    /// let filter: MpFilterExpr = "{ 2001:db8::/32^+ } AND { ::/0^33 }".parse()?;
    ///
    /// let expected: Vec<Prefix<Any>> = ["2001:db8::/33", "2001:db8:8000::/33"].into_iter()
    ///     .map(Prefix::<Any>::from_str)
    ///     .collect::<Result<_, _>>()?;
    ///
    /// match PrefixesEvalutor().evaluate(filter)? {
    ///     MaybePrefixes::Prefixes(prefixes) => {
    ///         assert_eq!(prefixes, expected);
    ///     }
    ///     MaybePrefixes::Other(_) => {
    ///         return Err("expecting a list of prefixes!".into());
    ///     }
    /// }
    /// # Ok::<_, Box<dyn std::error::Error + Send + Sync + 'static>>(())
    /// ```
    fn finalise<T>(&mut self, output: T::Output) -> Result<Self::Output<T>, Self::Error>
    where
        T: Evaluate<'a, Self>;

    /// Handle an error condition or abort evaluation.
    ///
    /// Implementations should inspect `err` argument to determine whether evaluation should
    /// proceed (by returning [`true`]) or abort (by returning [`false`]).
    /// If the error condition is non-fatal, additional error handling (such as logging, etc)
    /// should be performed here.
    ///
    /// # Examples
    ///
    /// Error handling based on error type:
    ///
    /// ``` no_run
    /// # use rpsl::expr::eval::{Evaluator, Evaluate};
    /// # mod some {
    /// #     #[derive(Debug)]
    /// #     pub struct IoError;
    /// #     impl std::fmt::Display for IoError {
    /// #         fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { "io error".fmt(f) }
    /// #     }
    /// #     impl std::error::Error for IoError {}
    /// # }
    /// struct Eval;
    ///
    /// impl<'a> Evaluator<'a> for Eval {
    ///
    ///     fn sink_error(&mut self, err: &(dyn std::error::Error + Send + Sync + 'static)) -> bool {
    ///         if err.is::<some::IoError>() {
    ///             println!("error: {err}");
    ///             return false;
    ///         }
    ///         println!("warning: {err}");
    ///         true
    ///     }
    ///
    ///     // ..
    ///     # type Output<T> = <T as Evaluate<'a, Self>>::Output where T: Evaluate<'a, Self>;
    ///     # type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
    ///     # fn finalise<T: Evaluate<'a, Self>>(&mut self, output: T::Output) -> Result<Self::Output<T>, Self::Error> { Ok(output) }
    /// }
    /// ```
    fn sink_error(&mut self, err: &(dyn std::error::Error + Send + Sync + 'static)) -> bool;

    /// Handle the [`Result`] returned by a fallible operation using the error handling provided by
    /// [`sink_error()`][Self::sink_error].
    ///
    /// # Errors
    ///
    /// - [`Ok(val)`] values are mapped to [`Ok(Some(val))`].
    /// - [`Err(err)`] values that are fully handled by [`Self::sink_error`] are mapped to
    ///   [`Ok(None)`].
    /// - [`Err(err)`] values that are un-handled by [`Self::sink_error`] are mapped (unchanged,
    ///   except for their type) to [`Err(val)`].
    fn collect_result<T, E1, E2>(&mut self, result: Result<T, E1>) -> Result<Option<T>, E2>
    where
        E1: Into<E2> + std::error::Error + Send + Sync + 'static,
        E2: std::error::Error + Send + Sync + 'static,
    {
        result.map(Some).or_else(|err| {
            if self.sink_error(&err) {
                Ok(None)
            } else {
                Err(err.into())
            }
        })
    }

    /// Collect an [`Iterator`] of [`Result`]s into a collection, handling and filtering out
    /// non-fatal errors in the process.
    ///
    /// # Errors
    ///
    /// [`Err`] items are handled and filtered out using [`Self::sink_error`]. Errors that cannot
    /// be fully handled in this way are propagated to the return value.
    ///
    /// # Examples
    ///
    /// ``` rust
    /// # use std::fmt;
    /// # use rpsl::expr::{eval::{Evaluator, Resolver, Evaluate}, FilterExpr};
    /// struct Eval {
    ///     errors: usize,
    /// }
    ///
    /// impl Default for Eval {
    ///     fn default() -> Self {
    ///         Self { errors: 0 }
    ///     }
    /// }
    ///
    /// #[derive(Debug)]
    /// enum Error {
    ///     Fatal(&'static str),
    ///     NonFatal(&'static str),
    /// }
    ///
    /// impl fmt::Display for Error {
    ///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    ///         match self {
    ///             Self::Fatal(msg) | Self::NonFatal(msg) => msg.fmt(f)
    ///         }
    ///     }
    /// }
    ///
    /// impl std::error::Error for Error {}
    ///
    /// impl<'a> Evaluator<'a> for Eval {
    ///
    ///     fn sink_error(&mut self, err: &(dyn std::error::Error + Send + Sync + 'static)) -> bool {
    ///         if let Some(Error::Fatal(msg)) = err.downcast_ref::<Error>() {
    ///             println!("fatal error: {msg}");
    ///             return false;
    ///         }
    ///         println!("error: {err}");
    ///         self.errors += 1;
    ///         true
    ///     }
    ///
    ///     // ..
    ///     # type Output<T> = <T as Evaluate<'a, Self>>::Output where T: Evaluate<'a, Self>;
    ///     # type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
    ///     # fn finalise<T: Evaluate<'a, Self>>(&mut self, output: T::Output) -> Result<Self::Output<T>, Self::Error> { Ok(output) }
    /// }
    ///
    /// let results = [Ok(()), Err(Error::NonFatal("quite bad")), Err(Error::Fatal("very bad"))];
    ///
    /// let mut eval = Eval::default();
    ///
    /// let processed: Result<Vec<()>, Error> = eval.collect_results(results);
    ///
    /// assert!(matches!(processed, Err(Error::Fatal(_))));
    /// assert_eq!(eval.errors, 1);
    /// # Ok::<_, Box<dyn std::error::Error + Send + Sync + 'static>>(())
    /// ```
    fn collect_results<I, T, E1, E2, O>(&mut self, iter: I) -> Result<O, E2>
    where
        I: IntoIterator<Item = Result<T, E1>>,
        E1: Into<E2> + std::error::Error + Send + Sync + 'static,
        E2: std::error::Error + Send + Sync + 'static,
        O: FromIterator<T>,
    {
        iter.into_iter()
            .filter_map(|result| self.collect_result(result).transpose())
            .collect()
    }
}

/// An object capable of resolving an RPSL name or primitive `I` into a type `O` as part of the
/// evaluation of an RPSL expression in which it appears.
pub trait Resolver<'a, I, O>: Evaluator<'a> {
    /// The error type produced by failed resolution attempts.
    type IError: std::error::Error + Send + Sync + 'static;

    /// Attempt to resolve an RPSL name or primitive.
    ///
    /// Users of the library are not expected to call this method directly: it will be called
    /// during evaluation when an item of type `I` is encountered in that expression AST.
    ///
    /// # Errors
    ///
    /// Implementations should only return `Err(err)` variants in cases where the error should be
    /// considered fatal.
    ///
    /// The error handling methods on [`Evaluator`] can be used to process non-fatal errors.
    fn resolve(&mut self, expr: &I) -> Result<O, Self::IError>;
}