#setter #macro-derive #derive #from #builder #macro #getter-setter

fromit

一个功能强大的宏,用于根据给定的结构体生成新的结构体,其中包含获取器、设置器和基于该结构体的 FromTryFrom 实现

6 个版本

0.1.2 2023 年 3 月 5 日
0.1.1 2023 年 3 月 5 日
0.0.3 2023 年 3 月 5 日
0.0.0 2023 年 2 月 23 日

#setter 中排名 #26

每月下载量 25
用于 derivit

Apache-2.0/MIT

76KB
2K SLoC

fromit

一个功能强大的宏,用于根据给定的结构体生成新的结构体,其中包含获取器、设置器和基于该结构体的 FromTryFrom 实现。

示例

use fromit::FromIt;

fn conv(x: &String) -> Result<Vec<u8>, std::convert::Infallible> {
  Ok(x.as_bytes().to_vec())
}

#[derive(FromIt)]
#[fromit(
  name = "FooDb",
  converter(try_from(style = "ref")),
  attributes(
    derive(Clone, Debug, serde::Serialize, serde::Deserialize),
    serde(rename_all = "camelCase")
  )
)]
#[fromit(
  name = "FooGraphql",
  getters(prefix = "x", style = "ref"),
  bound(
    inherit,
    extra = "T: Clone + core::fmt::Debug + Default, C: Copy + Default"
  ),
  extra(fields(
    r#"{
      panda: T,
      tiger: C,
    }"#
  ))
)]
struct Foo<H: core::hash::Hash, O>
where
  O: Eq,
{
  #[fromit(
    parent = "FooDb",
    rename = "foo1",
    type = "Vec<u8>",
    from(fn = "conv"),
    getter(style = "ref", rename = "foo"),
    setter(style = "ref", bound = "C"),
    attributes(serde(rename = "foo2"))
  )]
  #[fromit(
    parent = "FooGraphql",
    rename = "foo1",
    skip,
    type = "Vec<u8>",
  )]
  foo: String,
  #[fromit(parent = "FooDb", from(style = "move"))]
  bar: i32,
  #[fromit(parent = "FooDb", from(style = "move"))]
  baz: u64,
  #[fromit(parent = "FooDb", skip)]
  h: H,
  #[fromit(parent = "FooDb", skip)]
  o: O,
}

FromIt 将帮助您编写以下代码。


use fromit::FromIt;

fn conv(x: &String) -> Result<Vec<u8>, std::convert::Infallible> {
    Ok(x.as_bytes().to_vec())
}

#[fromit(
    name = "FooDb",
    converter(try_from(style = "ref")),
    attributes(
        derive(Clone, Debug, serde::Serialize, serde::Deserialize),
        serde(rename_all = "camelCase")
    )
)]
#[fromit(
    name = "FooGraphql",
    getters(prefix = "x", style = "ref"),
    bound(
        inherit,
        extra = "T: Clone + core::fmt::Debug + Default, C: Copy + Default"
    ),
    extra(fields(
        r#"{
      panda: T,
      tiger: C,
    }"#
    ))
)]
struct Foo<H: core::hash::Hash, O>
where
    O: Eq,
{
    #[fromit(
        parent = "FooDb",
        rename = "foo1",
        type = "Vec<u8>",
        from(fn = "conv"),
        getter(style = "ref", rename = "foo"),
        setter(style = "ref", bound = "C"),
        attributes(serde(rename = "foo2"))
    )]
    #[fromit(parent = "FooGraphql", rename = "foo1", skip, type = "Vec<u8>")]
    foo: String,
    #[fromit(parent = "FooDb", from(style = "move"))]
    bar: i32,
    #[fromit(parent = "FooDb", from(style = "move"))]
    baz: u64,
    #[fromit(parent = "FooDb", skip)]
    h: H,
    #[fromit(parent = "FooDb", skip)]
    o: O,
}

#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct FooDb {
    #[serde(rename = "foo2")]
    foo1: Vec<u8>,
    bar: i32,
    baz: u64,
}

impl<H: core::hash::Hash, O> ::core::convert::TryFrom<&Foo<H, O>> for FooDb
where
    O: Eq,
{
    type Error = ::std::boxed::Box<
        dyn ::std::error::Error + ::core::marker::Send + ::core::marker::Sync + 'static,
    >;
    fn try_from(s: &Foo<H, O>) -> ::core::result::Result<Self, Self::Error> {
        ::core::result::Result::Ok(Self {
            foo1: conv(&s.foo)?,
            bar: ::core::convert::TryInto::try_into(s.bar)?,
            baz: ::core::convert::TryInto::try_into(s.baz)?,
        })
    }
}
impl FooDb {
    #[inline]
    fn foo(&self) -> &Vec<u8> {
        &self.foo1
    }
    #[inline]
    fn bar(&self) -> &i32 {
        &self.bar
    }
    #[inline]
    fn baz(&self) -> &u64 {
        &self.baz
    }
}
impl FooDb {
    fn set_foo1<C>(&mut self, val: Vec<u8>) {
        self.foo1 = val;
    }
    fn set_bar(mut self, val: i32) -> Self {
        self.bar = val;
        self
    }
    fn set_baz(mut self, val: u64) -> Self {
        self.baz = val;
        self
    }
}
struct FooGraphql<H: core::hash::Hash, O, T: Clone + core::fmt::Debug + Default, C: Copy + Default>
where
    O: Eq,
{
    panda: T,
    tiger: C,
    h: H,
    o: O,
    bar: i32,
    baz: u64,
}

impl<H: core::hash::Hash, O, T: Clone + core::fmt::Debug + Default, C: Copy + Default>
    FooGraphql<H, O, T, C>
where
    O: Eq,
{
    #[inline]
    fn x_h(&self) -> &H {
        &self.h
    }
    #[inline]
    fn x_baz(&self) -> &u64 {
        &self.baz
    }
    #[inline]
    fn x_o(&self) -> &O {
        &self.o
    }
    #[inline]
    fn x_bar(&self) -> &i32 {
        &self.bar
    }
    #[inline]
    fn x_panda(&self) -> &T {
        &self.panda
    }
    #[inline]
    fn x_tiger(&self) -> &C {
        &self.tiger
    }
}
impl<H: core::hash::Hash, O, T: Clone + core::fmt::Debug + Default, C: Copy + Default>
    FooGraphql<H, O, T, C>
where
    O: Eq,
{
    fn set_h(mut self, val: H) -> Self {
        self.h = val;
        self
    }
    fn set_baz(mut self, val: u64) -> Self {
        self.baz = val;
        self
    }
    fn set_o(mut self, val: O) -> Self {
        self.o = val;
        self
    }
    fn set_bar(mut self, val: i32) -> Self {
        self.bar = val;
        self
    }
    fn set_panda(mut self, val: T) -> Self {
        self.panda = val;
        self
    }
    fn set_tiger(mut self, val: C) -> Self {
        self.tiger = val;
        self
    }
}

依赖关系

~2MB
~49K SLoC