field_kinds/field_meta/
categories.rs

1use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
2
3/// Trait for type category markers.
4///
5/// Implemented by category marker types ([`Numeric`], [`Text`], etc.).
6pub trait TypeCategory: 'static + Copy {
7    /// String name of the category (e.g., "numeric", "text").
8    const NAME: &'static str;
9}
10
11/// Marker type for numeric types (`i8`-`i128`, `u8`-`u128`, `f32`, `f64`, `isize`, `usize`).
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct Numeric;
14
15/// Marker type for text types (`String`, `&str`, `Box<str>`, `char`).
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub struct Text;
18
19/// Marker type for boolean type.
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub struct Bool;
22
23/// Marker type for optional types (`Option<T>`).
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub struct Optional;
26
27/// Marker type for collection types (`Vec`, `HashSet`, `HashMap`, arrays, slices).
28#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29pub struct Collection;
30
31/// Marker type for types that don't match any known category.
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub struct Unknown;
34
35impl TypeCategory for Numeric {
36    const NAME: &'static str = "numeric";
37}
38impl TypeCategory for Text {
39    const NAME: &'static str = "text";
40}
41impl TypeCategory for Bool {
42    const NAME: &'static str = "bool";
43}
44impl TypeCategory for Optional {
45    const NAME: &'static str = "optional";
46}
47impl TypeCategory for Collection {
48    const NAME: &'static str = "collection";
49}
50impl TypeCategory for Unknown {
51    const NAME: &'static str = "unknown";
52}
53
54/// Trait for mapping Rust types to their categories.
55///
56/// Implement this trait for custom types to enable automatic categorization.
57///
58/// # Example
59///
60/// ```rust
61/// use field_kinds::{Categorized, Numeric};
62///
63/// struct MyNumber(i32);
64///
65/// impl Categorized for MyNumber {
66///     type Category = Numeric;
67/// }
68/// ```
69pub trait Categorized {
70    /// The category marker type for this type.
71    type Category: TypeCategory;
72}
73
74impl Categorized for u8 {
75    type Category = Numeric;
76}
77impl Categorized for u16 {
78    type Category = Numeric;
79}
80impl Categorized for u32 {
81    type Category = Numeric;
82}
83impl Categorized for u64 {
84    type Category = Numeric;
85}
86impl Categorized for u128 {
87    type Category = Numeric;
88}
89impl Categorized for usize {
90    type Category = Numeric;
91}
92
93impl Categorized for i8 {
94    type Category = Numeric;
95}
96impl Categorized for i16 {
97    type Category = Numeric;
98}
99impl Categorized for i32 {
100    type Category = Numeric;
101}
102impl Categorized for i64 {
103    type Category = Numeric;
104}
105impl Categorized for i128 {
106    type Category = Numeric;
107}
108impl Categorized for isize {
109    type Category = Numeric;
110}
111
112impl Categorized for f32 {
113    type Category = Numeric;
114}
115impl Categorized for f64 {
116    type Category = Numeric;
117}
118
119impl Categorized for String {
120    type Category = Text;
121}
122impl Categorized for &str {
123    type Category = Text;
124}
125impl Categorized for Box<str> {
126    type Category = Text;
127}
128impl Categorized for char {
129    type Category = Text;
130}
131
132impl Categorized for bool {
133    type Category = Bool;
134}
135
136impl<T> Categorized for Option<T> {
137    type Category = Optional;
138}
139
140impl<T> Categorized for Vec<T> {
141    type Category = Collection;
142}
143impl<T, const N: usize> Categorized for [T; N] {
144    type Category = Collection;
145}
146impl<T> Categorized for &[T] {
147    type Category = Collection;
148}
149impl<T, S> Categorized for HashSet<T, S> {
150    type Category = Collection;
151}
152impl<T> Categorized for BTreeSet<T> {
153    type Category = Collection;
154}
155impl<K, V, S> Categorized for HashMap<K, V, S> {
156    type Category = Collection;
157}
158impl<K, V> Categorized for BTreeMap<K, V> {
159    type Category = Collection;
160}