4 releases

0.2.0 Oct 30, 2022
0.1.2 May 20, 2022
0.1.1 May 20, 2022
0.1.0 May 20, 2022

#4 in #bake

Download history 23/week @ 2023-12-18 10/week @ 2024-01-01 26/week @ 2024-02-19 12/week @ 2024-02-26 4/week @ 2024-03-04 11/week @ 2024-03-11 10/week @ 2024-03-25 33/week @ 2024-04-01

55 downloads per month
Used in rotz

MIT license

17KB
421 lines

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,
}

Dependencies

~1.5MB
~40K SLoC