5 releases

0.3.2 Aug 1, 2024
0.3.1 Aug 1, 2024
0.3.0 Jul 30, 2024
0.2.1 Apr 4, 2024
0.2.0 Nov 16, 2023

#84 in #enum


Used in 4 crates (via jkcenum)

Custom license

22KB
517 lines

JkcEnum

Crates.io Crates.io License

Feature

  • from_str
  • from_int
  • to_string
  • to_vec

Example

Cargo.toml

[dependencies]
jkcenum = { version = "0.3.2", features = ["derive"] }

no_std:

[dependencies]
jkcenum = { version = "0.3.2", default-features = false, 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);
}

Dependencies

~265KB