pub fn parse_rpsl_object(rpsl: &str) -> Result<Object, Error<&str>>
👎Deprecated since 0.1.4: rpsl-parser was renamed to rpsl-rs, please use that crate instead.
Expand description
Parse a string containing a RPSL object.
§Errors
Returns nom Error
if any error occurs during parsing.
§Examples
let role_acme = "
role: ACME Company
address: Packet Street 6
address: 128 Series of Tubes
address: Internet
email: rpsl-parser@github.com
nic-hdl: RPSL1-RIPE
source: RIPE
";
let parsed_attributes = parse_rpsl_object(role_acme)?;
assert_eq!(
parsed_attributes,
rpsl::Object::new(vec![
rpsl::Attribute::new("role".to_string(), vec![Some("ACME Company".to_string())]),
rpsl::Attribute::new(
"address".to_string(),
vec![Some("Packet Street 6".to_string())],
),
rpsl::Attribute::new(
"address".to_string(),
vec![Some("128 Series of Tubes".to_string())],
),
rpsl::Attribute::new("address".to_string(), vec![Some("Internet".to_string())]),
rpsl::Attribute::new(
"email".to_string(),
vec![Some("rpsl-parser@github.com".to_string())],
),
rpsl::Attribute::new("nic-hdl".to_string(), vec![Some("RPSL1-RIPE".to_string())]),
rpsl::Attribute::new("source".to_string(), vec![Some("RIPE".to_string())]),
])
);
Values spread over multiple lines can be parsed too.
let multi_value = "
remarks: Value 1
Value 2
";
assert_eq!(
parse_rpsl_object(multi_value)?,
rpsl::Object::new(vec![rpsl::Attribute::new(
"remarks".to_string(),
vec![Some("Value 1".to_string()), Some("Value 2".to_string())]
),])
);
Empty values are valid RPSL and are represented as None
.
let empty_value = "
as-name: REMARKABLE
remarks:
remarks: ^^^^^^^^^^ nothing here
";
assert_eq!(
parse_rpsl_object(empty_value)?,
rpsl::Object::new(vec![
rpsl::Attribute::new("as-name".to_string(), vec![Some("REMARKABLE".to_string())]),
rpsl::Attribute::new("remarks".to_string(), vec![None]),
rpsl::Attribute::new(
"remarks".to_string(),
vec![Some("^^^^^^^^^^ nothing here".to_string())]
),
])
);
The same goes for values containing only whitespace. Since whitespace to the left of a value is trimmed, they are equivalent to an empty value.
let whitespace_value = "
as-name: REMARKABLE
remarks:
remarks: ^^^^^^^^^^ nothing but hot air
";
assert_eq!(
parse_rpsl_object(whitespace_value)?,
rpsl::Object::new(vec![
rpsl::Attribute::new("as-name".to_string(), vec![Some("REMARKABLE".to_string())]),
rpsl::Attribute::new("remarks".to_string(), vec![None]),
rpsl::Attribute::new(
"remarks".to_string(),
vec![Some("^^^^^^^^^^ nothing but hot air".to_string())]
),
])
);