fromit
A super powerful macro for generating new structs with getters, setters, and From
or TryFrom
implementation based on the given struct.
Example
```rust
use fromit::FromIt;
fn conv(x: &String) -> Result, std::convert::Infallible> {
Ok(x.asbytes().tovec())
}
[derive(FromIt)]
[fromit(
name = "FooDb",
converter(tryfrom(style = "ref")),
attributes(
derive(Clone, Debug, serde::Serialize, serde::Deserialize),
serde(renameall = "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
where
O: Eq,
{
#[fromit(
parent = "FooDb",
rename = "foo1",
type = "Vec",
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",
)]
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,
}
```
The FromIt
will help you to write the below of code.
```rust
use fromit::FromIt;
fn conv(x: &String) -> Result, std::convert::Infallible> {
Ok(x.asbytes().tovec())
}
[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
where
O: Eq,
{
#[fromit(
parent = "FooDb",
rename = "foo1",
type = "Vec",
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")]
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,
bar: i32,
baz: u64,
}
impl ::core::convert::TryFrom<&Foo> for FooDb
where
O: Eq,
{
type Error = ::std::boxed::Box<
dyn ::std::error::Error + ::core::marker::Send + ::core::marker::Sync + 'static,
;
fn tryfrom(s: &Foo) -> ::core::result::Result {
::core::result::Result::Ok(Self {
foo1: conv(&s.foo)?,
bar: ::core::convert::TryInto::tryinto(s.bar)?,
baz: ::core::convert::TryInto::tryinto(s.baz)?,
})
}
}
impl FooDb {
#[inline]
fn foo(&self) -> &Vec {
&self.foo1
}
#[inline]
fn bar(&self) -> &i32 {
&self.bar
}
#[inline]
fn baz(&self) -> &u64 {
&self.baz
}
}
impl FooDb {
fn setfoo1(&mut self, val: Vec) {
self.foo1 = val;
}
fn setbar(mut self, val: i32) -> Self {
self.bar = val;
self
}
fn setbaz(mut self, val: u64) -> Self {
self.baz = val;
self
}
}
struct FooGraphql
where
O: Eq,
{
panda: T,
tiger: C,
h: H,
baz: u64,
o: O,
bar: i32,
}
impl
FooGraphql
where
O: Eq,
{
#[inline]
fn xh(&self) -> &H {
&self.h
}
#[inline]
fn xbaz(&self) -> &u64 {
&self.baz
}
#[inline]
fn xo(&self) -> &O {
&self.o
}
#[inline]
fn xbar(&self) -> &i32 {
&self.bar
}
#[inline]
fn xpanda(&self) -> &T {
&self.panda
}
#[inline]
fn xtiger(&self) -> &C {
&self.tiger
}
}
impl
FooGraphql
where
O: Eq,
{
fn seth(mut self, val: H) -> Self {
self.h = val;
self
}
fn setbaz(mut self, val: u64) -> Self {
self.baz = val;
self
}
fn seto(mut self, val: O) -> Self {
self.o = val;
self
}
fn setbar(mut self, val: i32) -> Self {
self.bar = val;
self
}
fn setpanda(mut self, val: T) -> Self {
self.panda = val;
self
}
fn settiger(mut self, val: C) -> Self {
self.tiger = val;
self
}
}
```