Function rpsl::parse_object

source ·
pub fn parse_object(rpsl: &str) -> Result<ObjectView<'_>, Error<&str>>
Expand description

Parse RPSL into an ObjectView, a type that borrows from the RPSL input and provides a convenient interface to access attributes as references.

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
                       ↓
role:           ACME Company ◀─────────────── &"role"    ───  &"ACME Company"
address:        Packet Street 6 ◀──────────── &"address" ───  &"Packet Street 6"
address:        128 Series of Tubes ◀──────── &"address" ───  &"128 Series of Tubes"
address:        Internet ◀─────────────────── &"address" ───  &"Internet"
email:          rpsl-rs@github.com ◀───── &"email"   ───  &"rpsl-rs@github.com"
nic-hdl:        RPSL1-RIPE ◀───────────────── &"nic-hdl" ───  &"RPSL1-RIPE"
source:         RIPE ◀─────────────────────── &"source"  ───  &"RIPE"

Errors

Returns a Nom error if the input is not valid RPSL.

Examples

let role_acme = "
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

";
let parsed = parse_object(role_acme)?;
assert_eq!(
    parsed,
    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";
    }
);

Values spread over multiple lines can be parsed too.

let multiline_remark = "
remarks:     Value 1
             Value 2

";
assert_eq!(
    parse_object(multiline_remark)?,
    object! {
        "remarks": "Value 1", "Value 2";
    }
);

An attribute that does not have a value is valid.

let without_value = "
as-name:     REMARKABLE
remarks:
remarks:     ^^^^^^^^^^ nothing here

";
assert_eq!(
    parse_object(without_value)?,
    object! {
        "as-name": "REMARKABLE";
        "remarks": "";
        "remarks": "^^^^^^^^^^ nothing here";
    }
);

The same goes for values containing only whitespace. Since whitespace to the left of a value is trimmed, they are equivalent to no value.

let whitespace_value = "
as-name:     REMARKABLE
remarks:               
remarks:     ^^^^^^^^^^ nothing but hot air

";
assert_eq!(
    parse_object(whitespace_value)?,
    object! {
        "as-name": "REMARKABLE";
        "remarks": "";
        "remarks": "^^^^^^^^^^ nothing but hot air";
    }
);