18个版本

0.7.0 2024年6月15日
0.5.0 2020年9月30日
0.4.3 2020年9月19日
0.3.7 2020年9月13日
0.1.2 2020年8月16日

#1149 in 过程宏

Download history 4/week @ 2024-04-24 2/week @ 2024-05-01 4/week @ 2024-05-08 1/week @ 2024-05-15 9/week @ 2024-05-22 4/week @ 2024-05-29 5/week @ 2024-06-05 192/week @ 2024-06-12 8/week @ 2024-06-19 1/week @ 2024-06-26 7/week @ 2024-07-17 112/week @ 2024-07-24 10/week @ 2024-07-31

每月下载量129次
用于 3 crates

LGPL-3.0

17KB
326

弃用通知

此crate及其主要crate edn-rs 将不再进行任何工作。

edn-derive

Edn (De)序列化衍生过程宏。

此库仍然是 beta 版,但非常接近稳定版.

使用方法

[dependencies]
edn-derive = "0.7.0"

示例

序列化

use edn_derive::Serialize;

#[derive(Serialize)]
enum Kind {
    Cool,
    Chill,
    Pirate,
}

#[derive(Serialize)]
pub struct Person {
    name: String,
    age: usize,
    kind: Kind,
}

fn main() {
    let person = Person {
        name: "joana".to_string(),
        age: 290000,
        kind: Kind::Chill,
    };
    assert_eq!(
        edn_rs::to_string(person),
        "{ :name \"joana\", :age 290000, :kind :kind/chill, }"
    );
}

反序列化

use edn_derive::Deserialize;
use edn_rs::EdnError;

// The `Debug` and `PartialEq` are only necessary because of `assert_eq`, you don't need them
#[derive(Deserialize, Debug, PartialEq)]
enum Kind {
    Cool,
    Chill,
    Pirate,
}

// The `Debug` and `PartialEq` are only necessary because of `assert_eq`, you don't need them
#[derive(Deserialize, Debug, PartialEq)]
pub struct Person {
    name: String,
    age: usize,
    kind: Kind,
}

fn main() -> Result<(), EdnError> {
    let edn_person = "{ :name \"joana\", :age 290000, :kind :kind/pirate, }";

    let person: Person = edn_rs::from_str(edn_person)?;

    assert_eq!(
        person,
        Person {
            name: "joana".to_string(),
            age: 290000,
            kind: Kind::Pirate,
        }
    );

    Ok(())
}

使用 .-/ 在EDN转换上添加更多复杂度

use edn_derive::{Deserialize, Serialize};
use edn_rs::EdnError;

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
enum AccountType {
    Basic,
    Premium,
    PremiumPlus,
}

#[allow(non_snake_case)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
struct Account {
    crux__db___id: String,
    account___amount: usize,
    account_type: AccountType,
}

fn main() -> Result<(), EdnError> {
    let account = Account {
        crux__db___id: "123".to_string(),
        account___amount: 42,
        account_type: AccountType::PremiumPlus,
    };

    let account_edn_str =
        "{ :crux.db/id \"123\", :account/amount 42, :account-type :account-type/premium-plus, }";

    assert_eq!(edn_rs::to_string(account), account_edn_str);

    let account: Account = edn_rs::from_str(account_edn_str)?;

    assert_eq!(
        account,
        Account {
            crux__db___id: "123".to_string(),
            account___amount: 42,
            account_type: AccountType::PremiumPlus,
        }
    );

    Ok(())
}

依赖项

~0.4–0.8MB
~19K SLoC