#枚举 #jkcenum #jenum

无std jkcenum_derive

Rust枚举库

5个版本

新版本 0.3.2 2024年8月1日
0.3.1 2024年8月1日
0.3.0 2024年7月30日
0.2.1 2024年4月4日
0.2.0 2023年11月16日

#712#枚举

Download history 14/week @ 2024-04-29 14/week @ 2024-05-20 3/week @ 2024-05-27 11/week @ 2024-06-03 6/week @ 2024-06-10 5/week @ 2024-06-17 8/week @ 2024-06-24 9/week @ 2024-07-15 382/week @ 2024-07-29

每月下载量 391次
4 个crate中使用 (通过 jkcenum)

自定义许可证

22KB
517

JkcEnum

Crates.io Crates.io License

功能

  • from_str
  • from_int
  • to_string
  • to_vec

示例

Cargo.toml

[dependencies]
jkcenum = { git = "https://github.com/caizhengxin/jkcenum.git", features = ["derive"] }

from_str

use std::str::FromStr;
use jkcenum::JkcEnum;



#[derive(Debug, PartialEq, Eq, JkcEnum)]
enum JkcExample {
    #[jenum(alias = "r", alias = "read")]
    Read,
    #[jenum(rename = "WRITE", alias = "w", alias = "write")]
    Write,
}


#[test]
fn test_fromstr() {
    assert_eq!(JkcExample::from_str("Read").unwrap(), JkcExample::Read);
    assert_eq!(JkcExample::from_str("r").unwrap(), JkcExample::Read);
    assert_eq!(JkcExample::from_str("read").unwrap(), JkcExample::Read);
    assert_eq!(JkcExample::from_str("WRITE").unwrap(), JkcExample::Write);
    assert_eq!(JkcExample::from_str("w").unwrap(), JkcExample::Write);
    assert_eq!(JkcExample::from_str("write").unwrap(), JkcExample::Write);
    assert_eq!(JkcExample::from_str("Write").is_err(), true);
}

from_int

use jkcenum::JkcEnum;
use jkcenum::FromInt;


#[derive(Debug, PartialEq, Eq, JkcEnum)]
pub enum JkcExample {
    Read = 0x01,
    ReadWrite,
    Write = 0x03,
    #[jenum(range="4..=6")]
    Test,
    Test2 = 7,
    #[jenum(default)]
    Unknown,
}

#[test]
fn test_fromint() {
    assert_eq!(JkcExample::from_int(1).unwrap(), JkcExample::Read);
    assert_eq!(JkcExample::from_int(2).unwrap(), JkcExample::ReadWrite);
    assert_eq!(JkcExample::from_int(3).unwrap(), JkcExample::Write);
    assert_eq!(JkcExample::from_int(4).unwrap(), JkcExample::Test);
    assert_eq!(JkcExample::from_int(5).unwrap(), JkcExample::Test);
    assert_eq!(JkcExample::from_int(6).unwrap(), JkcExample::Test);
    assert_eq!(JkcExample::from_int(7).unwrap(), JkcExample::Test2);
    assert_eq!(JkcExample::from_int(8).unwrap(), JkcExample::Unknown);
    assert_eq!(JkcExample::from_int(9).unwrap(), JkcExample::Unknown);
}

to_string

use jkcenum::JkcEnum;


#[derive(Debug, PartialEq, Eq, JkcEnum)]
enum JkcExample {
    Read,
    #[jenum(rename = "WRITE")]
    Write,
}


#[test]
fn test_tostring() {
    assert_eq!(JkcExample::Read.to_string(), "Read");
    assert_eq!(JkcExample::Write.to_string(), "WRITE");
}
use jkcenum::JkcEnum;


#[derive(Debug, PartialEq, Eq, JkcEnum)]
#[jenum(rename_all="lowercase")]
enum JkcExample {
    JanKinCai,
}


#[test]
fn test_tostring() {
    assert_eq!(JkcExample::JanKinCai.to_string(), "jankincai");
}
use jkcenum::JkcEnum;


#[derive(Debug, PartialEq, Eq, JkcEnum)]
#[jenum(rename_all="desc")]
enum JkcExample {
    JanKinCai,
}


#[test]
fn test_tostring() {
    assert_eq!(JkcExample::JanKinCai.to_string(), "Jan Kin Cai");
}

to_vec

use jkcenum::JkcEnum;


#[derive(Debug, Clone, Copy, PartialEq, Eq, JkcEnum)]
pub enum JkcExample {
    Read,
    #[jenum(rename = "WRITE")]
    Write,
}


#[test]
fn test_to_vec() {
    assert_eq!(JkcExample::to_vec(), vec![
        JkcExample::Read,
        JkcExample::Write,
    ]);
}

serde

  • rename
  • alias
  • lowercase
  • uppercase
use std::str::FromStr;

use jkcenum::JkcEnum;
use serde::{Serialize, Deserialize};


#[derive(Debug, PartialEq, Eq, JkcEnum, Serialize, Deserialize)]
enum JkcExample {
    Read,
    #[serde(rename="WRITE")]
    Write,
}


#[derive(Debug, PartialEq, Eq, JkcEnum, Serialize, Deserialize)]
#[serde(rename_all="lowercase")]
enum JkcExample2 {
    Read,
    Write,
}


#[test]
fn test_serde_tostring() {
    assert_eq!(JkcExample::Read.to_string(), "Read");
    assert_eq!(JkcExample::Write.to_string(), "WRITE");
    assert_eq!(JkcExample::from_str("Read").unwrap(), JkcExample::Read);
    assert_eq!(JkcExample::from_str("WRITE").unwrap(), JkcExample::Write);

    assert_eq!(JkcExample2::Read.to_string(), "read");
    assert_eq!(JkcExample2::Write.to_string(), "write");
    assert_eq!(JkcExample2::from_str("read").unwrap(), JkcExample2::Read);
    assert_eq!(JkcExample2::from_str("write").unwrap(), JkcExample2::Write);
}

依赖

~265KB