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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
use super::Attribute;
use std::{fmt, ops::Index};
/// A RPSL object.
///
/// ```text
/// ┌───────────────────────────────────────────────┐
/// │ Object │
/// ├───────────────────────────────────────────────┤
/// │ [role] ─── ACME Company │
/// │ [address] ──┬─ Packet Street 6 │
/// │ ├─ 128 Series of Tubes │
/// │ └─ Internet │
/// │ [email] ─── rpsl-rs@github.com │
/// │ [nic-hdl] ─── RPSL1-RIPE │
/// │ [source] ─── RIPE │
/// └───────────────────────────────────────────────┘
/// ```
///
/// # Examples
///
/// A role object for the ACME corporation.
/// ```
/// # use rpsl::{Attribute, Object};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let role_acme = Object::new(vec![
/// Attribute::new("role".parse()?, "ACME Company".parse()?),
/// Attribute::new("address".parse()?, "Packet Street 6".parse()?),
/// Attribute::new("address".parse()?, "128 Series of Tubes".parse()?),
/// Attribute::new("address".parse()?, "Internet".parse()?),
/// Attribute::new("email".parse()?, "rpsl-rs@github.com".parse()?),
/// Attribute::new("nic-hdl".parse()?, "RPSL1-RIPE".parse()?),
/// Attribute::new("source".parse()?, "RIPE".parse()?),
/// ]);
/// # Ok(())
/// # }
/// ```
///
/// Although creating an [`Object`] from a vector of [`Attribute`]s works, the more idiomatic way
/// to do it is by using the [`object!`](crate::object) macro.
/// ```
/// # use rpsl::{Attribute, Object, object};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let role_acme = Object::new(vec![
/// # Attribute::new("role".parse()?, "ACME Company".parse()?),
/// # Attribute::new("address".parse()?, "Packet Street 6".parse()?),
/// # Attribute::new("address".parse()?, "128 Series of Tubes".parse()?),
/// # Attribute::new("address".parse()?, "Internet".parse()?),
/// # Attribute::new("email".parse()?, "rpsl-rs@github.com".parse()?),
/// # Attribute::new("nic-hdl".parse()?, "RPSL1-RIPE".parse()?),
/// # Attribute::new("source".parse()?, "RIPE".parse()?),
/// # ]);
/// assert_eq!(
/// role_acme,
/// object! {
/// "role": "ACME Company";
/// "address": "Packet Street 6";
/// "address": "128 Series of Tubes";
/// "address": "Internet";
/// "email": "rpsl-rs@github.com";
/// "nic-hdl": "RPSL1-RIPE";
/// "source": "RIPE";
/// },
/// );
/// # Ok(())
/// # }
/// ```
///
/// Each attribute can be accessed by index.
/// ```
/// # use rpsl::{Attribute, Object};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let role_acme = Object::new(vec![
/// # Attribute::new("role".parse()?, "ACME Company".parse()?),
/// # Attribute::new("address".parse()?, "Packet Street 6".parse()?),
/// # Attribute::new("address".parse()?, "128 Series of Tubes".parse()?),
/// # Attribute::new("address".parse()?, "Internet".parse()?),
/// # Attribute::new("email".parse()?, "rpsl-rs@github.com".parse()?),
/// # Attribute::new("nic-hdl".parse()?, "RPSL1-RIPE".parse()?),
/// # Attribute::new("source".parse()?, "RIPE".parse()?),
/// # ]);
/// assert_eq!(role_acme[0], Attribute::new("role".parse()?, "ACME Company".parse()?));
/// assert_eq!(role_acme[6], Attribute::new("source".parse()?, "RIPE".parse()?));
/// # Ok(())
/// # }
/// ```
///
/// While specific attribute values can be accessed by name.
/// ```
/// # use rpsl::{Attribute, Object};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let role_acme = Object::new(vec![
/// # Attribute::new("role".parse()?, "ACME Company".parse()?),
/// # Attribute::new("address".parse()?, "Packet Street 6".parse()?),
/// # Attribute::new("address".parse()?, "128 Series of Tubes".parse()?),
/// # Attribute::new("address".parse()?, "Internet".parse()?),
/// # Attribute::new("email".parse()?, "rpsl-rs@github.com".parse()?),
/// # Attribute::new("nic-hdl".parse()?, "RPSL1-RIPE".parse()?),
/// # Attribute::new("source".parse()?, "RIPE".parse()?),
/// # ]);
/// 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(())
/// # }
/// ```
///
/// The entire object can also be represented as RPSL.
/// ```
/// # use rpsl::{Attribute, Object};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let role_acme = Object::new(vec![
/// # Attribute::new("role".parse()?, "ACME Company".parse()?),
/// # Attribute::new("address".parse()?, "Packet Street 6".parse()?),
/// # Attribute::new("address".parse()?, "128 Series of Tubes".parse()?),
/// # Attribute::new("address".parse()?, "Internet".parse()?),
/// # Attribute::new("email".parse()?, "rpsl-rs@github.com".parse()?),
/// # Attribute::new("nic-hdl".parse()?, "RPSL1-RIPE".parse()?),
/// # Attribute::new("source".parse()?, "RIPE".parse()?),
/// # ]);
/// assert_eq!(
/// role_acme.to_string(),
/// concat!(
/// "role: ACME Company\n",
/// "address: Packet Street 6\n",
/// "address: 128 Series of Tubes\n",
/// "address: Internet\n",
/// "email: rpsl-rs@github.com\n",
/// "nic-hdl: RPSL1-RIPE\n",
/// "source: RIPE\n",
/// "\n"
/// )
/// );
/// # Ok(())
/// # }
/// ```
#[derive(Debug, PartialEq, Eq, Clone)]
#[allow(clippy::len_without_is_empty)]
pub struct Object(Vec<Attribute>);
impl Object {
/// Create a new RPSL object from a vector of attributes.
///
/// # Example
/// ```
/// # use rpsl::{Attribute, Object};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let role_acme = Object::new(vec![
/// Attribute::new("role".parse()?, "ACME Company".parse()?),
/// Attribute::new("address".parse()?, "Packet Street 6".parse()?),
/// Attribute::new("address".parse()?, "128 Series of Tubes".parse()?),
/// Attribute::new("address".parse()?, "Internet".parse()?),
/// Attribute::new("email".parse()?, "rpsl-rs@github.com".parse()?),
/// Attribute::new("nic-hdl".parse()?, "RPSL1-RIPE".parse()?),
/// Attribute::new("source".parse()?, "RIPE".parse()?),
/// ]);
/// # Ok(())
/// # }
/// ```
#[must_use]
pub fn new(attributes: Vec<Attribute>) -> Self {
Object(attributes)
}
/// The number of attributes in the object.
#[must_use]
pub fn len(&self) -> usize {
self.0.len()
}
/// Get the value(s) of specific attribute(s).
pub fn get(&self, name: &str) -> Vec<&String> {
let values_matching_name = self.0.iter().filter(|a| a.name == name).map(|a| &a.value);
let mut values: Vec<&String> = Vec::new();
for value in values_matching_name {
match value {
super::attribute::Value::SingleLine(ref v) => {
if let Some(v) = v.as_ref() {
values.push(v);
}
}
super::attribute::Value::MultiLine(ref v) => {
values.extend(v.iter().filter_map(Option::as_ref));
}
}
}
values
}
}
impl Index<usize> for Object {
type Output = Attribute;
fn index(&self, index: usize) -> &Self::Output {
&self.0[index]
}
}
impl IntoIterator for Object {
type Item = Attribute;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl fmt::Display for Object {
/// Display the object as RPSL.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for attribute in &self.0 {
write!(f, "{attribute}")?;
}
writeln!(f)
}
}
/// Creates an [`Object`] containing the given attributes.
///
/// - Create an [`Object`] containing only single value attributes:
/// ```
/// # use rpsl::object;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let obj = object! {
/// "role": "ACME Company";
/// "address": "Packet Street 6";
/// "address": "128 Series of Tubes";
/// "address": "Internet";
/// };
/// assert_eq!(obj[0].name, "role");
/// assert_eq!(obj[0].value, "ACME Company");
/// assert_eq!(obj[1].name, "address");
/// assert_eq!(obj[1].value, "Packet Street 6");
/// assert_eq!(obj[2].name, "address");
/// assert_eq!(obj[2].value, "128 Series of Tubes");
/// assert_eq!(obj[3].name, "address");
/// assert_eq!(obj[3].value, "Internet");
/// # Ok(())
/// # }
/// ```
///
/// - Create an `Object` containing multi value attributes:
/// ```
/// # use rpsl::object;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let obj = object! {
/// "role": "ACME Company";
/// "address": "Packet Street 6", "128 Series of Tubes", "Internet";
/// };
/// assert_eq!(obj[0].name, "role");
/// assert_eq!(obj[0].value, "ACME Company");
/// assert_eq!(obj[1].name, "address");
/// assert_eq!(obj[1].value, vec!["Packet Street 6", "128 Series of Tubes", "Internet"]);
/// # Ok(())
/// # }
#[macro_export]
macro_rules! object {
(
$(
$name:literal: $($value:literal),+
);+ $(;)?
) => {
$crate::Object::new(vec![
$(
$crate::Attribute::new($name.parse().unwrap(), vec![$($value),+].try_into().unwrap()),
)*
])
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn object_from_macro() {
let object = object! {
"role": "ACME Company";
"address": "Packet Street 6", "128 Series of Tubes", "Internet";
"email": "rpsl-rs@github.com";
"nic-hdl": "RPSL1-RIPE";
"source": "RIPE";
};
let role_acme = Object::new(vec![
Attribute::new("role".parse().unwrap(), "ACME Company".parse().unwrap()),
Attribute::new(
"address".parse().unwrap(),
vec!["Packet Street 6", "128 Series of Tubes", "Internet"]
.try_into()
.unwrap(),
),
Attribute::new(
"email".parse().unwrap(),
"rpsl-rs@github.com".parse().unwrap(),
),
Attribute::new("nic-hdl".parse().unwrap(), "RPSL1-RIPE".parse().unwrap()),
Attribute::new("source".parse().unwrap(), "RIPE".parse().unwrap()),
]);
assert_eq!(object, role_acme);
}
#[test]
fn values_by_name() {
let as42 =
Object::new(vec![
Attribute::new("aut-num".parse().unwrap(), "AS42".parse().unwrap()),
Attribute::new(
"remarks".parse().unwrap(),
"All imported prefixes will be tagged with geographic communities and"
.parse()
.unwrap(),
),
Attribute::new(
"remarks".parse().unwrap(),
"the type of peering relationship according to the table below, using the default"
.parse()
.unwrap(),
),
Attribute::new(
"remarks".parse().unwrap(),
"announce rule (x=0).".parse().unwrap(),
),
Attribute::new("remarks".parse().unwrap(), "".parse().unwrap()),
Attribute::new(
"remarks".parse().unwrap(),
"The following communities can be used by peers and customers".parse().unwrap(),
),
Attribute::new(
"remarks".parse().unwrap(),
vec![
"x = 0 - Announce (default rule)",
"x = 1 - Prepend x1",
"x = 2 - Prepend x2",
"x = 3 - Prepend x3",
"x = 9 - Do not announce",
].try_into().unwrap(),
),
]);
assert_eq!(as42.get("aut-num"), vec!["AS42"]);
assert_eq!(
as42.get("remarks"),
vec![
"All imported prefixes will be tagged with geographic communities and",
"the type of peering relationship according to the table below, using the default",
"announce rule (x=0).",
"The following communities can be used by peers and customers",
"x = 0 - Announce (default rule)",
"x = 1 - Prepend x1",
"x = 2 - Prepend x2",
"x = 3 - Prepend x3",
"x = 9 - Do not announce",
]
);
}
}