reproto_core/
rp_file.rs

1//! File declarations
2
3use errors::Result;
4use serde::Serialize;
5use std::collections::LinkedList;
6use {Diagnostics, Flavor, RpDecl, Translate, Translator};
7
8#[derive(Debug, Clone, Serialize)]
9#[serde(bound = "F: Serialize, F::Field: Serialize, F::Endpoint: Serialize, F::Package: \
10                 Serialize, F::Name: Serialize, F::EnumType: Serialize")]
11pub struct RpFile<F: 'static>
12where
13    F: Flavor,
14{
15    pub comment: Vec<String>,
16    pub decls: Vec<RpDecl<F>>,
17}
18
19/// Iterator over all declarations in a file.
20#[allow(linkedlist)]
21pub struct ForEachDecl<'a, F: 'static>
22where
23    F: Flavor,
24{
25    queue: LinkedList<&'a RpDecl<F>>,
26}
27
28impl<'a, F: 'static> Iterator for ForEachDecl<'a, F>
29where
30    F: Flavor,
31{
32    type Item = &'a RpDecl<F>;
33
34    fn next(&mut self) -> Option<Self::Item> {
35        if let Some(decl) = self.queue.pop_front() {
36            self.queue.extend(decl.decls());
37            Some(decl)
38        } else {
39            None
40        }
41    }
42}
43
44impl<F: 'static> RpFile<F>
45where
46    F: Flavor,
47{
48    /// Iterate over all declarations in file.
49    pub fn for_each_decl(&self) -> ForEachDecl<F> {
50        let mut queue = LinkedList::new();
51        queue.extend(self.decls.iter());
52        ForEachDecl { queue: queue }
53    }
54}
55
56impl<F: 'static, T> Translate<T> for RpFile<F>
57where
58    F: Flavor,
59    T: Translator<Source = F>,
60{
61    type Source = F;
62    type Out = RpFile<T::Target>;
63
64    /// Translate into different flavor.
65    fn translate(self, diag: &mut Diagnostics, translator: &T) -> Result<RpFile<T::Target>> {
66        Ok(RpFile {
67            comment: self.comment,
68            decls: self.decls.translate(diag, translator)?,
69        })
70    }
71}