1
#![cfg_attr(docsrs, feature(doc_cfg))]
2
#![doc = include_str!("../README.md")]
3
// @@ begin lint list maintained by maint/add_warning @@
4
#![allow(renamed_and_removed_lints)] // @@REMOVE_WHEN(ci_arti_stable)
5
#![allow(unknown_lints)] // @@REMOVE_WHEN(ci_arti_nightly)
6
#![warn(missing_docs)]
7
#![warn(noop_method_call)]
8
#![warn(unreachable_pub)]
9
#![warn(clippy::all)]
10
#![deny(clippy::await_holding_lock)]
11
#![deny(clippy::cargo_common_metadata)]
12
#![deny(clippy::cast_lossless)]
13
#![deny(clippy::checked_conversions)]
14
#![allow(clippy::cognitive_complexity)] // See arti#2556
15
#![deny(clippy::debug_assert_with_mut_call)]
16
#![deny(clippy::exhaustive_enums)]
17
#![deny(clippy::exhaustive_structs)]
18
#![deny(clippy::expl_impl_clone_on_copy)]
19
#![deny(clippy::fallible_impl_from)]
20
#![deny(clippy::implicit_clone)]
21
#![deny(clippy::large_stack_arrays)]
22
#![warn(clippy::manual_ok_or)]
23
#![deny(clippy::missing_docs_in_private_items)]
24
#![warn(clippy::needless_borrow)]
25
#![warn(clippy::needless_pass_by_value)]
26
#![warn(clippy::option_option)]
27
#![warn(clippy::or_fun_call)]
28
#![deny(clippy::print_stderr)]
29
#![deny(clippy::print_stdout)]
30
#![warn(clippy::rc_buffer)]
31
#![deny(clippy::ref_option_ref)]
32
#![warn(clippy::semicolon_if_nothing_returned)]
33
#![warn(clippy::trait_duplication_in_bounds)]
34
#![deny(clippy::unchecked_time_subtraction)]
35
#![deny(clippy::unnecessary_wraps)]
36
#![warn(clippy::unseparated_literal_suffix)]
37
#![deny(clippy::unwrap_used)]
38
#![deny(clippy::mod_module_files)]
39
#![allow(clippy::let_unit_value)] // This can reasonably be done for explicitness
40
#![allow(clippy::uninlined_format_args)]
41
#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
42
#![allow(clippy::result_large_err)] // temporary workaround for arti#587
43
#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
44
#![allow(clippy::needless_lifetimes)] // See arti#1765
45
#![allow(mismatched_lifetime_syntaxes)] // temporary workaround for arti#2060
46
#![allow(clippy::collapsible_if)] // See arti#2342
47
#![deny(clippy::unused_async)]
48
#![deny(clippy::string_slice)] // See arti#2571
49
#![allow(recursion_depth_exceeding_limit)] // arti#2715, rust/issues/159228
50
//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
51

            
52
// TODO #1645 (either remove this, or decide to have it everywhere)
53
#![cfg_attr(not(all(feature = "full", feature = "experimental")), allow(unused))]
54
#![allow(clippy::redundant_field_names)] // TODO beta clippy bug, rust-clippy/issues/17525
55

            
56
#[macro_use]
57
mod util;
58
#[macro_use]
59
mod derive_common;
60
#[macro_use]
61
pub mod parse2;
62
#[cfg(any(test, feature = "testing"))]
63
#[macro_use]
64
pub mod test_support;
65
#[macro_use]
66
pub mod encode;
67
#[macro_use]
68
pub(crate) mod parse;
69
pub mod doc;
70
mod err;
71
pub mod types;
72

            
73
#[cfg(test)]
74
mod test2;
75

            
76
#[cfg(any(test, feature = "testing"))]
77
pub mod testdata_live;
78

            
79
#[cfg(feature = "testing")]
80
#[path = "../testdata2/generated_consts.rs"]
81
#[rustfmt::skip]
82
pub mod testdata2;
83

            
84
#[doc(hidden)]
85
pub use derive_deftly;
86

            
87
// Use `#[doc(hidden)]` rather than pub(crate), because otherwise the doctest
88
// doesn't work.
89
#[doc(hidden)]
90
pub use util::batching_split_before;
91

            
92
pub use err::{BuildError, Error, ExpectedConstantString, NetdocErrorKind, Pos};
93

            
94
pub use util::rangemap_ext::rangemap_mutate_range;
95

            
96
#[cfg(any(test, feature = "testing"))]
97
pub use test_support::{assert_eq_or_diff, parse_testcase_from_netdoc};
98

            
99
pub use encode::NetdocBuilder;
100

            
101
/// Alias for the Result type returned by most objects in this module.
102
pub type Result<T> = std::result::Result<T, Error>;
103

            
104
/// Alias for the Result type returned by document-builder functions in this
105
/// module.
106
pub type BuildResult<T> = std::result::Result<T, BuildError>;
107

            
108
/// Keywords that can be encoded (written) into a (being-built) network document
109
pub trait KeywordEncodable {
110
    /// Encoding of the keyword.
111
    ///
112
    /// Used for error reporting, and also by `NetdocEncoder::item`.
113
    fn to_str(self) -> &'static str;
114
}
115

            
116
impl KeywordEncodable for &'static str {
117
5975
    fn to_str(self) -> &'static str {
