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#![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]
57mod util;
58#[macro_use]
59mod derive_common;
60#[macro_use]
61pub mod parse2;
62#[cfg(any(test, feature = "testing"))]
63#[macro_use]
64pub mod test_support;
65#[macro_use]
66pub mod encode;
67#[macro_use]
68pub(crate) mod parse;
69pub mod doc;
70mod err;
71pub mod types;
72
73#[cfg(test)]
74mod test2;
75
76#[cfg(any(test, feature = "testing"))]
77pub mod testdata_live;
78
79#[doc(hidden)]
80pub use derive_deftly;
81
82// Use `#[doc(hidden)]` rather than pub(crate), because otherwise the doctest
83// doesn't work.
84#[doc(hidden)]
85pub use util::batching_split_before;
86
87pub use err::{BuildError, Error, ExpectedConstantString, NetdocErrorKind, Pos};
88
89pub use util::rangemap_ext::rangemap_mutate_range;
90
91#[cfg(any(test, feature = "testing"))]
92pub use test_support::{assert_eq_or_diff, parse_testcase_from_netdoc};
93
94pub use encode::NetdocBuilder;
95
96/// Alias for the Result type returned by most objects in this module.
97pub type Result<T> = std::result::Result<T, Error>;
98
99/// Alias for the Result type returned by document-builder functions in this
100/// module.
101pub type BuildResult<T> = std::result::Result<T, BuildError>;
102
103/// Keywords that can be encoded (written) into a (being-built) network document
104pub 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
111impl KeywordEncodable for &'static str {
112    fn to_str(self) -> &'static str {
113        self
114    }
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)]
121pub 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`).
141pub 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
147impl NormalItemArgument for usize {}
148impl NormalItemArgument for u8 {}
149impl NormalItemArgument for u16 {}
150impl NormalItemArgument for u32 {}
151impl NormalItemArgument for u64 {}
152impl NormalItemArgument for u128 {}
153
154impl NormalItemArgument for isize {}
155impl NormalItemArgument for i8 {}
156impl NormalItemArgument for i16 {}
157impl NormalItemArgument for i32 {}
158impl NormalItemArgument for i64 {}
159impl NormalItemArgument for i128 {}
160
161impl NormalItemArgument for String {}
162
163/// Return a list of the protocols [supported](tor_protover::doc_supported)
164/// by this crate.
165pub 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    [
170        DESC_CROSSSIGN,
171        DESC_NO_TAP,
172        DESC_FAMILY_IDS,
173        MICRODESC_ED25519_KEY,
174        MICRODESC_NO_TAP,
175        CONS_ED25519_MDS,
176    ]
177    .into_iter()
178    .collect()
179}
180
181#[cfg(test)]
182mod 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}