serde_magnus 0.1.0

Serde integration for Magnus
Documentation
serde_magnus-0.1.0 has been yanked.

serde_magnus

serde_magnus converts between Rust and Ruby data structures using Serde and Magnus.

Quick links

Usage

The serde_magnus::serialize function converts from a Rust type implementing the serde::Serialize trait into a Ruby equivalent.

use serde::Serialize;
use magnus::{eval, Value};
use serde_magnus::serialize;

#[derive(Serialize)]
struct Post {
    title: String,
    content: String,
    author: Author,
    tags: Vec<String>
}

#[derive(Serialize)]
struct Author {
    name: String,
    email_address: String
}

let post = Post {
    title: String::from("Spring carnival planning update"),
    content: String::from("Here's what's new."),
    author: Author {
        name: String::from("Martha"),
        email_address: String::from("martha@example.com")
    },
    tags: vec![
        String::from("carnival"),
        String::from("update")
    ]
};

let post: Value = serialize(&post).unwrap();

// Output:
//
//     {
//       title: "Spring carnival planning update",
//       content: "Here's what's new.",
//       author: {
//         name: "Martha",
//         email_address: "martha@example.com"
//       },
//       tags: ["carnival", "update"]
//     }
let _: Value = eval!("pp post", post).unwrap();

serde_magnus::deserialize converts from a Ruby value to a Rust type implementing serde::Deserialize.

use magnus::RHash;
use serde_magnus::Deserialize;

let post: RHash = eval!(r#"
  {
    title: "Spring carnival planning update",
    content: "Here's what's new.",
    author: {
      name: "Martha",
      email_address: "martha@example.com"
    },
    tags: ["carnival", "update"]
  }
"#).unwrap();

let post: Post = deserialize(post).unwrap();

// Output:
//
//     Post {
//         title: "Spring carnival planning update",
//         content: "Here's what's new.",
//         author: Author {
//             name: "Martha",
//             email_address: "martha@example.com"
//         },
//         tags: ["carnival", "update"]
//     }
println!("{:?}", post);

Requirements

serde_magnus requires Rust 1.51+ and Ruby 2.6+.

License

serde_magnus is released under the terms of the MIT License. See LICENSE for details.