118
5975
        self
119
5975
    }
120
}
121

            
122
/// Indicates whether we should parse an annotated list of objects or a
123
/// non-annotated list.
124
#[derive(PartialEq, Debug, Eq)]
125
#[allow(clippy::exhaustive_enums)]
126
pub enum AllowAnnotations {
127
    /// Parsing a document where items might be annotated.
128
    ///
129
    /// Annotations are a list of zero or more items with keywords
130
    /// beginning with @ that precede the items that are actually part
131
    /// of the document.
132
    AnnotationsAllowed,
133
    /// Parsing a document where annotations are not allowed.
134
    AnnotationsNotAllowed,
135
}
136

            
137
/// A "normally formatted" argument to a netdoc item
138
///
139
/// A type that is represented as a single argument
140
/// whose representation is as for the type's `FromStr` and `Display`.
141
///
142
/// Implementing this trait enables a blanket impl of
143
/// [`parse2::ItemArgumentParseable`] (if `FromStr`)
144
/// and
145
/// [`encode::ItemArgument`] (if `Display`).
146
pub trait NormalItemArgument {}
147
// TODO: should we implement ItemArgument for, say, tor_llcrypto::pk::rsa::RsaIdentity ?
148
// It's not clear whether it's always formatted the same way in all parts of the spec.
149
// The Display impl of RsaIdentity adds a `$` which is not supposed to be present
150
// in (for example) an authority certificate (authcert)'s "fingerprint" line.
151

            
152
impl NormalItemArgument for usize {}
153
impl NormalItemArgument for u8 {}
154
impl NormalItemArgument for u16 {}
155
impl NormalItemArgument for u32 {}
156
impl NormalItemArgument for u64 {}
157
impl NormalItemArgument for u128 {}
158

            
159
impl NormalItemArgument for isize {}
160
impl NormalItemArgument for i8 {}
161
impl NormalItemArgument for i16 {}
162
impl NormalItemArgument for i32 {}
163
impl NormalItemArgument for i64 {}
164
impl NormalItemArgument for i128 {}
165

            
166
impl NormalItemArgument for String {}
167

            
168
/// Return a list of the protocols [supported](tor_protover::doc_supported)
169
/// by this crate.
170
160
pub fn supported_protocols() -> tor_protover::Protocols {
171
    use tor_protover::named::*;
172
    // WARNING: REMOVING ELEMENTS FROM THIS LIST CAN BE DANGEROUS!
173
    // SEE [`tor_protover::doc_changing`]
174
160
    [
175
160
        DESC_CROSSSIGN,
176
160
        DESC_NO_TAP,
177
160
        DESC_FAMILY_IDS,
178
160
        MICRODESC_ED25519_KEY,
179
160
        MICRODESC_NO_TAP,
180
160
        CONS_ED25519_MDS,
181
160
    ]
182
160
    .into_iter()
183
160
    .collect()
184
160
}
185

            
186
#[cfg(test)]
187
mod test {
188
    // @@ begin test lint list maintained by maint/add_warning @@
189
    #![allow(clippy::bool_assert_comparison)]
190
    #![allow(clippy::clone_on_copy)]
191
    #![allow(clippy::dbg_macro)]
192
    #![allow(clippy::mixed_attributes_style)]
193
    #![allow(clippy::print_stderr)]
194
    #![allow(clippy::print_stdout)]
195
    #![allow(clippy::single_char_pattern)]
196
    #![allow(clippy::unwrap_used)]
197
    #![allow(clippy::unchecked_time_subtraction)]
198
    #![allow(clippy::useless_vec)]
199
    #![allow(clippy::needless_pass_by_value)]
200
    #![allow(clippy::string_slice)] // See arti#2571
201
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
202

            
203
    use super::*;
204

            
205
    #[test]
206
    fn protocols() {
207
        let pr = supported_protocols();
208
        let expected = "Cons=2 Desc=2-4 Microdesc=2-3".parse().unwrap();
209
        assert_eq!(pr, expected);
210
    }
211
}