Skip to main content

reinhardt_db/orm/query_fields/
traits.rs

1//! Type traits for field lookups
2//!
3//! These traits are used to constrain which methods are available
4//! for different field types.
5
6use super::lookup::LookupValue;
7use serde::{Deserialize, Serialize};
8
9/// Marker trait for types that can be compared (=, !=, <, >, <=, >=)
10pub trait Comparable:
11	Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync + Into<LookupValue>
12{
13}
14
15/// Marker trait for string types (String, &str)
16pub trait StringType: Comparable {}
17
18/// Marker trait for numeric types (i32, i64, f32, f64)
19pub trait NumericType: Comparable {}
20
21/// Marker trait for date/time types
22pub trait DateTimeType: Comparable {}
23
24// Implement Comparable for common types
25impl Comparable for String {}
26impl Comparable for i32 {}
27impl Comparable for i64 {}
28impl Comparable for f32 {}
29impl Comparable for f64 {}
30impl Comparable for bool {}
31
32// Implement StringType
33impl StringType for String {}
34
35// Implement NumericType
36impl NumericType for i32 {}
37impl NumericType for i64 {}
38impl NumericType for f32 {}
39impl NumericType for f64 {}
40
41// DateTime type (legacy - prefer chrono types)
42#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
43/// Represents a date time.
44pub struct DateTime {
45	/// The timestamp.
46	pub timestamp: i64,
47}
48
49impl Comparable for DateTime {}
50impl DateTimeType for DateTime {}
51
52// Date type (legacy - prefer chrono types)
53#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
54/// Represents a date.
55pub struct Date {
56	/// The year.
57	pub year: i32,
58	/// The month.
59	pub month: u8,
60	/// The day.
61	pub day: u8,
62}
63
64impl Comparable for Date {}
65
66// chrono integration
67//
68// These implementations allow chrono types to be used directly in QuerySet
69// filters and lookups. All chrono datetime types are converted to Unix
70// timestamps for comparison operations.
71
72impl Comparable for chrono::NaiveDateTime {}
73impl DateTimeType for chrono::NaiveDateTime {}
74
75impl Comparable for chrono::NaiveDate {}
76impl DateTimeType for chrono::NaiveDate {}
77
78impl Comparable for chrono::NaiveTime {}
79
80impl Comparable for chrono::DateTime<chrono::Utc> {}
81impl DateTimeType for chrono::DateTime<chrono::Utc> {}
82
83impl Comparable for chrono::DateTime<chrono::FixedOffset> {}
84impl DateTimeType for chrono::DateTime<chrono::FixedOffset> {}
85
86impl Comparable for chrono::DateTime<chrono::Local> {}
87impl DateTimeType for chrono::DateTime<chrono::Local> {}