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
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use IntoBytes;
use core::errors::*;
use core::{Flavor, Handle, Loc, RelativePath, RelativePathBuf, RpDecl, RpEnumBody,
           RpInterfaceBody, RpName, RpPackage, RpServiceBody, RpTupleBody, RpTypeBody};
use std::cmp;
use std::collections::BTreeMap;
use std::fmt;
use std::io::Write;

pub trait Name<F>: Clone + fmt::Display + fmt::Debug + cmp::Eq
where
    F: Flavor,
{
    /// Access the package for the name.
    fn package(&self) -> &F::Package;
}

/// Implementation for default name translation.
impl<F> Name<F> for Loc<RpName<F>>
where
    F: Flavor,
{
    fn package(&self) -> &F::Package {
        &self.package
    }
}

pub trait PackageProcessor<'el, F: 'static, N: 'static>
where
    Self: 'el + Sized,
    F: Flavor<Name = N, Package = RpPackage>,
    N: Name<F>,
{
    type Out: Default + IntoBytes<Self>;
    type DeclIter: Iterator<Item = &'el RpDecl<F>>;

    /// Access the extension for processing.
    fn ext(&self) -> &str;

    /// Iterate over all existing declarations.
    fn decl_iter(&self) -> Self::DeclIter;

    fn handle(&self) -> &'el Handle;

    fn default_process(&self, _: &mut Self::Out, name: &'el F::Name) -> Result<()> {
        warn!("not supported: {}", name);
        Ok(())
    }

    fn process_interface(&self, out: &mut Self::Out, body: &'el RpInterfaceBody<F>) -> Result<()> {
        self.default_process(out, &body.name)
    }

    fn process_type(&self, out: &mut Self::Out, body: &'el RpTypeBody<F>) -> Result<()> {
        self.default_process(out, &body.name)
    }

    fn process_tuple(&self, out: &mut Self::Out, body: &'el RpTupleBody<F>) -> Result<()> {
        self.default_process(out, &body.name)
    }

    fn process_enum(&self, out: &mut Self::Out, body: &'el RpEnumBody<F>) -> Result<()> {
        self.default_process(out, &body.name)
    }

    fn process_service(&self, out: &mut Self::Out, body: &'el RpServiceBody<F>) -> Result<()> {
        self.default_process(out, &body.name)
    }

    fn populate_files(&self) -> Result<BTreeMap<F::Package, Self::Out>> {
        self.do_populate_files(|_| Ok(()))
    }

    fn do_populate_files<C>(&self, mut callback: C) -> Result<BTreeMap<F::Package, Self::Out>>
    where
        C: FnMut(&'el RpDecl<F>) -> Result<()>,
    {
        use self::RpDecl::*;

        let mut files = BTreeMap::new();

        // Process all types discovered so far.
        for decl in self.decl_iter() {
            callback(decl).and_then(|_| {
                let mut out = files
                    .entry(decl.name().package().clone())
                    .or_insert_with(Self::Out::default);

                match *decl {
                    Interface(ref b) => self.process_interface(&mut out, b),
                    Type(ref b) => self.process_type(&mut out, b),
                    Tuple(ref b) => self.process_tuple(&mut out, b),
                    Enum(ref b) => self.process_enum(&mut out, b),
                    Service(ref b) => self.process_service(&mut out, b),
                }
            })?;
        }

        Ok(files)
    }

    fn resolve_full_path(&self, package: &RpPackage) -> Result<RelativePathBuf> {
        let mut full_path = package
            .parts()
            .fold(RelativePathBuf::new(), |a, b| a.join(b));
        full_path.set_extension(self.ext());
        Ok(full_path)
    }

    fn setup_module_path(&self, package: &RpPackage) -> Result<RelativePathBuf> {
        let handle = self.handle();
        let full_path = self.resolve_full_path(package)?;

        {
            let parent = full_path.parent().unwrap_or(RelativePath::new("."));

            if !handle.is_dir(&parent) {
                debug!("+dir: {}", parent.display());
                handle.create_dir_all(&parent)?;
            }
        }

        Ok(full_path)
    }

    fn write_files(&'el self, files: BTreeMap<F::Package, Self::Out>) -> Result<()> {
        let handle = self.handle();

        for (package, out) in files {
            let full_path = self.setup_module_path(&package)?;

            debug!("+module: {}", full_path.display());

            let mut f = handle.create(&full_path)?;
            let bytes = out.into_bytes(self, &package)?;
            f.write_all(&bytes)?;
            f.flush()?;
        }

        Ok(())
    }
}