baker

Crates.iobaker
lib.rsbaker
version0.2.0
sourcesrc
created_at2022-05-20 15:21:57.148071
updated_at2022-10-30 13:32:36.68736
descriptionDerive macro for creating intermediate structs.
homepage
repositoryhttps://github.com/volllly/baker
max_upload_size
id590341
size18,838
Paul Volavsek (volllly)

documentation

https://docs.rs/baker/

README

Baker

crates.io docs.rs

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,
}
Commit count: 18

cargo fmt