#serde #case #lower-case #case-insensitive

unicase_serde

UniCase crate的serde序列化和反序列化

1 个不稳定版本

使用旧的Rust 2015

0.1.0 2017年7月17日

#1091编码

Download history 3776/week @ 2024-04-25 3626/week @ 2024-05-02 3211/week @ 2024-05-09 3122/week @ 2024-05-16 3031/week @ 2024-05-23 3135/week @ 2024-05-30 3033/week @ 2024-06-06 4484/week @ 2024-06-13 3726/week @ 2024-06-20 3662/week @ 2024-06-27 2824/week @ 2024-07-04 3115/week @ 2024-07-11 3052/week @ 2024-07-18 3286/week @ 2024-07-25 3990/week @ 2024-08-01 3805/week @ 2024-08-08

14,695 每月下载量
rocket_cors 中使用

MIT/Apache

23KB
245

unicase_serde

Build Status Crates.io Repository Documentation

  • 文档:[稳定版本](https://docs.rs/unicase_serde/) | [master分支](https://lawliet89.github.io/unicase_serde)

UniCase crate的serde序列化和反序列化

支持使用serde进行序列化和反序列化。

安装

在Cargo.toml中添加以下内容

serde = "1.0"
serde_derive = "1.0"
unicase="2.0"
unicase_serde = "0.1.0"

用法

您需要使用serde的with 字段属性来注解任何UniCaseAscii字段,并使用适当的模块。请参阅以下示例。

序列化

对任何实现了UniCase<S>Ascii<S>的序列化,其中S: AsRef<str>。例如,S包括&strCow<'a, str>String

所有模块都提供了相同的Serialize实现。

反序列化

您可以将字符串反序列化为UniCase<S>Ascii<S>,其中

  • S:FromStr+ AsRef<str>
  • S: From<&'de str> + AsRef<str> + 'de

S:FromStr+ AsRef<str>

Deserialize实现提供在unicase_serde::unicaseunicase_serde::ascii模块中。转换使用FromStr::from_str函数完成。

通常,您将使用此实现来处理任何Rust内置类型,该类型拥有数据而不进行借用。例如包括String

当您遇到如 "trait bound &'a str: std::convert::From<&str> 不满足"的 trait bound 错误时,您就知道需要使用第二种情况。

S: From<&'de str> + AsRef<str> + 'de

Deserialize 实现中,提供了 unicase_serde::unicase::borrowedunicase_serde::ascii::borrowed 模块。

第二种情况是为了与 Rust 内置的借用数据的类型一起使用。转换是通过使用 Into::into 函数完成的。

如果您将第二种情况与任何拥有数据的类型一起使用,则在运行时将出现错误。

示例序列化和反序列化

extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate unicase;
extern crate unicase_serde;

use std::borrow::Cow;
use unicase::{UniCase, Ascii};

#[derive(Serialize, Deserialize)]
struct TestUniCase<'a> {
    #[serde(with = "unicase_serde::unicase")]
    owned: UniCase<String>,
    #[serde(borrow, with = "unicase_serde::unicase::borrowed")]
    borrowed_str: UniCase<&'a str>,
    #[serde(borrow, with = "unicase_serde::unicase::borrowed")]
    cow_str: UniCase<Cow<'a, str>>,
}

#[derive(Serialize, Deserialize)]
struct TestAscii<'a> {
    #[serde(with = "unicase_serde::ascii")]
    owned: Ascii<String>,
    #[serde(borrow, with = "unicase_serde::ascii::borrowed")]
    borrowed_str: Ascii<&'a str>,
    #[serde(borrow, with = "unicase_serde::ascii::borrowed")]
    cow_str: Ascii<Cow<'a, str>>,
}

# fn main() {
# }

自定义 "字符串" 类型示例

此示例将展示您如何使用 "自定义" 字符串类型,并在其被包装在 UniCaseAscii 中时仍使用序列化和反序列化。这对于像 UniCase<Cow<'a, String>>UniCase<&'a String> 这样的类型特别有用,因为 &String 没有实现 From::<&str>

如您从下面的示例中看到,您可以使用直接的 Deserialize 实现来反序列化借用数据。由于缺少 trait 实现,这通常不适用于 Rust 内置类型。然而,因为转换是通过使用 FromStr::from_str 函数完成的,并且函数签名表明传入的 &str 可能有一个短暂的生存期,实现者必须将 &str 转换为拥有版本。因此,您最好使用借用反序列化器。

extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate unicase;
extern crate unicase_serde;

use std::borrow::Cow;
use std::str::FromStr;
use unicase::UniCase;

#[derive(Eq, PartialEq, Debug)]
struct CustomStr<'a>(Cow<'a, str>);

impl<'a> AsRef<str> for CustomStr<'a> {
    fn as_ref(&self) -> &str {
        self.0.as_ref()
    }
}

impl<'a> FromStr for CustomStr<'a> {
    type Err = ();
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(CustomStr(Cow::from(s.to_string())))
    }
}

impl<'a> From<&'a str> for CustomStr<'a> {
    fn from(s: &'a str) -> Self {
        CustomStr(Cow::from(s))
    }
}

#[derive(Eq, PartialEq, Debug)]
struct CustomString<'a>(Cow<'a, String>);

impl<'a> AsRef<str> for CustomString<'a> {
    fn as_ref(&self) -> &str {
        self.0.as_ref()
    }
}

impl<'a> FromStr for CustomString<'a> {
    type Err = ();
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(CustomString(Cow::Owned(s.to_string())))
    }
}

impl<'a> From<&'a str> for CustomString<'a> {
    fn from(s: &'a str) -> Self {
        CustomString(Cow::Owned(s.to_string()))
    }
}

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
struct TestCustomStruct<'a> {
    #[serde(borrow, with = "unicase_serde::unicase::borrowed")]
    test_str: UniCase<CustomStr<'a>>,
    #[serde(borrow, with = "unicase_serde::unicase::borrowed")]
    test_string: UniCase<CustomString<'a>>,
    #[serde(borrow, with = "unicase_serde::unicase::borrowed")]
    test_str_borrowed: UniCase<CustomStr<'a>>,
    #[serde(borrow, with = "unicase_serde::unicase::borrowed")]
    test_string_borrowed: UniCase<CustomString<'a>>,
}

依赖项

~195–440KB
~10K SLoC