Crates.io | baker |
lib.rs | baker |
version | 0.2.0 |
source | src |
created_at | 2022-05-20 15:21:57.148071 |
updated_at | 2022-10-30 13:32:36.68736 |
description | Derive macro for creating intermediate structs. |
homepage | |
repository | https://github.com/volllly/baker |
max_upload_size | |
id | 590341 |
size | 18,838 |
Baker provides a procedural macro for creating a final (baked) struct from an intermediate struct.
Lets say you have a struct that gets parsed from your args or a config file and you need to process this data into similar struct before using it.
struct Cli {
pub urls: Vec<String>,
pub add_slash_to_end: bool
}
struct CliBaked {
pub urls: Vec<String>,
}
impl Cli {
pub fn bake(self) -> CliBaked {
CliBaked {
urls: self.urls.into_iter().map(|u| if self.add_slash_to_end && !u.ends_with('/') { u + "/" } else { u }).collect::<Vec<_>>()
}
}
}
The same thing can be achieved using Baker.
use baker::Bake;
#[derive(Bake)]
#[baked(name = "CliBaked")]
struct Cli {
#[baked(map_fn(bake = "|cli| cli.urls.iter().map(|u| if cli.add_slash_to_end && !u.ends_with('/') { u.to_string() + \"/\" } else { u.to_string() }).collect::<Vec<_>>()"))]
pub urls: Vec<String>,
#[baked(ignore)]
pub add_slash_to_end: bool,
}