Skip to content

Commit dd42c01

Browse files
committed
docs
1 parent a4ca3ba commit dd42c01

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

rust/functora-tagged/README.md

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ use std::marker::PhantomData;
3131
pub struct Tagged<Rep, Tag>(Rep, PhantomData<Tag>);
3232
```
3333

34+
The `Tagged::new` constructor returns a `Result` that can be `unwrap`ped directly if the `Tag`'s `Refine` implementation returns `std::convert::Infallible`. The `InfallibleInto` trait and its `infallible()` method provide a convenient way to handle this.
35+
36+
## ViaString Struct
37+
38+
The `ViaString<Rep, Tag>` struct is a specialized newtype primarily intended for scenarios where the underlying representation (`Rep`) is closely tied to string manipulation or needs to be serialized/deserialized as a string. It differs from `Tagged` in its serialization and deserialization behavior:
39+
40+
- **Serialization**: `ViaString` serializes to its string representation (via `ToString` on `Rep`), whereas `Tagged` serializes the `Rep` directly.
41+
- **Deserialization**: `ViaString` deserializes from a string, then attempts to parse it into `Rep` using `FromStr`. `Tagged` deserializes `Rep` directly.
42+
43+
It also implements `FromStr` and derives common traits, similar to `Tagged`, respecting the `Refine` trait for validation.
44+
3445
## Refine Trait
3546

3647
To enforce specific refinement rules for your newtypes, you implement the `Refine<Rep>` trait for the `Tag` type. This trait allows you to define custom logic for refining the newtype representation.
@@ -55,6 +66,8 @@ impl Refine<String> for NonEmptyTag {
5566
}
5667
```
5768

69+
Note that the `Refine` trait has a default implementation that simply returns the input `Rep` without modification. This allows you to create simple newtypes for type distinction without needing to implement the `refine` function, as demonstrated in the `NonNegTag` example.
70+
5871
## Derived Traits
5972

6073
`functora-tagged` provides blanket implementations for several important traits. These traits work seamlessly with your newtypes, respecting the underlying representation behavior and customizable refinement rules defined by the `Tag` type's implementation of `Refine<Rep>`.
@@ -73,10 +86,19 @@ impl Refine<String> for NonEmptyTag {
7386

7487
### Refined Derive:
7588

76-
- FromStr
77-
- serde::Deserialize (with `serde` feature)
78-
- diesel::Queryable (with `diesel` feature)
79-
- diesel::deserialize::FromSql (with `diesel` feature)
89+
- **FromStr**: Implemented for `Tagged<Rep, Tag>` and `ViaString<Rep, Tag>`. Returns a `ParseError<Rep, Tag>`, which can be either a `Decode` error (from `Rep::from_str`) or a `Refine` error (from `Tag::refine`). For nested types, these errors can be further nested.
90+
- **serde::Deserialize** (with `serde` feature)
91+
- **diesel::Queryable** (with `diesel` feature)
92+
- **diesel::deserialize::FromSql** (with `diesel` feature)
93+
94+
## Integrations
95+
96+
`functora-tagged` provides optional integrations for common Rust ecosystems:
97+
98+
- **`serde`**: For serialization and deserialization. Enable with the `serde` feature.
99+
- **`diesel`**: For database interactions. Enable with the `diesel` feature.
100+
101+
These integrations respect the `Refine` rules defined for your types.
80102

81103
## Examples
82104

@@ -299,19 +321,11 @@ assert_eq!(
299321
))
300322
);
301323

302-
let err = "-1".parse::<UserId<isize>>().unwrap_err();
324+
let err =
325+
"-1".parse::<UserId<isize>>().unwrap_err();
303326
assert_eq!(err, ParseError::Refine(UserIdError));
304327
```
305328

306-
## Integrations
307-
308-
`functora-tagged` provides optional integrations for common Rust ecosystems:
309-
310-
- **`serde`**: For serialization and deserialization. Enable with the `serde` feature.
311-
- **`diesel`**: For database interactions. Enable with the `diesel` feature.
312-
313-
These integrations respect the `Refine` rules defined for your types.
314-
315329
<hr>
316330

317331
© 2025 [Functora](https://functora.github.io/). All rights reserved.

0 commit comments

Comments
 (0)