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
//! Model for tuples.

use super::{Loc, RpCode, RpField, RpSubType};
use std::collections::BTreeMap;
use std::rc::Rc;
use std::slice;

/// Default key to use for tagged sub type strategy.
pub const DEFAULT_TAG: &str = "type";

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum RpSubTypeStrategy {
    /// An object, with a single tag key indicating which sub-type to use.
    Tagged { tag: String },
}

impl Default for RpSubTypeStrategy {
    fn default() -> Self {
        RpSubTypeStrategy::Tagged {
            tag: DEFAULT_TAG.to_string(),
        }
    }
}

decl_body!(pub struct RpInterfaceBody {
    pub fields: Vec<Loc<RpField>>,
    pub codes: Vec<Loc<RpCode>>,
    pub sub_types: BTreeMap<String, Rc<Loc<RpSubType>>>,
    pub sub_type_strategy: RpSubTypeStrategy,
});

/// Iterator over fields.
pub struct Fields<'a> {
    iter: slice::Iter<'a, Loc<RpField>>,
}

impl<'a> Iterator for Fields<'a> {
    type Item = &'a Loc<RpField>;

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next()
    }
}

impl RpInterfaceBody {
    pub fn fields(&self) -> Fields {
        Fields {
            iter: self.fields.iter(),
        }
    }
}