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
#![warn(clippy::cognitive_complexity)]
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
//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
48

            
49
// TODO #1645 (either remove this, or decide to have it everywhere)
50
#![cfg_attr(not(all(feature = "full", feature = "experimental")), allow(unused))]
51

            
52
#[macro_use]
53
mod util;
54
#[macro_use]
55
mod derive_common;
56
#[macro_use]
57
pub mod parse2;
58
#[macro_use]
59
pub mod encode;
60
#[macro_use]
61
pub(crate) mod parse;
62
pub mod doc;
63
mod err;
64
pub mod types;
65

            
66
#[cfg(all(test, feature = "parse2", feature = "encode"))]
67
mod test2;
68

            
69
#[doc(hidden)]
70
pub use derive_deftly;
71

            
72
// Use `#[doc(hidden)]` rather than pub(crate), because otherwise the doctest
73
// doesn't work.
74
#[doc(hidden)]
75
pub use util::batching_split_before;
76

            
77
pub use err::{BuildError, Error, NetdocErrorKind, Pos};
78

            
79
#[cfg(feature = "encode")]
80
#[cfg_attr(docsrs, doc(cfg(feature = "encode")))]
81
pub use encode::NetdocBuilder;
82

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

            
86
/// Alias for the Result type returned by document-builder functions in this
87
/// module.
88
pub type BuildResult<T> = std::result::Result<T, BuildError>;
89

            
90
/// Keywords that can be encoded (written) into a (being-built) network document
91
pub trait KeywordEncodable {
92
    /// Encoding of the keyword.
93
    ///
94
    /// Used for error reporting, and also by `NetdocEncoder::item`.
95
    fn to_str(self) -> &'static str;
96
}
97

            
98
impl KeywordEncodable for &'static str {
99
210
    fn to_str(self) -> &'static str {
100
210
        self
101
210
    }
102
}
103

            
104
/// Indicates whether we should parse an annotated list of objects or a
105
/// non-annotated list.
106
#[derive(PartialEq, Debug, Eq)]
107
#[allow(clippy::exhaustive_enums)]
108
pub enum AllowAnnotations {
109
    /// Parsing a document where items might be annotated.
110
    ///
111
    /// Annotations are a list of zero or more items with keywords
112
    /// beginning with @ that precede the items that are actually part
113
    /// of the document.
114
    AnnotationsAllowed,
115
    /// Parsing a document where annotations are not allowed.
116
    AnnotationsNotAllowed,
117
}
118

            
119
/// A "normally formatted" argument to a netdoc item
120
///
121
/// A type that is represented as a single argument
122
/// whose representation is as for the type's `FromStr` and `Display`.
123
///
124
/// Implementing this trait enables a blanket impl of `parse2::ItemArgumentParseable`
125
/// and `build::ItemArgument`.
126
pub trait NormalItemArgument: std::str::FromStr + std::fmt::Display {}
127
// TODO: should we implement ItemArgument for, say, tor_llcrypto::pk::rsa::RsaIdentity ?
128
// It's not clear whether it's always formatted the same way in all parts of the spec.
129
// The Display impl of RsaIdentity adds a `$` which is not supposed to be present
130
// in (for example) an authority certificate (authcert)'s "fingerprint" line.
131

            
132
impl NormalItemArgument for usize {}
133
impl NormalItemArgument for u8 {}
134
impl NormalItemArgument for u16 {}
135
impl NormalItemArgument for u32 {}
136
impl NormalItemArgument for u64 {}
137
impl NormalItemArgument for u128 {}
138

            
139
impl NormalItemArgument for isize {}
140
impl NormalItemArgument for i8 {}
141
impl NormalItemArgument for i16 {}
142
impl NormalItemArgument for i32 {}
143
impl NormalItemArgument for i64 {}
144
impl NormalItemArgument for i128 {}
145

            
146
impl NormalItemArgument for String {}
147

            
148
/// Return a list of the protocols [supported](tor_protover::doc_supported)
149
/// by this crate.
150
638
pub fn supported_protocols() -> tor_protover::Protocols {
151
    use tor_protover::named::*;
152
    // WARNING: REMOVING ELEMENTS FROM THIS LIST CAN BE DANGEROUS!
153
    // SEE [`tor_protover::doc_changing`]
154
638
    [
155
638
        DESC_CROSSSIGN,
156
638
        DESC_NO_TAP,
157
638
        DESC_FAMILY_IDS,
158
638
        MICRODESC_ED25519_KEY,
159
638
        MICRODESC_NO_TAP,
160
638
        CONS_ED25519_MDS,
161
638
    ]
162
638
    .into_iter()
163
638
    .collect()
164
638
}
165

            
166
#[cfg(test)]
167
mod test {
168
    // @@ begin test lint list maintained by maint/add_warning @@
169
    #![allow(clippy::bool_assert_comparison)]
170
    #![allow(clippy::clone_on_copy)]
171
    #![allow(clippy::dbg_macro)]
172
    #![allow(clippy::mixed_attributes_style)]
173
    #![allow(clippy::print_stderr)]
174
    #![allow(clippy::print_stdout)]
175
    #![allow(clippy::single_char_pattern)]
176
    #![allow(clippy::unwrap_used)]
177
    #![allow(clippy::unchecked_time_subtraction)]
178
    #![allow(clippy::useless_vec)]
179
    #![allow(clippy::needless_pass_by_value)]
180
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
181

            
182
    use super::*;
183

            
184
    #[test]
185
    fn protocols() {
186
        let pr = supported_protocols();
187
        let expected = "Cons=2 Desc=2-4 Microdesc=2-3".parse().unwrap();
188
        assert_eq!(pr, expected);
189
    }
190
}