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
#![deny(clippy::print_stderr)]
28
#![deny(clippy::print_stdout)]
29
#![warn(clippy::rc_buffer)]
30
#![deny(clippy::ref_option_ref)]
31
#![warn(clippy::semicolon_if_nothing_returned)]
32
#![warn(clippy::trait_duplication_in_bounds)]
33
#![deny(clippy::unchecked_time_subtraction)]
34
#![deny(clippy::unnecessary_wraps)]
35
#![warn(clippy::unseparated_literal_suffix)]
36
#![deny(clippy::unwrap_used)]
37
#![deny(clippy::mod_module_files)]
38
#![allow(clippy::let_unit_value)] // This can reasonably be done for explicitness
39
#![allow(clippy::uninlined_format_args)]
40
#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
41
#![allow(clippy::result_large_err)] // temporary workaround for arti#587
42
#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
43
#![allow(clippy::needless_lifetimes)] // See arti#1765
44
#![allow(mismatched_lifetime_syntaxes)] // temporary workaround for arti#2060
45
#![allow(clippy::collapsible_if)] // See arti#2342
46
#![deny(clippy::unused_async)]
47
#![deny(clippy::string_slice)] // See arti#2571
48
#![allow(recursion_depth_exceeding_limit)] // arti#2715, rust/issues/159228
49
//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
50

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

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

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

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

            
78
#[doc(hidden)]
79
pub use derive_deftly;
80

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

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

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

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

            
93
pub use encode::NetdocBuilder;
94

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

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

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

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

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

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

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

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

            
160
impl NormalItemArgument for String {}
161

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

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

            
197
    use super::*;
198

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