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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use super::attribute::AttributeView;
use std::{fmt, ops::Index};

/// A view into an RPSL object in textual representation somewhere in memory.
///
/// This is the borrowed equivalent of an [`Object`], only containing references to the
/// original data in the form of [`AttributeView`]s. It presents largely the same interface as
/// its owned equivalent, although it will always return references.
///
///
/// ```text
/// role:           ACME Company ◀─────────────── &"role"    ───  &"ACME Company"
/// address:        Packet Street 6 ◀──────────── &"address" ─┬─  &"Packet Street 6"
///                 128 Series of Tubes ◀────────             ├─  &"128 Series of Tubes"
///                 Internet ◀───────────────────             └─  &"Internet"
/// email:          rpsl-rs@github.com ◀───────── &"email"   ───  &"rpsl-rs@github.com"
/// nic-hdl:        RPSL1-RIPE ◀───────────────── &"nic-hdl" ───  &"RPSL1-RIPE"
/// source:         RIPE ◀─────────────────────── &"source"  ───  &"RIPE"
/// ```
///
/// Since an [`ObjectView`] is purely used to provide a view into referenced RPSL data, it can only
/// be created from RPSL text using the [`parse_object`] and [`parse_whois_response`](crate::parse_whois_response) functions.
///
/// # Examples
///  
/// Like an owned [`Object`], its attributes can be accessed by index.
/// ```
/// # use rpsl::{parse_object, Attribute};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let role_acme = parse_object("
/// # role:           ACME Company
/// # address:        Packet Street
/// #                 128 Series of Tubes
/// #                 Internet
/// # email:          rpsl-rs@github.com
/// # nic-hdl:        RPSL1-RIPE
/// # source:         RIPE
/// #
/// # ")?;
/// assert_eq!(role_acme[0], Attribute::new("role".parse()?, "ACME Company".parse()?));
/// assert_eq!(role_acme[3], Attribute::new("nic-hdl".parse()?, "RPSL1-RIPE".parse()?));
/// # Ok(())
/// # }
/// ```
///
/// While specific attribute values can be accessed by name.
/// ```
/// # use rpsl::{parse_object, Attribute};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let role_acme = parse_object("
/// # role:           ACME Company
/// # address:        Packet Street 6
/// #                 128 Series of Tubes
/// #                 Internet
/// # email:          rpsl-rs@github.com
/// # nic-hdl:        RPSL1-RIPE
/// # source:         RIPE
/// #
/// # ")?;
/// assert_eq!(role_acme.get("role"), vec!["ACME Company"]);
/// assert_eq!(role_acme.get("address"), vec!["Packet Street 6", "128 Series of Tubes", "Internet"]);
/// assert_eq!(role_acme.get("email"), vec!["rpsl-rs@github.com"]);
/// assert_eq!(role_acme.get("nic-hdl"), vec!["RPSL1-RIPE"]);
/// assert_eq!(role_acme.get("source"), vec!["RIPE"]);
/// # Ok(())
/// # }
/// ```
///
/// Views can be compared to their owned equivalents.
/// ```
/// # use rpsl::{parse_object, Attribute, object};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let role_acme = parse_object("
/// # role:           ACME Company
/// # address:        Packet Street 6
/// #                 128 Series of Tubes
/// #                 Internet
/// # email:          rpsl-rs@github.com
/// # nic-hdl:        RPSL1-RIPE
/// # source:         RIPE
/// #
/// # ")?;
/// assert_eq!(
///     role_acme,
///     object! {
///         "role": "ACME Company";
///         "address": "Packet Street 6", "128 Series of Tubes", "Internet";
///         "email": "rpsl-rs@github.com";
///         "nic-hdl": "RPSL1-RIPE";
///         "source": "RIPE";
///      },
/// );
/// # Ok(())
/// # }
/// ```
///
/// As well as converted to them if required.
/// ```
/// # use rpsl::{parse_object, Attribute};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let role_acme = parse_object("
/// # role:           ACME Company
/// # address:        Packet Street 6
/// #                 128 Series of Tubes
/// #                 Internet
/// # email:          rpsl-rs@github.com
/// # nic-hdl:        RPSL1-RIPE
/// # source:         RIPE
/// #
/// # ")?;
/// role_acme.to_owned();
/// # Ok(())
/// # }
/// ```
/// [`Object`]: crate::Object
/// [`parse_object`]: crate::parse_object
/// [`parse_whois_response`]: crate::parse_whois_response
#[derive(PartialEq, Eq, Clone)]
#[allow(clippy::len_without_is_empty)]
pub struct ObjectView<'a> {
    attributes: Vec<AttributeView<'a>>,
    /// The original RPSL text that was parsed to create this view.
    source: Option<&'a str>,
}

