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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use super::{Loc, RpCode, RpDecl, RpEnumBody, RpField, RpInterfaceBody, RpServiceBody, RpSubType,
            RpTupleBody, RpTypeBody};
use super::errors::*;
use std::collections::BTreeMap;
use std::collections::btree_map;
use std::rc::Rc;

/// Merging of models.
pub trait Merge {
    /// Merge the current model with another.
    fn merge(&mut self, other: Self) -> Result<()>;
}

impl<T> Merge for Rc<T>
where
    T: Merge,
{
    fn merge(&mut self, source: Rc<T>) -> Result<()> {
        let rc = Rc::get_mut(self).ok_or(ErrorKind::RcGetMut)?;
        let source = Rc::try_unwrap(source).map_err(|_| ErrorKind::RcTryUnwrap)?;
        rc.merge(source)?;
        Ok(())
    }
}

impl<K, T> Merge for BTreeMap<K, T>
where
    T: Merge,
    K: ::std::cmp::Ord,
{
    fn merge(&mut self, source: BTreeMap<K, T>) -> Result<()> {
        for (key, value) in source {
            match self.entry(key) {
                btree_map::Entry::Vacant(entry) => {
                    entry.insert(value);
                }
                btree_map::Entry::Occupied(entry) => {
                    Merge::merge(entry.into_mut(), value)?;
                }
            }
        }

        Ok(())
    }
}

impl Merge for Vec<Loc<RpCode>> {
    fn merge(&mut self, source: Vec<Loc<RpCode>>) -> Result<()> {
        self.extend(source);
        Ok(())
    }
}

impl Merge for Loc<RpDecl> {
    fn merge(&mut self, source: Loc<RpDecl>) -> Result<()> {
        use self::RpDecl::*;

        let dest_pos = self.pos().clone();
        let m = self.as_mut();

        match *m {
            Type(ref mut body) => {
                if let Type(ref other) = *source {
                    return body.merge(other.clone());
                }
            }
            Enum(ref mut body) => {
                if let Enum(ref other) = *source {
                    if let Some(variant) = other.variants.iter().next() {
                        return Err(
                            ErrorKind::ExtendEnum(
                                "cannot extend enum with additional variants".to_owned(),
                                variant.pos().into(),
                                dest_pos.into(),
                            ).into(),
                        );
                    }

                    return body.merge(other.clone());
                }
            }
            Interface(ref mut body) => {
                if let Interface(ref other) = *source {
                    return body.merge(other.clone());
                }
            }
            Tuple(ref mut body) => {
                if let Tuple(ref other) = *source {
                    return body.merge(other.clone());
                }
            }
            Service(ref mut body) => {
                if let Service(ref other) = *source {
                    return body.merge(other.clone());
                }
            }
        }

        return Err(
            ErrorKind::DeclMerge(
                format!("cannot merge with {}", source),
                source.pos().into(),
                dest_pos.into(),
            ).into(),
        );
    }
}

impl Merge for RpEnumBody {
    fn merge(&mut self, source: RpEnumBody) -> Result<()> {
        self.codes.merge(source.codes)?;
        Ok(())
    }
}

impl Merge for Vec<Loc<RpField>> {
    fn merge(&mut self, source: Vec<Loc<RpField>>) -> Result<()> {
        for f in source {
            if let Some(field) = self.iter().find(|e| e.name == f.name) {
                return Err(
                    ErrorKind::FieldConflict(
                        f.name.to_string(),
                        f.pos().into(),
                        field.pos().into(),
                    ).into(),
                );
            }

            self.push(f);
        }

        Ok(())
    }
}

impl Merge for RpInterfaceBody {
    fn merge(&mut self, source: RpInterfaceBody) -> Result<()> {
        self.fields.merge(source.fields)?;
        self.codes.merge(source.codes)?;
        self.sub_types.merge(source.sub_types)?;
        Ok(())
    }
}

impl Merge for RpServiceBody {
    fn merge(&mut self, _source: RpServiceBody) -> Result<()> {
        Ok(())
    }
}

impl Merge for RpSubType {
    fn merge(&mut self, source: RpSubType) -> Result<()> {
        self.fields.merge(source.fields)?;
        self.codes.merge(source.codes)?;
        self.names.extend(source.names);
        Ok(())
    }
}

impl Merge for RpTupleBody {
    fn merge(&mut self, source: RpTupleBody) -> Result<()> {
        self.fields.merge(source.fields)?;
        self.codes.merge(source.codes)?;
        Ok(())
    }
}

impl Merge for RpTypeBody {
    fn merge(&mut self, source: RpTypeBody) -> Result<()> {
        self.fields.merge(source.fields)?;
        self.codes.merge(source.codes)?;
        Ok(())
    }
}