You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Mapping to primitive types](#mapping-to-primitive-types)
147
+
-[Using literals](#using-literals)
148
+
-[Using patterns](#using-patterns)
149
+
-[Using literals and patterns together](#using-literals-and-patterns-together)
144
150
-[Contributions](#contributions)
145
151
-[License](#license)
146
152
@@ -152,7 +158,7 @@ To let o2o know what traits you want implemented, you have to use type-level `o2
152
158
structEntity { }
153
159
154
160
#[derive(o2o::o2o)]
155
-
#[from_ref(Entity)] // This tells o2o to generate 'From<&Person> for PersonDto' implementation
161
+
#[from_ref(Entity)] // This tells o2o to generate 'From<&Entity> for EntityDto' implementation
156
162
structEntityDto { }
157
163
```
158
164
@@ -247,7 +253,7 @@ o2o has a concept of Inline Expressions, which can be passed as a parameter to s
247
253
structEntity { some_int:i32 }
248
254
249
255
#[derive(o2o::o2o)]
250
-
#[map_owned(Entity)] // tells o2o to implement 'From<Person> for PersonDto' and 'Into<Person> for PersonDto'
256
+
#[map_owned(Entity)] // tells o2o to implement 'From<Entity> for EntityDto' and 'Into<Entity> for EntityDto'
251
257
structEntityDto {
252
258
#[from(~ * 2)] // Let's say for whatever reason we want to multiply 'some_int' by 2 when converting from Entity
253
259
#[into(~ / 2)] // And divide back by 2 when converting into it
@@ -311,7 +317,7 @@ Obviously, you can use `~` for inline expressions that are passed only to member
311
317
312
318
So finally, let's look at some examples.
313
319
314
-
## Examples
320
+
## Struct Examples
315
321
316
322
### Different member name
317
323
@@ -1190,9 +1196,9 @@ pub struct Entity{
1190
1196
```
1191
1197
</details>
1192
1198
1193
-
### Struct kind hints
1199
+
### Type hints
1194
1200
1195
-
By default, **o2o** will suppose that the struct on the other side is the same kind of struct that the original one is. In order to convert between named and tuple structs when you need to place instructions on a tuple side, you`ll need to use Struct Kind Hint:
1201
+
By default, **o2o** will suppose that the struct on the other side is the same kind of type that the original one is. In order to convert between named and tuple structs when you need to place instructions on a tuple side, you`ll need to use Type Hint:
1196
1202
1197
1203
```rust
1198
1204
useo2o::o2o;
@@ -1577,6 +1583,262 @@ struct CarDto {
1577
1583
```
1578
1584
</details>
1579
1585
1586
+
## Enum Examples
1587
+
1588
+
### Different variant name
1589
+
1590
+
```rust
1591
+
pubenumSort {
1592
+
ASC,
1593
+
DESC,
1594
+
None
1595
+
}
1596
+
1597
+
#[derive(o2o::o2o)]
1598
+
#[map_owned(Sort)]
1599
+
pubenumSortDto {
1600
+
#[map(ASC)] Ascending,
1601
+
#[map(DESC)] Descending,
1602
+
None
1603
+
}
1604
+
```
1605
+
1606
+
<details>
1607
+
<summary>View generated code</summary>
1608
+
1609
+
```rust ignore
1610
+
implstd::convert::From<Sort> forSortDto {
1611
+
fnfrom(value:Sort) ->SortDto {
1612
+
matchvalue {
1613
+
Sort::ASC=>SortDto::Ascending,
1614
+
Sort::DESC=>SortDto::Descending,
1615
+
Sort::None=>SortDto::None,
1616
+
}
1617
+
}
1618
+
}
1619
+
implstd::convert::Into<Sort> forSortDto {
1620
+
fninto(self) ->Sort {
1621
+
matchself {
1622
+
SortDto::Ascending=>Sort::ASC,
1623
+
SortDto::Descending=>Sort::DESC,
1624
+
SortDto::None=>Sort::None,
1625
+
}
1626
+
}
1627
+
}
1628
+
```
1629
+
</details>
1630
+
1631
+
### Mapping to primitive types
1632
+
1633
+
#### Using literals
1634
+
1635
+
Literals can be used to produce both `From` and `Into` implementations:
1636
+
1637
+
```rust
1638
+
#[derive(o2o::o2o)]
1639
+
#[map_owned(i32| _ => panic!("Not supported"))]
1640
+
enumHttpStatus {
1641
+
#[literal(200)]Ok,
1642
+
#[literal(201)]Created,
1643
+
#[literal(401)]Unauthorized,
1644
+
#[literal(403)]Forbidden,
1645
+
#[literal(404)]NotFound,
1646
+
#[literal(500)]InternalError
1647
+
}
1648
+
1649
+
typeStaticStr=&'staticstr;
1650
+
1651
+
#[derive(o2o::o2o)]
1652
+
#[map_owned(StaticStr| _ => todo!())]
1653
+
enumAnimal {
1654
+
#[literal("🐶")] Dog,
1655
+
#[literal("🐱")] Cat,
1656
+
#[literal("🐵")] Monkey
1657
+
}
1658
+
```
1659
+
<details>
1660
+
<summary>View generated code</summary>
1661
+
1662
+
```rust ignore
1663
+
implstd::convert::From<i32> forHttpStatus {
1664
+
fnfrom(value:i32) ->HttpStatus {
1665
+
matchvalue {
1666
+
200=>HttpStatus::Ok,
1667
+
201=>HttpStatus::Created,
1668
+
401=>HttpStatus::Unauthorized,
1669
+
403=>HttpStatus::Forbidden,
1670
+
404=>HttpStatus::NotFound,
1671
+
500=>HttpStatus::InternalError,
1672
+
_=>panic!("Not supported"),
1673
+
}
1674
+
}
1675
+
}
1676
+
implstd::convert::Into<i32> forHttpStatus {
1677
+
fninto(self) ->i32 {
1678
+
matchself {
1679
+
HttpStatus::Ok=>200,
1680
+
HttpStatus::Created=>201,
1681
+
HttpStatus::Unauthorized=>401,
1682
+
HttpStatus::Forbidden=>403,
1683
+
HttpStatus::NotFound=>404,
1684
+
HttpStatus::InternalError=>500,
1685
+
}
1686
+
}
1687
+
}
1688
+
1689
+
implstd::convert::From<StaticStr> forAnimal {
1690
+
fnfrom(value:StaticStr) ->Animal {
1691
+
matchvalue {
1692
+
"🐶"=>Animal::Dog,
1693
+
"🐱"=>Animal::Cat,
1694
+
"🐵"=>Animal::Monkey,
1695
+
_=>todo!(),
1696
+
}
1697
+
}
1698
+
}
1699
+
implstd::convert::Into<StaticStr> forAnimal {
1700
+
fninto(self) ->StaticStr {
1701
+
matchself {
1702
+
Animal::Dog=>"🐶",
1703
+
Animal::Cat=>"🐱",
1704
+
Animal::Monkey=>"🐵",
1705
+
}
1706
+
}
1707
+
}
1708
+
```
1709
+
</details>
1710
+
1711
+
#### Using patterns
1712
+
1713
+
Patterns are only used to produce `From` implementations:
0 commit comments