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

            
6
use super::*;
7

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

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

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

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

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

            
48
impl ItemArgumentParseable for VarietyKeyword {
49
218
    fn from_args<'s>(args: &mut ArgumentStream<'s>) -> Result<Self, ArgumentError> {
50
218
        match args.next() {
51
218
            None => Ok(VarietyKeyword),
52
            Some(s) if s == VARIETY_KEYWORD => Ok(VarietyKeyword),
53
            Some(_other) => Err(ArgumentError::Invalid),
54
        }
55
218
    }
56
}