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