aboutsummaryrefslogtreecommitdiff
path: root/crates/tor-netdoc/src/doc/netstatus/plain.rs
blob: 45f0b5148a51ca241fc33e6c0f3d71bc8d588f4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! Implementation for plain consensus documents.
//
// Read this file in conjunction with `each_variety.rs`.
// See "module scope" ns_variety_definition_macros.rs.

use super::*;

// Import `each_variety.rs`, appropriately variegated
ns_do_variety_plain! {}

/// Used for reporting errors when parsing this document type
const NETSTATUS_DOCTYPE_FOR_ERROR: &str = "plain consensus";

/// The optional `ns` keyword in a plain consensus heading line
///
/// This type is one of the fields in `NetworkStatusVersionItem`.
///
/// plain consensuses start with `network-status-version 3 ns ...`,
/// or are just `network-status-version 3`.
///
/// C Tor doesn't emit `ns`, but we will.
///
/// In our terminology this is a `plain` consensus, in but the protocol it's `ns`.
/// So in *this* variety, we parse as an *optional* fixed string `ns`,
/// and encode as (always) the string `ns`.
///
/// See also torspec#359
//
// This is not Option<fixed string> because we don't actually want to store whether
// the keyword is present.
//
// TODO DIRAUTH Arti consensus method should define that this field is present.
// <https://gitlab.torproject.org/tpo/core/torspec/-/merge_requests/481#note_3409590>
#[derive(Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[allow(clippy::exhaustive_structs)]
pub struct VarietyKeyword;

/// The constant keyword string
const VARIETY_KEYWORD: &str = "ns";

impl ItemArgument for VarietyKeyword {
    fn write_arg_onto(&self, out: &mut ItemEncoder<'_>) -> Result<(), Bug> {
        out.add_arg(&VARIETY_KEYWORD);
        Ok(())
    }
}

impl ItemArgumentParseable for VarietyKeyword {
    fn from_args<'s>(args: &mut ArgumentStream<'s>) -> Result<Self, ArgumentError> {
        match args.next() {
            None => Ok(VarietyKeyword),
            Some(s) if s == VARIETY_KEYWORD => Ok(VarietyKeyword),
            Some(_other) => Err(ArgumentError::Invalid),
        }
    }
}