4 releases (breaking)

0.4.0 Feb 23, 2025
0.3.0 Dec 2, 2024
0.2.0 Nov 24, 2024
0.1.0 Oct 21, 2024

#99 in #options

Download history 74/week @ 2025-07-28 39/week @ 2025-08-04 118/week @ 2025-08-11 102/week @ 2025-08-18 137/week @ 2025-08-25 122/week @ 2025-09-01 69/week @ 2025-09-08 110/week @ 2025-09-15 136/week @ 2025-09-22 100/week @ 2025-09-29 80/week @ 2025-10-06 160/week @ 2025-10-13 144/week @ 2025-10-20 148/week @ 2025-10-27 162/week @ 2025-11-03 73/week @ 2025-11-10

539 downloads per month
Used in 4 crates

MIT license

16KB
191 lines

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 Foo {
  id: u32,
  name: String,
}

The macro generates following structs:

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

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

impl From<OptionalFoo> for Foo {
  fn from(optional: OptionalFoo) -> 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
  }
}

Visibility

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

use umbra::optional;

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

The macro generates following structs:

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

pub struct OptionalFoo { // public
  id: Option<u32>,
  name: Option<String>,
}

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

License

MIT

Dependencies

~150–550KB
~13K SLoC