3个不稳定版本

0.8.1 2024年1月26日
0.8.0 2023年10月19日
0.7.0 2023年7月6日
0.2.2 2023年3月22日
0.1.1 2023年1月22日

#54 in FFI

Download history 682/week @ 2024-04-22 363/week @ 2024-04-29 442/week @ 2024-05-06 238/week @ 2024-05-13 784/week @ 2024-05-20 905/week @ 2024-05-27 866/week @ 2024-06-03 523/week @ 2024-06-10 378/week @ 2024-06-17 398/week @ 2024-06-24 553/week @ 2024-07-01 415/week @ 2024-07-08 265/week @ 2024-07-15 1293/week @ 2024-07-22 868/week @ 2024-07-29 1092/week @ 2024-08-05

3,519 每月下载量
用于 pks

MIT 许可证

55KB
807

serde_magnus

serde_magnus 使用 SerdeMagnus 在Rust和Ruby数据结构之间进行转换。

快速链接

用法

serde_magnus::serialize 函数将实现了 serde::Serialize 特性的Rust类型转换为Ruby等价类型。

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

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

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

let post = Post {
    title: "Spring carnival planning update".into(),
    content: "Here's what's new.".into(),
    author: Author {
        name: "Martha".into(),
        email_address: "[email protected]".into()
    },
    tags: vec![
        "carnival".into(),
        "update".into()
    ]
};

let post: Value = serialize(&post)?;

assert!(eval!(
    r#"
    post == {
      title: "Spring carnival planning update",
      content: "Here's what's new.",
      author: {
        name: "Martha",
        email_address: "[email protected]"
      },
      tags: ["carnival", "update"]
    }
    "#,
    post
)?);

serde_magnus::deserialize 将Ruby值转换为实现了 serde::Deserialize 的Rust类型。

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: "[email protected]"
    },
    tags: ["carnival", "update"]
  }
"#)?;

let post: Post = deserialize(post)?;

assert_eq!(
    Post {
        title: "Spring carnival planning update".into(),
        content: "Here's what's new.".into(),
        author: Author {
            name: "Martha".into(),
            email_address: "[email protected]".into(),
        },
        tags: vec![
            "carnival".into(),
            "update".into()
        ]
    },
    post
);

要求

serde_magnus 需要 Rust 1.65+ 和 Ruby 3.0+。

许可证

serde_magnus 在MIT许可证的条款下发布。有关详细信息,请参阅 LICENSE

依赖项

~1.5–4.5MB
~73K SLoC