impl<'a> ObjectView<'a> {
    pub(crate) fn new(attributes: Vec<AttributeView<'a>>, source: Option<&'a str>) -> Self {
        Self {
            attributes,
            source: source.map(str::trim),
        }
    }

    /// Turn the view into an owned [`Object`](crate::Object).
    pub fn to_owned(&self) -> crate::Object {
        crate::Object::new(
            self.attributes
                .iter()
                .map(AttributeView::to_owned)
                .collect(),
        )
    }

    /// The number of attributes referenced within the view.
    #[must_use]
    pub fn len(&self) -> usize {
        self.attributes.len()
    }

    /// Get the value(s) of specific attribute(s).
    pub fn get(&self, name: &str) -> Vec<&str> {
        let values_matching_name = self
            .attributes
            .iter()
            .filter(|a| a.name == name)
            .map(|a| &a.value);

        let mut values: Vec<&str> = Vec::new();
        for value in values_matching_name {
            match value {
                super::attribute::ValueView::SingleLine(v) => {
                    if let Some(v) = v {
                        values.push(v);
                    }
                }
                super::attribute::ValueView::MultiLine(v) => {
                    values.extend(v.iter().filter_map(Option::as_ref));
                }
            }
        }
        values
    }
}

impl PartialEq<crate::Object> for ObjectView<'_> {
    fn eq(&self, other: &crate::Object) -> bool {
        // TODO: Avoid cloning
        for (s, o) in self.clone().into_iter().zip(other.clone().into_iter()) {
            if s != o {
                return false;
            }
        }
        true
    }
}

impl<'a> Index<usize> for ObjectView<'a> {
    type Output = AttributeView<'a>;

    fn index(&self, index: usize) -> &Self::Output {
        &self.attributes[index]
    }
}

impl<'a> IntoIterator for ObjectView<'a> {
    type Item = AttributeView<'a>;
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.attributes.into_iter()
    }
}

impl fmt::Debug for ObjectView<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:#?}", self.attributes)
    }
}

impl fmt::Display for ObjectView<'_> {
    /// Display the view as RPSL.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(source) = self.source {
            writeln!(f, "{source}")?;
            writeln!(f)?;
        } else {
            // If the source is not available, fall back to the implementation of the owned type.
            write!(f, "{}", self.to_owned())?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn eq_owned_object_is_eq() {
        let borrowed = ObjectView::new(
            vec![
                AttributeView::new_single("role", "ACME Company"),
                AttributeView::new_single("address", "Packet Street 6"),
                AttributeView::new_single("address", "128 Series of Tubes"),
                AttributeView::new_single("address", "Internet"),
            ],
            None,
        );
        let owned = crate::Object::new(vec![
            crate::Attribute::new("role".parse().unwrap(), "ACME Company".parse().unwrap()),
            crate::Attribute::new(
                "address".parse().unwrap(),
                "Packet Street 6".parse().unwrap(),
            ),
            crate::Attribute::new(
                "address".parse().unwrap(),
                "128 Series of Tubes".parse().unwrap(),
            ),
            crate::Attribute::new("address".parse().unwrap(), "Internet".parse().unwrap()),
        ]);
        assert_eq!(borrowed, owned);
    }

    #[test]
    fn ne_owned_object_is_ne() {
        let borrowed = ObjectView::new(
            vec![
                AttributeView::new_single("role", "Umbrella Corporation"),
                AttributeView::new_single("address", "Paraguas Street"),
                AttributeView::new_single("address", "Raccoon City"),
                AttributeView::new_single("address", "Colorado"),
            ],
            None,
        );
        let owned = crate::Object::new(vec![
            crate::Attribute::new("role".parse().unwrap(), "ACME Company".parse().unwrap()),
            crate::Attribute::new(
                "address".parse().unwrap(),
                "Packet Street 6".parse().unwrap(),
            ),
            crate::Attribute::new(
                "address".parse().unwrap(),
                "128 Series of Tubes".parse().unwrap(),
            ),
            crate::Attribute::new("address".parse().unwrap(), "Internet".parse().unwrap()),
        ]);
        assert_ne!(borrowed, owned);
    }
}