#signals #mutable #futures-signals #signal #proc-macro

macro designal

Procedural macro for use with futures-signals

9 releases

0.3.2 Jan 16, 2021
0.3.1 Dec 4, 2020
0.3.0 Nov 4, 2020
0.2.4 Sep 11, 2020
0.1.0 Sep 6, 2020

#1160 in Data structures

MIT license

46KB
792 lines

Designal

This is a Derive macro that trys to help reduce code duplication between the front and backend when using futures-signals and dominator. When using signals you have to wrap a lot of types in a Mutable, MutableVec, and MutableBTreeMap which you likely don't want to have on your backend code. It will recursively trim away the following types from struct fields:

  • Mutable<T> -> T
  • MutableVec<T> -> Vec<T>
  • MutableBTreeMap<K, V> -> BTreeMap<K, V>
  • MutableBTreeMap<K, ()> -> BTreeSet<K>
  • Rc<T> -> T
  • Arc<T> -> T

See the Container Attributes and Field Attributes section for some configuration options.

use designal::Designal;
use futures_signals::signal::Mutable;
use futures_signals::signal_vec::MutableVec;
use std::rc::Rc;

#[derive(Designal)]
#[designal(trim_end = "Signal", derive = "Debug")]
struct FlavoursSignal(MutableVec<String>);

#[derive(Designal)]
#[designal(trim_end = "Signal", derive = "Debug")]
struct TasteSignal {
    salt: Mutable<u32>,
    sweet: Mutable<bool>,
    sour: Mutable<Rc<i8>>,
    #[designal(trim_end = "Signal")]
    flavours: FlavoursSignal,
}

#[derive(Designal)]
#[designal(trim_end = "Signal", derive = "Debug")]
struct HumanSignal {
    #[designal(trim_end = "Signal")]
    taste: Rc<TasteSignal>,
    name: Mutable<(String, String)>,
    #[designal(remove)]
    editing: Mutable<bool>,
}

Generates this code:

#[derive(Debug)]
struct Flavours(Vec<String>);

#[derive(Debug)]
struct Taste {
    salt: u32,
    sweet: bool,
    sour: i8,
    flavours: Flavours,
}

#[derive(Debug)]
struct Human {
    taste: Taste,
    name: (String, String),
}

Container Attributes

Every struct will need to have one of the renaming attributes rename, add_start, add_end, trim_start, trim_start_all, trim_end, or trim_end_all.

#[designal(rename = "NewName")]

Renames the struct completely.

#[designal(add_start = "Prefix")]

Renames the struct by adding the string to the start of the struct identifier.

#[designal(add_end = "Postfix")]

Renames the struct by adding the string to the end of the struct identifier.

#[designal(trim_start = "Prefix")]

Renames the struct by removing the string from the start of the struct identifier.

#[designal(trim_start_all = "Prefix")]

Renames the struct by removing the string from the start of the struct identifier and also renames any field types that start with the same prefix. If a field doesn't start with the prefix it is left as is; if the field has its own renamer that will take precedence.

#[designal(trim_end = "Postfix")]

Renames the struct by removing the string from the end of the struct identifier.

#[designal(trim_end_all = "Postfix")]

Renames the struct by removing the string from the end of the struct identifier and also renames any field types that end with the same postfix. If a field doesn't end with the postfix it is left as is; if the field has its own renamer that will take precedence.

#[designal(derive = "Debug")]

Adds a derive attribute to the generated struct. Can accept a list of csv values #[designal(derive = "Serialize, Deserialize, Debug, Default")]; be used multiple times; or like#[designal(derive = "Debug", derive = "PartialEq")].

#[designal(cfg_feature = "your_feature")]

Adds a #[cfg(feature = "your_feature")] attribute to the generated struct.

#[designal(keep_rc)]

Keeps any Rc's used on any fields.

#[designal(keep_arc)]

Keeps any Arc's used on any fields.

#[designal(hashmap)]

If any field is a MutableBTreeMap<K, V> returns it as a HashMap<K, V> rather than the default of BTreeMap<K, V>. If any field is MutableBTreeMap<K, ()> returns it as a HashSet<K>.

Field Attributes

#[designal(rename = "NewName")]

Renames the field's declared type completely.

#[designal(add_start = "Prefix")]

Renames the field's declared type by adding the string to the start of the field's declared type identifier.

#[designal(add_end = "Postfix")]

Renames the field's declared type by adding the string to the end of the field's declared type identifier.

#[designal(trim_start = "Prefix")]

Renames the field's declared type by removing the string from the start of the field's declared type identifier.

#[designal(trim_end = "Postfix")]

Renames the field's declared type by removing the string from the end of the field's declared type identifier.

#[designal(remove)]

Removes the field from the generated struct.

#[designal(ignore)]

Tells designal to leave the field alone and return it as is.

#[designal(keep_rc)]

Keeps any Rc's used in the field.

#[designal(keep_arc)]

Keeps any Arc's used in the field.

#[designal(hashmap)]

If the field is a MutableBTreeMap<K, V> returns it as a HashMap<K, V> rather than the default of BTreeMap<K, V>. If it is MutableBTreeMap<K, ()> returns it as a HashSet<K>.

Dependencies

~1.5MB
~34K SLoC