ebarnard / rust-plist

A rusty plist parser.
MIT License
71 stars 42 forks source link

Parsing From String Not File #25

Closed oscartbeaumont closed 6 years ago

oscartbeaumont commented 6 years ago

How Would You Go About Parsing The Plist File From A String Instead Of Loading It From A File On The Disk. I Have A Webserver (Rocket.rs) And Would Like To Be Able To Parse The Data From It Without Saving Then Immediately Loading The File Again Which Would Be Inefficient.

ebarnard commented 6 years ago

You can use a Cursor which implements Read and Seek for an underlying Vec<u8> or &[u8]. If you have a String or &str you can get a reference to the underlying &[u8] by calling as_bytes().

oscartbeaumont commented 6 years ago

Thanks. Another Question, Is There A Way To Supply Multiple Structs And Have It Find The Struct That Matches The Plist's Data And Return The Data In That Struct. If This Isn't Possible I Will Just Have To Use Options for All Of The Possible Fields Instead Of Having Multiple Structs.

ebarnard commented 6 years ago

Is there a tag or field indicating which type should be used? If not the best you can do is use optional fields or, if there are only a few possible types, maybe try decoding each type one by one.

ebarnard commented 6 years ago

I'd take a look at Serde's enum representations. Specifically the untagged representation.

oscartbeaumont commented 6 years ago

I Am Using The Struct In The Code Below And The Plist Parsing Works Perfectly. I Am Having Trouble Being Able To Access Its Fields I Get An Error Saying That The Field Doesn't Exist. From My Research It Seems That A Match Statement Must Be Used But How Can I Use A Match Statement Without Knowing The Data That The Enum Fields Hold.

Broken Match Statement:

match payload {
        MDMCheckin::Authenticate {/* Needs The enums Data */} => println!("Authenticate: {}", value),
        MDMCheckin::TokenUpdate {/* Needs The enums Data */} => println!("TokenUpdate: {}", value),
        MDMCheckin::CheckOut {/* Needs The enums Data */} => println!("CheckOut: {}", value),
        _ => println!("Error"),
    }

Explaining What I Mean By Enum Data In The Last Code Snipit

MDMCheckin::Authenticate { BuildVersion: "1.0.0", IMEI: "Data Here", /* etc */ } => println!("Authenticate: {}", value),

Enum Being Used For Parsing:

#[serde(untagged)]
enum MDMCheckin {
    Authenticate {
        BuildVersion: String,
        IMEI: String,
        MEID: String,
        MessageType: String,
        OSVersion: String,
        ProductName: String,
        SerialNumber: String,
        Topic: String,
        UDID: String
    },
    TokenUpdate {
        AwaitingConfiguration: bool,
        MessageType: String,
        PushMagic: String,
        Token: String,
        Topic: String,
        UDID: String,
        UnlockToken: String
    },
    CheckOut {
        MessageType: String,
        Topic: String,
        UDID: String
    }
}

Thanks For All Your Help With This.

ebarnard commented 6 years ago

You don't seem to have defined value anywhere. I suggest you take a look at the section of the Rust book about enums.

I'm going to close this issue as it seems like your problem specifically with the plist library has been resolved.