1
//! A filtering mechanism for directory objects.
2
//!
3
//! This module and its members are only available when `tor-dirmgr` is built
4
//! with the `dirfilter` feature.
5
//!
6
//! This is unstable code, currently used for testing only.  It might go away in
7
//! future versions, or its API might change completely. There are no semver
8
//! guarantees.
9

            
10
use std::fmt::Debug;
11
use std::sync::Arc;
12

            
13
use crate::Result;
14
use tor_netdoc::doc::{microdesc::Microdesc, netstatus::UncheckedMdConsensus};
15

            
16
/// Filtering configuration, as provided to the directory code
17
pub type FilterConfig = Option<Arc<dyn DirFilter>>;
18

            
19
/// An object that can filter directory documents before they're handled.
20
///
21
/// Instances of DirFilter can be used for testing, to modify directory data
22
/// on-the-fly.
23
pub trait DirFilter: Debug + Send + Sync {
24
    /// Modify `consensus` in an unspecified way.
25
10
    fn filter_consensus(&self, consensus: UncheckedMdConsensus) -> Result<UncheckedMdConsensus> {
26
10
        Ok(consensus)
27
10
    }
28
    /// Modify `md` in an unspecified way.
29
8
    fn filter_md(&self, md: Microdesc) -> Result<Microdesc> {
30
8
        Ok(md)
31
8
    }
32
}
33

            
34
/// A [`DirFilter`] that does nothing.
35
#[derive(Debug)]
36
#[allow(clippy::exhaustive_structs)]
37
pub struct NilFilter;
38

            
39
impl DirFilter for NilFilter {}