Crates.io | substruct |
lib.rs | substruct |
version | 0.1.1 |
source | src |
created_at | 2024-05-17 19:07:41.25887 |
updated_at | 2024-05-20 21:24:02.523746 |
description | A proc-macro to create subsets of structs |
homepage | |
repository | https://github.com/swlynch99/substruct |
max_upload_size | |
id | 1243580 |
size | 46,551 |
Substruct is a proc-macro wich allows you to easily declare strucs which are subsets of another struct.
A basic use of substruct looks like this
use substruct::substruct;
#[substruct(LimitedQueryParams)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct QueryParams {
#[substruct(LimitedQueryParams)]
pub name: Option<String>,
#[substruct(LimitedQueryParams)]
pub parent: Option<String>,
pub limit: usize
}
which expands out to produce
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct QueryParams {
pub name: Option<String>,
pub parent: Option<String>,
pub limit: usize
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LimitedQueryParams {
pub name: Option<String>,
pub parent: Option<String>,
}
Substruct also supports copying attributes or adding attributes specific to a subset of the child structs.
use std::time::SystemTime;
use substruct::substruct;
#[substruct(PostQueryParams, ThreadQueryParams)]
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct QueryParams {
/// Query only within forums with this id
pub forum: Option<u64>,
/// Query only with threads with this id.
#[substruct(PostQueryParams)]
pub thread: Option<u64>,
/// The username to search.
#[substruct(PostQueryParams, ThreadQueryParams)]
// Alias only applied for ThreadQueryParams
#[substruct_attr(ThreadQueryParams, serde(alias = "username"))]
pub user: Option<String>,
#[substruct(PostQueryParams, ThreadQueryParams)]
// Field is renamed (in serde) for PostQueryParams and ThreadQueryParams
// but not for QueryParams.
#[substruct_attr(not(QueryParams), serde(rename = "before_ts"))]
pub before: Option<SystemTime>,
#[substruct(PostQueryParams, ThreadQueryParams)]
#[substruct_attr(not(QueryParams), serde(rename = "before_ts"))]
pub after: Option<SystemTime>,
// Limit is only present on QueryParams.
pub limit: Option<usize>,
}
Substruct supports generics but will fail if the generic parameters are not used by all of the child structs.