1
1
//! `tor-config`: Tools for configuration management in Arti
2
//!
3
//! # Overview
4
//!
5
//! This crate is part of
6
//! [Arti](https://gitlab.torproject.org/tpo/core/arti/), a project to
7
//! implement [Tor](https://www.torproject.org/) in Rust.
8
//!
9
//! It provides low-level types for handling configuration values.
10
//!
11
//! # ⚠ Stability Warning ⚠
12
//!
13
//! The design of this crate, and of the configuration system for
14
//! Arti, is likely to change significantly before the release of Arti
15
//! 1.0.0.  For more information see ticket [#285].
16
//!
17
//! [#285]: https://gitlab.torproject.org/tpo/core/arti/-/issues/285
18

            
19
#![deny(missing_docs)]
20
#![warn(noop_method_call)]
21
#![deny(unreachable_pub)]
22
#![warn(clippy::all)]
23
#![deny(clippy::await_holding_lock)]
24
#![deny(clippy::cargo_common_metadata)]
25
#![deny(clippy::cast_lossless)]
26
#![deny(clippy::checked_conversions)]
27
#![warn(clippy::cognitive_complexity)]
28
#![deny(clippy::debug_assert_with_mut_call)]
29
#![deny(clippy::exhaustive_enums)]
30
#![deny(clippy::exhaustive_structs)]
31
#![deny(clippy::expl_impl_clone_on_copy)]
32
#![deny(clippy::fallible_impl_from)]
33
#![deny(clippy::implicit_clone)]
34
#![deny(clippy::large_stack_arrays)]
35
#![warn(clippy::manual_ok_or)]
36
#![deny(clippy::missing_docs_in_private_items)]
37
#![deny(clippy::missing_panics_doc)]
38
#![warn(clippy::needless_borrow)]
39
#![warn(clippy::needless_pass_by_value)]
40
#![warn(clippy::option_option)]
41
#![warn(clippy::rc_buffer)]
42
#![deny(clippy::ref_option_ref)]
43
#![warn(clippy::semicolon_if_nothing_returned)]
44
#![warn(clippy::trait_duplication_in_bounds)]
45
#![deny(clippy::unnecessary_wraps)]
46
#![warn(clippy::unseparated_literal_suffix)]
47
#![deny(clippy::unwrap_used)]
48

            
49
mod err;
50
mod mut_cfg;
51
mod path;
52

            
53
pub use err::{ConfigBuildError, ReconfigureError};
54
pub use mut_cfg::MutCfg;
55
pub use path::CfgPath;
56

            
57
/// Rules for reconfiguring a running Arti instance.
58
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
59
#[non_exhaustive]
60
pub enum Reconfigure {
61
    /// Perform no reconfiguration unless we can guarantee that all changes will be successful.
62
    AllOrNothing,
63
    /// Try to reconfigure as much as possible; warn on fields that we cannot reconfigure.
64
    WarnOnFailures,
65
    /// Don't reconfigure anything: Only check whether we can guarantee that all changes will be successful.
66
    CheckAllOrNothing,
67
}
68

            
69
impl Reconfigure {
70
    /// Called when we see a disallowed attempt to change `field`: either give a ReconfigureError,
71
    /// or warn and return `Ok(())`, depending on the value of `self`.
72
2
    pub fn cannot_change<S: AsRef<str>>(self, field: S) -> Result<(), ReconfigureError> {
73
2
        match self {
74
            Reconfigure::AllOrNothing | Reconfigure::CheckAllOrNothing => {
75
1
                Err(ReconfigureError::CannotChange {
76
1
                    field: field.as_ref().to_owned(),
77
1
                })
78
            }
79
            Reconfigure::WarnOnFailures => {
80
1
                tracing::warn!("Cannot change {} on a running client.", field.as_ref());
81
1
                Ok(())
82
            }
83
        }
84
2
    }
85
}
86

            
87
#[cfg(test)]
88
mod test {
89
    use super::*;
90
    use tracing_test::traced_test;
91

            
92
    #[test]
93
    #[traced_test]
94
    fn reconfigure_helpers() {
95
        let how = Reconfigure::AllOrNothing;
96
        let err = how.cannot_change("the_laws_of_physics").unwrap_err();
97
        assert_eq!(
98
            err.to_string(),
99
            "Cannot change the_laws_of_physics on a running client.".to_owned()
100
        );
101

            
102
        let how = Reconfigure::WarnOnFailures;
103
        let ok = how.cannot_change("stuff");
104
        assert!(ok.is_ok());
105
        assert!(logs_contain("Cannot change stuff on a running client."));
106
    }
107
}