#traits #partial #type #derive #relaxed #“relaxed” #typescript-like

relax

针对TypeScript-like部分(relaxed)类型的特性

11个版本

0.1.0-alpha.11 2024年8月20日
0.1.0-alpha.102024年8月16日
0.1.0-alpha.82024年7月20日

479Rust模式 中排名 479

Download history 537/week @ 2024-07-15 58/week @ 2024-07-22 123/week @ 2024-08-05 116/week @ 2024-08-12

每月下载量 302

MIT/Apache

6KB

relax

针对TypeScript-like部分(relaxed)类型的特性

用法

生成“relaxed”类型

use relax::Relax;

#[derive(Relax)]
#[relax(PartialData)]
pub(crate) struct Data {
    id: u16,
    pub name: String,
    pub favorite: Option<String>,
}

产生以下部分(relaxed)结构体

pub(crate) struct PartialData {
    id: Option<u16>,
    pub name: Option<String>,
    pub favorite: Option<String>,
}

支持嵌套

use relax::Relax;

#[derive(Relax)]
#[relax(PartialFoo)]
struct Foo {
    id: u16,
    #[relax]
    bar: Bar,
    #[relax]
    bar2: Option<Bar>,
}

#[derive(Relax)]
#[relax(PartialBar)]
struct Bar {
    name: String,
    favorite: Option<String>,
}

产生

struct PatrialFoo {
    id: Option<u16>,
    bar: Option<PartialBar>,
    bar2: Option<PartialBar>,
}

struct PartialBar {
    name: Option<String>,
    favorite: Option<String>,
}

您还可以向生成的结构体添加属性

use relax::Relax;
use serde::{Serialize, Deserialize};

#[derive(Relax)]
#[relax(PartialData, derive(Serialize, Deserialize), serde(rename_all = "UPPERCASE"))]
pub(crate) struct Data {
    id: u16,
    pub name: String,
    pub favorite: Option<String>,
}

产生

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub(crate) struct PartialData {
    id: Option<u16>,
    pub name: Option<String>,
    pub favorite: Option<String>,
}

创建空的“relaxed”实例

relaxed结构体继承 Relaxed 特性,它有一个 new 关联函数。

use relax::{Relax, Relaxed};

#[derive(Relax)]
#[relax(PartialData, derive(Debug, PartialEq))]
pub(crate) struct Data {
    id: u16,
    pub name: String,
    pub favorite: Option<String>,
}

# fn main() {
let empty = PartialData {
    id: None,
    name: None,
    favorite: None,
};

assert_eq!(PartialData::new(), empty);
# }

合并多个“relaxed”实例

Relaxed 特性还有一个 merge 函数。其行为类似于 Option::or,但这个函数应用于每个结构体字段。

use relax::{Relax, Relaxed};

#[derive(Relax)]
#[relax(PartialData, derive(Debug, PartialEq))]
pub(crate) struct Data {
    id: u16,
    pub name: String,
    pub favorite: Option<String>,
}

# fn main() {

let partial_data_1 = PartialData {
    id: None,
    name: Some("John".to_owned()),
    favorite: None,
};

let partial_data_2 = PartialData {
    id: None,
    name: None,
    favorite: Some("Rust".to_owned()),
};

let merged = partial_data_1.merge(partial_data_2);

let expected = PartialData {
    id: None,
    name: Some("John".to_owned()),
    favorite: Some("Rust".to_owned()),
};

assert_eq!(merged, expected);

# }

从relaxed实例创建原始结构体实例

如果原始结构体的所有必需字段都已设置在relaxed结构体实例中,可以使用 try_into() 将其转换为原始结构体。

use relax::{Relax, Relaxed};

let partial_data_1: PartialData = read_data(file_1);
let partial_data_2: PartialData = read_data(file_2);
let partial_data_3: PartialData = read_data(file_3);

let data: Data = partial_data_1
                    .merge(partial_data_2)
                    .merge(partial_data_3)
                    .try_into()?;

依赖关系

~110KB