Skip to main content

pharos_native/runtime/
json_ops.rs

1use std::cmp::Ordering;
2
3use serde_json::Value;
4
5/// Returns the JSON long.
6fn json_long(value: &Value, key: &str, default_value: i64) -> i64 {
7    value
8        .get(key)
9        .and_then(Value::as_i64)
10        .unwrap_or(default_value)
11}
12
13/// Compares item ID.
14///
15/// # Parameters
16///
17/// - `lhs`: Input consumed by [`crate::runtime::json_ops::compare_item_id`].
18/// - `rhs`: Input consumed by [`crate::runtime::json_ops::compare_item_id`].
19///
20/// # Returns
21///
22/// Returns the computed value produced by [`crate::runtime::json_ops::compare_item_id`].
23///
24/// # Examples
25///
26/// ```rust,ignore
27/// let _result = pharos_native::runtime::json_ops::compare_item_id(todo!(), todo!());
28/// ```
29///
30/// # Related
31///
32/// - [`crate::runtime::json_ops`] for adjacent helpers that share the same runtime surface.
33pub fn compare_item_id(lhs: &Value, rhs: &Value) -> Ordering {
34    json_long(lhs, "id", 0).cmp(&json_long(rhs, "id", 0))
35}
36
37#[derive(Debug, Clone, Eq, PartialEq)]
38/// Public struct `EventStreamRowRuntime` used by this module.
39///
40/// # Fields
41///
42/// - `created_at`: Stored field on `EventStreamRowRuntime`.
43/// - `source`: Stored field on `EventStreamRowRuntime`.
44/// - `native_order`: Stored field on `EventStreamRowRuntime`.
45///
46/// # Examples
47///
48/// ```rust,ignore
49/// let _ = std::mem::size_of::<pharos_native::runtime::json_ops::EventStreamRowRuntime>();
50/// ```
51///
52/// # Related
53///
54/// - [`crate::runtime::json_ops`] for constructors, helpers, and neighboring runtime types.
55pub struct EventStreamRowRuntime {
56    pub created_at: Option<String>,
57    pub source: Option<String>,
58    pub native_order: i64,
59}
60
61/// Compares event stream rows.
62///
63/// # Parameters
64///
65/// - `lhs`: Input consumed by [`crate::runtime::json_ops::compare_event_stream_rows`].
66/// - `rhs`: Input consumed by [`crate::runtime::json_ops::compare_event_stream_rows`].
67///
68/// # Returns
69///
70/// Returns the computed value produced by [`crate::runtime::json_ops::compare_event_stream_rows`].
71///
72/// # Examples
73///
74/// ```rust,ignore
75/// let _result = pharos_native::runtime::json_ops::compare_event_stream_rows(todo!(), todo!());
76/// ```
77///
78/// # Related
79///
80/// - [`crate::runtime::json_ops`] for adjacent helpers that share the same runtime surface.
81pub fn compare_event_stream_rows(
82    lhs: &EventStreamRowRuntime,
83    rhs: &EventStreamRowRuntime,
84) -> Ordering {
85    lhs.created_at
86        .as_deref()
87        .unwrap_or_default()
88        .cmp(rhs.created_at.as_deref().unwrap_or_default())
89        .then(
90            lhs.source
91                .as_deref()
92                .unwrap_or_default()
93                .cmp(rhs.source.as_deref().unwrap_or_default()),
94        )
95        .then(lhs.native_order.cmp(&rhs.native_order))
96}
97
98#[derive(Debug, Clone, Eq, PartialEq)]
99/// Public struct `GeneratedSurfaceOrderItemRuntime` used by this module.
100///
101/// # Fields
102///
103/// - `sort_key`: Stored field on `GeneratedSurfaceOrderItemRuntime`.
104/// - `record_id`: Stored field on `GeneratedSurfaceOrderItemRuntime`.
105///
106/// # Examples
107///
108/// ```rust,ignore
109/// let _ = std::mem::size_of::<pharos_native::runtime::json_ops::GeneratedSurfaceOrderItemRuntime>();
110/// ```
111///
112/// # Related
113///
114/// - [`crate::runtime::json_ops`] for constructors, helpers, and neighboring runtime types.
115pub struct GeneratedSurfaceOrderItemRuntime {
116    pub sort_key: i64,
117    pub record_id: i64,
118}
119
120/// Compares generated surface order item.
121///
122/// # Parameters
123///
124/// - `left`: Input consumed by [`crate::runtime::json_ops::compare_generated_surface_order_item`].
125/// - `right`: Input consumed by [`crate::runtime::json_ops::compare_generated_surface_order_item`].
126///
127/// # Returns
128///
129/// Returns the computed value produced by [`crate::runtime::json_ops::compare_generated_surface_order_item`].
130///
131/// # Examples
132///
133/// ```rust,ignore
134/// let _result = pharos_native::runtime::json_ops::compare_generated_surface_order_item(todo!(), todo!());
135/// ```
136///
137/// # Related
138///
139/// - [`crate::runtime::json_ops`] for adjacent helpers that share the same runtime surface.
140pub fn compare_generated_surface_order_item(
141    left: &GeneratedSurfaceOrderItemRuntime,
142    right: &GeneratedSurfaceOrderItemRuntime,
143) -> Ordering {
144    left.sort_key
145        .cmp(&right.sort_key)
146        .then(left.record_id.cmp(&right.record_id))
147}