majordome_derive/
lib.rs

1mod majordome_errors;
2mod majordome_scylla;
3
4/// Derive macro for the `IntoMajordomeError` trait.
5/// Convert an enum to a MajordomeError.
6/// Enum Attributes:
7/// - `prefix`: Prefix for the error code. Required.
8/// Enum Variants Attributes:
9/// - `code`: Error code. Required.
10/// - `msg`: Error message. The string is formatted using enum variant fields. Required.
11/// - `status`: HTTP status code. Required.
12///
13/// # Example
14/// ```rs
15/// #[derive(MajordomeError)]
16/// #[err(prefix = "errors.gg.wls.")]
17/// pub enum AuthError {
18///     #[err(code="invalid_token", msg="Invalid token", status=401)]
19///     InvalidToken,
20///
21///     #[err(code="unknown_event", msg="Unknown event {id}", status=404)]
22///     UnknownEvent {id: String},
23///
24///     #[err(code="not_enough_players", msg="Not enough players (required: {required}, actual: {actual})", status=400)]
25///     NotEnoughPlayers{required: u32, actual: u32},
26/// }
27/// ```
28///
29/// Into/From are implemented for MajordomeError.
30/// ```rs
31/// AuthError::UnknownEvent{id: "123".to_string()}.into()
32/// ```
33/// equals to
34/// ```rs
35/// MajordomeError::new(
36///     "errors.gg.wls.unknown_event".to_string(),
37///     "Unknown event 123".to_string(),
38///     vec!["123".to_string()],
39///     404
40/// )
41/// ```
42#[proc_macro_derive(IntoMajordomeError, attributes(err))]
43pub fn into_majordome_error_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
44    majordome_errors::parse_enum_error(input)
45}
46
47/// Derive macro for the `ScyllaRow` trait.
48/// ORM for ScyllaDB.
49/// Struct Attributes:
50/// - `table`: Table name. Required.
51/// - `primary_key`: Primary key field names. Required.
52/// - `clustering_key`: Clustering key field names. Optional.
53/// - `indexes`: Index field names. Optional.
54/// 
55/// Struct Fields Attributes:
56/// - `map`: Map field. Optional.
57/// - `set`: Set field. Optional.
58/// - `counter`: Counter field. Optional.
59/// 
60/// # Example
61/// ```rs
62/// #[derive(ScyllaRow)]
63/// #[scylla(table = "users", primary_key = "id")]
64/// pub struct UserDBRepr {
65///    pub id: i64,
66///    pub email: Option<String>,
67///    pub sponsor_id: Option<i64>,
68///    pub p_desc: Option<String>,
69///    #[scylla(map = 1)]
70///    pub assets: std::collections::BTreeMap<String, String>,
71///    pub flags: i64,
72/// }
73/// ```
74/// 
75/// This can then be used to update the struct.
76/// ```rs
77/// let u = UserDBRepr::new(1);
78/// let u = u.update().email_set(Some("test".to_string())).assets_add({
79///    let mut m = BTreeMap::new();
80///    m.insert("test".to_string(), "test".to_string());
81///    m
82/// }).save().await?;
83/// ```
84#[proc_macro_derive(ScyllaRow, attributes(majordome_scylla))]
85pub fn scylla_row_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
86    majordome_scylla::parse_struct_orm(input)
87}