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
#[doc(hidden)]
80
pub use derive_deftly;
81

            
82
// Use `#[doc(hidden)]` rather than pub(crate), because otherwise the doctest
83
// doesn't work.
84
#[doc(hidden)]
85
pub use util::batching_split_before;
86

            
87
pub use err::{BuildError, Error, ExpectedConstantString, NetdocErrorKind, Pos};
88

            
89
pub use util::rangemap_ext::rangemap_mutate_range;
90

            
91
#[cfg(any(test, feature = "testing"))]
92
pub use test_support::{assert_eq_or_diff, parse_testcase_from_netdoc};
93

            
94
pub use encode::NetdocBuilder;
95

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

            
99
/// Alias for the Result type returned by document-builder functions in this
100
/// module.
101
pub type BuildResult<T> = std::result::Result<T, BuildError>;
102

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

            
111
impl KeywordEncodable for &'static str {
112
5223
    fn to_str(self) -> &'static str {
113
5223
        self
114
5223
    }
115
}
116

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

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

            
147
impl NormalItemArgument for usize {}
148
impl NormalItemArgument for u8 {}
149
impl NormalItemArgument for u16 {}
150
impl NormalItemArgument for u32 {}
151
impl NormalItemArgument for u64 {}
152
impl NormalItemArgument for u128 {}
153

            
154
impl NormalItemArgument for isize {}
155
impl NormalItemArgument for i8 {}
156
impl NormalItemArgument for i16 {}
157
impl NormalItemArgument for i32 {}
158
impl NormalItemArgument for i64 {}
159
impl NormalItemArgument for i128 {}
160

            
161
impl NormalItemArgument for String {}
162

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

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

            
198
    use super::*;
199

            
200
    #[test]
201
    fn protocols() {
202
        let pr = supported_protocols();
203
        let expected = "Cons=2 Desc=2-4 Microdesc=2-3".parse().unwrap();
204
        assert_eq!(pr, expected);
205
    }
206
}