Skip to main content

tor_netdoc/doc/netstatus/
plain.rs

1//! Implementation for plain consensus documents.
2//
3// Read this file in conjunction with `each_variety.rs`.
4// See "module scope" ns_variety_definition_macros.rs.
5
6use super::*;
7
8// Import `each_variety.rs`, appropriately variegated
9ns_do_variety_plain! {}
10
11/// The optional `ns` keyword in a plain consensus heading line
12///
13/// This type is one of the fields in `NetworkStatusVersionItem`.
14///
15/// plain consensuses start with `network-status-version 3 ns ...`,
16/// or are just `network-status-version 3`.
17///
18/// C Tor doesn't emit `ns`, but we will.
19///
20/// In our terminology this is a `plain` consensus, in but the protocol it's `ns`.
21/// So in *this* variety, we parse as an *optional* fixed string `ns`,
22/// and encode as (always) the string `ns`.
23///
24/// See also torspec#359
25//
26// This is not Option<fixed string> because we don't actually want to store whether
27// the keyword is present.
28//
29// TODO DIRAUTH Arti consensus method should define that this field is present.
30// <https://gitlab.torproject.org/tpo/core/torspec/-/merge_requests/481#note_3409590>
31#[derive(Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
32#[allow(clippy::exhaustive_structs)]
33pub struct VarietyKeyword;
34
35/// The constant keyword string
36const VARIETY_KEYWORD: &str = "ns";
37
38impl ItemArgument for VarietyKeyword {
39    fn write_arg_onto(&self, out: &mut ItemEncoder<'_>) -> StdResult<(), Bug> {
40        out.add_arg(&VARIETY_KEYWORD);
41        Ok(())
42    }
43}
44
45impl ItemArgumentParseable for VarietyKeyword {
46    fn from_args<'s>(args: &mut ArgumentStream<'s>) -> StdResult<Self, ArgumentError> {
47        match args.next() {
48            None => Ok(VarietyKeyword),
49            Some(s) if s == VARIETY_KEYWORD => Ok(VarietyKeyword),
50            Some(_other) => Err(ArgumentError::Invalid),
51        }
52    }
53}