Skip to main content

tor_netdoc/
lib.rs

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#![deny(clippy::string_slice)] // See arti#2571
48//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
49
50// TODO #1645 (either remove this, or decide to have it everywhere)
51#![cfg_attr(not(all(feature = "full", feature = "experimental")), allow(unused))]
52
53#[macro_use]
54mod util;
55#[macro_use]
56mod derive_common;
57#[macro_use]
58pub mod parse2;
59#[macro_use]
60pub mod encode;
61#[macro_use]
62pub(crate) mod parse;
63pub mod doc;
64mod err;
65pub mod types;
66
67#[cfg(test)]
68mod test2;
69
70#[doc(hidden)]
71pub use derive_deftly;
72
73// Use `#[doc(hidden)]` rather than pub(crate), because otherwise the doctest
74// doesn't work.
75#[doc(hidden)]
76pub use util::batching_split_before;
77
78pub use err::{BuildError, Error, ExpectedConstantString, NetdocErrorKind, Pos};
79
80pub use encode::NetdocBuilder;
81
82/// Alias for the Result type returned by most objects in this module.
83pub type Result<T> = std::result::Result<T, Error>;
84
85/// Alias for the Result type returned by document-builder functions in this
86/// module.
87pub type BuildResult<T> = std::result::Result<T, BuildError>;
88
89/// Keywords that can be encoded (written) into a (being-built) network document
90pub trait KeywordEncodable {
91    /// Encoding of the keyword.
92    ///
93    /// Used for error reporting, and also by `NetdocEncoder::item`.
94    fn to_str(self) -> &'static str;
95}
96
97impl KeywordEncodable for &'static str {
98    fn to_str(self) -> &'static str {
99        self
100    }
101}
102
103/// Indicates whether we should parse an annotated list of objects or a
104/// non-annotated list.
105#[derive(PartialEq, Debug, Eq)]
106#[allow(clippy::exhaustive_enums)]
107pub enum AllowAnnotations {
108    /// Parsing a document where items might be annotated.
109    ///
110    /// Annotations are a list of zero or more items with keywords
111    /// beginning with @ that precede the items that are actually part
112    /// of the document.
113    AnnotationsAllowed,
114    /// Parsing a document where annotations are not allowed.
115    AnnotationsNotAllowed,
116}
117
118/// A "normally formatted" argument to a netdoc item
119///
120/// A type that is represented as a single argument
121/// whose representation is as for the type's `FromStr` and `Display`.
122///
123/// Implementing this trait enables a blanket impl of
124/// [`parse2::ItemArgumentParseable`] (if `FromStr`)
125/// and
126/// [`encode::ItemArgument`] (if `Display`).
127pub trait NormalItemArgument {}
128// TODO: should we implement ItemArgument for, say, tor_llcrypto::pk::rsa::RsaIdentity ?
129// It's not clear whether it's always formatted the same way in all parts of the spec.
130// The Display impl of RsaIdentity adds a `$` which is not supposed to be present
131// in (for example) an authority certificate (authcert)'s "fingerprint" line.
132
133impl NormalItemArgument for usize {}
134impl NormalItemArgument for u8 {}
135impl NormalItemArgument for u16 {}
136impl NormalItemArgument for u32 {}
137impl NormalItemArgument for u64 {}
138impl NormalItemArgument for u128 {}
139
140impl NormalItemArgument for isize {}
141impl NormalItemArgument for i8 {}
142impl NormalItemArgument for i16 {}
143impl NormalItemArgument for i32 {}
144impl NormalItemArgument for i64 {}
145impl NormalItemArgument for i128 {}
146
147impl NormalItemArgument for String {}
148
149/// Return a list of the protocols [supported](tor_protover::doc_supported)
150/// by this crate.
151pub fn supported_protocols() -> tor_protover::Protocols {
152    use tor_protover::named::*;
153    // WARNING: REMOVING ELEMENTS FROM THIS LIST CAN BE DANGEROUS!
154    // SEE [`tor_protover::doc_changing`]
155    [
156        DESC_CROSSSIGN,
157        DESC_NO_TAP,
158        DESC_FAMILY_IDS,
159        MICRODESC_ED25519_KEY,
160        MICRODESC_NO_TAP,
161        CONS_ED25519_MDS,
162    ]
163    .into_iter()
164    .collect()
165}
166
167#[cfg(test)]
168mod test {
169    // @@ begin test lint list maintained by maint/add_warning @@
170    #![allow(clippy::bool_assert_comparison)]
171    #![allow(clippy::clone_on_copy)]
172    #![allow(clippy::dbg_macro)]
173    #![allow(clippy::mixed_attributes_style)]
174    #![allow(clippy::print_stderr)]
175    #![allow(clippy::print_stdout)]
176    #![allow(clippy::single_char_pattern)]
177    #![allow(clippy::unwrap_used)]
178    #![allow(clippy::unchecked_time_subtraction)]
179    #![allow(clippy::useless_vec)]
180    #![allow(clippy::needless_pass_by_value)]
181    #![allow(clippy::string_slice)] // See arti#2571
182    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
183
184    use super::*;
185
186    #[test]
187    fn protocols() {
188        let pr = supported_protocols();
189        let expected = "Cons=2 Desc=2-4 Microdesc=2-3".parse().unwrap();
190        assert_eq!(pr, expected);
191    }
192}