[][src]Crate mime_serde_shim

Mime crate Serde shim

To enable the Mime shim, add it as a dependency:

[dependencies]
mime_serde_shim = "0.2"

Full example:

#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate mime_serde_shim;

extern crate mime;

use mime::{Mime, TEXT_JAVASCRIPT};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct MimeTest {
    #[serde(with = "mime_serde_shim")]
    mime: Mime,
}

fn main() {
    let test = MimeTest {
        mime: TEXT_JAVASCRIPT,
    };

    let expected = r#"{"mime":"text/javascript"}"#;

    assert_eq!(serde_json::to_string(&test).unwrap(), expected);

    assert_eq!(serde_json::from_str::<MimeTest>(expected).unwrap(), test);

    assert!(serde_json::from_str::<MimeTest>("invalid").is_err());
}

Or you could use the provided Wrapper type:

#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate mime_serde_shim;
extern crate mime;

use mime_serde_shim::Wrapper as Mime;

use mime::TEXT_JAVASCRIPT;

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct MimeTest {
    mime: Option<Vec<Mime>>, // Wrapper easily supports nested types
}

fn main() {
    let test = MimeTest {
        mime: Some(vec![TEXT_JAVASCRIPT.into()]), // Note the `.into()`
    };

    let expected = r#"{"mime":["text/javascript"]}"#;

    assert_eq!(serde_json::to_string(&test).unwrap(), expected);

    assert_eq!(serde_json::from_str::<MimeTest>(expected).unwrap(), test);

    assert!(serde_json::from_str::<MimeTest>("invalid").is_err());
}

Structs

Wrapper

Wrapper type for Mime that provides Serde functionality while attempting to remain transparent.

Functions

deserialize
serialize