1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! Support for custom column types stored as text — the common case being a
//! Rust enum persisted as a string (`"pending"`, `"verified"`, …).
//!
//! Implement [`StringColumn`] for your type (how it maps to/from a string),
//! then `impl_string_column!(YourType)` to wire it into the ORM: it gets a
//! `Text` [`SqlType`](crate::SqlType), and [`ToColumn`](crate::ToColumn) /
//! [`FromColumn`](crate::FromColumn) impls (for both `T` and `Option<T>`) so it
//! can be a `#[derive(Entity)]` field like any primitive.
//!
//! ```ignore
//! use rainier_orm::{StringColumn, impl_string_column, Result, Error};
//!
//! #[derive(Clone, PartialEq)]
//! enum Status { Pending, Verified, Rejected }
//!
//! impl StringColumn for Status {
//! fn to_column_str(&self) -> String {
//! match self { Status::Pending => "pending", Status::Verified => "verified", Status::Rejected => "rejected" }.into()
//! }
//! fn from_column_str(s: &str) -> Result<Self> {
//! match s {
//! "pending" => Ok(Status::Pending),
//! "verified" => Ok(Status::Verified),
//! "rejected" => Ok(Status::Rejected),
//! other => Err(Error::msg(format!("unknown Status `{other}`"))),
//! }
//! }
//! }
//! impl_string_column!(Status);
//! ```
/// How a custom type maps to and from its stored string form. The string is
/// what lands in a `TEXT`/`VARCHAR` column.
/// Wire a [`StringColumn`] type into the ORM's column traits. See the
/// [module docs](crate::string_column).