umbra

Crates.ioumbra
lib.rsumbra
version
sourcesrc
created_at2024-10-21 00:31:04.860564
updated_at2024-12-02 08:46:19.429326
descriptionA macro to generate optional structs
homepagehttps://github.com/lusingander/umbra
repositoryhttps://github.com/lusingander/umbra
max_upload_size
id1416753
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Kyosuke Fujimoto (lusingander)

documentation

https://docs.rs/umbra/

README

umbra

Crate Status

A macro to generate optional structs

Usage

Basic

Add the #[optional] and #[nested] attributes as follows:

use umbra::optional;

#[optional]
#[derive(Default)]
struct Foo {
  id: u32,
  name: String,
  #[nested]
  bar: Bar,
}

#[optional]
#[derive(Default)]
struct Bar {
  name: String,
  value: Option<i32>,
}

The macro generates following structs:

#[derive(Default)]
struct Foo {
  id: u32,
  name: String,
  bar: Bar,
}

#[derive(Default)]
struct Bar {
  name: String,
  value: Option<i32>,
}

struct OptionalFoo {
  id: Option<u32>,
  name: Option<String>,
  bar: Option<OptionalBar>,
}

impl From<OptionalFoo> for Foo {
  fn from(optional: OptionalFoo) -> Self {
      let mut base = Self::default(); // create base values by default
      if let Some(value) = optional.id {
          base.id = value; // simple field
      }
      if let Some(value) = optional.bar {
          base.bar = value.into(); // nested field
      }
      // ...
      base
  }
}

struct OptionalBar {
  name: Option<String>,
  value: Option<i32>,
}

impl From<OptionalBar> for Bar {
  fn from(optional: OptionalBar) -> Self {
      let mut base = Self::default();
      // ...
      base
  }
}

Derives

By using the derives attribute, the derive attribute can be added to the generated struct:

use umbra::optional;

#[optional(derives = [Debug, std::clone::Clone])]
#[derive(Default)]
struct Bar {
  name: String,
  value: Option<i32>,
}

The macro generates following structs:

#[derive(Default)]
struct Bar {
  name: String,
  value: Option<i32>,
}

#[derive(Debug, std::clone::Clone)] // The derive attribute is added
struct OptionalBar {
  name: Option<String>,
  value: Option<i32>,
}

impl From<OptionalBar> for Bar {
  fn from(optional: OptionalBar) -> Self {
      let mut base = Self::default();
      // ...
      base
  }
}

Prefix / Suffix

By using the prefix and suffix attributes, the prefix and suffix of the generated struct are changed:

use umbra::optional;

#[optional(prefix = "Pre", suffix = "Suf")]
#[derive(Default)]
struct Foo {
  id: u32,
  name: String,
}

The macro generates following structs:

#[derive(Default)]
struct Foo {
  id: u32,
  name: String,
}

struct PreFooSuf { // <Prefix><Base name><Suffix> format
  id: Option<u32>,
  name: Option<String>,
}

impl From<PreFooSuf> for Foo {
  fn from(optional: PreFooSuf) -> Self {
      let mut base = Self::default();
      // ...
      base
  }
}

License

MIT

Commit count: 21

cargo fmt