qlib_rs/lib.rs
1mod data;
2mod test;
3
4pub use data::{
5 epoch, now, resolve_indirection, AdjustBehavior, BadIndirection, BadIndirectionReason, Context,
6 Entity, EntityId, EntitySchema, Field, FieldSchema, FieldType, Store, PageOpts, PageResult,
7 PushCondition, Request, Snowflake, Timestamp, Value, INDIRECTION_DELIMITER,
8 Single, Complete,
9};
10
11/// Create a Read request with minimal syntax
12///
13/// # Arguments
14///
15/// * `entity_id` - The entity ID to read from
16/// * `field_type` - The field type to read
17#[macro_export]
18macro_rules! sread {
19 ($entity_id:expr, $field_type:expr) => {
20 $crate::Request::Read {
21 entity_id: $entity_id,
22 field_type: $field_type,
23 value: None,
24 write_time: None,
25 writer_id: None,
26 }
27 };
28}
29
30/// Create a Write request with minimal syntax
31///
32/// # Arguments
33///
34/// * `entity_id` - The entity ID to write to
35/// * `field_type` - The field type to write
36/// * `value` - The value to write (must be a Some(Value) or None)
37/// * `push_condition` - (optional) The write option, defaults to Normal
38/// * `write_time` - (optional) The write time
39/// * `writer_id` - (optional) The writer ID
40#[macro_export]
41macro_rules! swrite {
42 // Basic version with no value: handle Some/None
43 ($entity_id:expr, $field_type:expr) => {
44 $crate::Request::Write {
45 entity_id: $entity_id,
46 field_type: $field_type,
47 value: None,
48 push_condition: $crate::PushCondition::Always,
49 adjust_behavior: $crate::AdjustBehavior::Set,
50 write_time: None,
51 writer_id: None,
52 }
53 };
54
55 // Basic version with just value: handle Some/None
56 ($entity_id:expr, $field_type:expr, $value:expr) => {
57 $crate::Request::Write {
58 entity_id: $entity_id,
59 field_type: $field_type,
60 value: $value,
61 push_condition: $crate::PushCondition::Always,
62 adjust_behavior: $crate::AdjustBehavior::Set,
63 write_time: None,
64 writer_id: None,
65 }
66 };
67
68 // With write option
69 ($entity_id:expr, $field_type:expr, $value:expr, $push_condition:expr) => {
70 $crate::Request::Write {
71 entity_id: $entity_id,
72 field_type: $field_type,
73 value: $value,
74 push_condition: $push_condition,
75 adjust_behavior: $crate::AdjustBehavior::Set,
76 write_time: None,
77 writer_id: None,
78 }
79 };
80
81 // With write option and write time
82 ($entity_id:expr, $field_type:expr, $value:expr, $push_condition:expr, $write_time:expr) => {
83 $crate::Request::Write {
84 entity_id: $entity_id,
85 field_type: $field_type,
86 value: $value,
87 push_condition: $push_condition,
88 adjust_behavior: $crate::AdjustBehavior::Set,
89 write_time: $write_time,
90 writer_id: None,
91 }
92 };
93
94 // With write option, write time, and writer ID
95 ($entity_id:expr, $field_type:expr, $value:expr, $push_condition:expr, $write_time:expr, $writer_id:expr) => {
96 $crate::Request::Write {
97 entity_id: $entity_id,
98 field_type: $field_type,
99 value: $value,
100 push_condition: $push_condition,
101 adjust_behavior: $crate::AdjustBehavior::Set,
102 write_time: $write_time,
103 writer_id: $writer_id,
104 }
105 };
106}
107
108/// Create a Write request with Add adjustment behavior
109///
110/// This macro creates a `Request::Write` with `AdjustBehavior::Add`, which is useful for
111/// incrementing values, appending to lists, or concatenating strings.
112///
113/// # Arguments
114///
115/// * `entity_id` - The entity ID to write to
116/// * `field_type` - The field type to write
117/// * `value` - The value to add (must be a Some(Value) or None)
118/// * `push_condition` - (optional) The write option, defaults to Always
119/// * `write_time` - (optional) The write time
120/// * `writer_id` - (optional) The writer ID
121#[macro_export]
122macro_rules! sadd {
123 // Basic version with just value
124 ($entity_id:expr, $field_type:expr, $value:expr) => {
125 $crate::Request::Write {
126 entity_id: $entity_id,
127 field_type: $field_type,
128 value: $value,
129 push_condition: $crate::PushCondition::Always,
130 adjust_behavior: $crate::AdjustBehavior::Add,
131 write_time: None,
132 writer_id: None,
133 }
134 };
135
136 // With write option
137 ($entity_id:expr, $field_type:expr, $value:expr, $push_condition:expr) => {
138 $crate::Request::Write {
139 entity_id: $entity_id,
140 field_type: $field_type,
141 value: $value,
142 push_condition: $push_condition,
143 adjust_behavior: $crate::AdjustBehavior::Add,
144 write_time: None,
145 writer_id: None,
146 }
147 };
148
149 // With write option and write time
150 ($entity_id:expr, $field_type:expr, $value:expr, $push_condition:expr, $write_time:expr) => {
151 $crate::Request::Write {
152 entity_id: $entity_id,
153 field_type: $field_type,
154 value: $value,
155 push_condition: $push_condition,
156 adjust_behavior: $crate::AdjustBehavior::Add,
157 write_time: $write_time,
158 writer_id: None,
159 }
160 };
161
162 // With write option, write time, and writer ID
163 ($entity_id:expr, $field_type:expr, $value:expr, $push_condition:expr, $write_time:expr, $writer_id:expr) => {
164 $crate::Request::Write {
165 entity_id: $entity_id,
166 field_type: $field_type,
167 value: $value,
168 push_condition: $push_condition,
169 adjust_behavior: $crate::AdjustBehavior::Add,
170 write_time: $write_time,
171 writer_id: $writer_id,
172 }
173 };
174}
175
176/// Create a Write request with Subtract adjustment behavior
177///
178/// This macro creates a `Request::Write` with `AdjustBehavior::Subtract`, which is useful for
179/// decrementing values or removing items from lists.
180///
181/// # Arguments
182///
183/// * `entity_id` - The entity ID to write to
184/// * `field_type` - The field type to write
185/// * `value` - The value to subtract (must be a Some(Value) or None)
186/// * `push_condition` - (optional) The write option, defaults to Always
187/// * `write_time` - (optional) The write time
188/// * `writer_id` - (optional) The writer ID
189#[macro_export]
190macro_rules! ssub {
191 // Basic version with just value
192 ($entity_id:expr, $field_type:expr, $value:expr) => {
193 $crate::Request::Write {
194 entity_id: $entity_id,
195 field_type: $field_type,
196 value: $value,
197 push_condition: $crate::PushCondition::Always,
198 adjust_behavior: $crate::AdjustBehavior::Subtract,
199 write_time: None,
200 writer_id: None,
201 }
202 };
203
204 // With write option
205 ($entity_id:expr, $field_type:expr, $value:expr, $push_condition:expr) => {
206 $crate::Request::Write {
207 entity_id: $entity_id,
208 field_type: $field_type,
209 value: $value,
210 push_condition: $push_condition,
211 adjust_behavior: $crate::AdjustBehavior::Subtract,
212 write_time: None,
213 writer_id: None,
214 }
215 };
216
217 // With write option and write time
218 ($entity_id:expr, $field_type:expr, $value:expr, $push_condition:expr, $write_time:expr) => {
219 $crate::Request::Write {
220 entity_id: $entity_id,
221 field_type: $field_type,
222 value: $value,
223 push_condition: $push_condition,
224 adjust_behavior: $crate::AdjustBehavior::Subtract,
225 write_time: $write_time,
226 writer_id: None,
227 }
228 };
229
230 // With write option, write time, and writer ID
231 ($entity_id:expr, $field_type:expr, $value:expr, $push_condition:expr, $write_time:expr, $writer_id:expr) => {
232 $crate::Request::Write {
233 entity_id: $entity_id,
234 field_type: $field_type,
235 value: $value,
236 push_condition: $push_condition,
237 adjust_behavior: $crate::AdjustBehavior::Subtract,
238 write_time: $write_time,
239 writer_id: $writer_id,
240 }
241 };
242}
243
244pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
245
246/// Creates a `Some(Value::Bool)` for use in write requests.
247///
248/// This macro wraps a boolean value in `Some(Value::Bool)`, making it ready
249/// for use with `swrite!` macro or any function expecting an `Option<Value>`.
250///
251/// # Arguments
252///
253/// * `$value` - A boolean value (`true` or `false`)
254///
255/// # Returns
256///
257/// * `Some(Value::Bool)` - The wrapped boolean value
258#[macro_export]
259macro_rules! sbool {
260 ($value:expr) => {
261 Some($crate::Value::Bool($value))
262 };
263}
264
265/// Creates a `Some(Value::Int)` for use in write requests.
266///
267/// This macro wraps an integer value in `Some(Value::Int)`, making it ready
268/// for use with `swrite!` macro or any function expecting an `Option<Value>`.
269///
270/// # Arguments
271///
272/// * `$value` - An integer value (will be converted to i64)
273///
274/// # Returns
275///
276/// * `Some(Value::Int)` - The wrapped integer value
277#[macro_export]
278macro_rules! sint {
279 ($value:expr) => {
280 Some($crate::Value::Int($value))
281 };
282}
283
284/// Creates a `Some(Value::Float)` for use in write requests.
285///
286/// This macro wraps a floating-point value in `Some(Value::Float)`, making it ready
287/// for use with `swrite!` macro or any function expecting an `Option<Value>`.
288///
289/// # Arguments
290///
291/// * `$value` - A floating-point value (will be converted to f64)
292///
293/// # Returns
294///
295/// * `Some(Value::Float)` - The wrapped floating-point value
296#[macro_export]
297macro_rules! sfloat {
298 ($value:expr) => {
299 Some($crate::Value::Float($value))
300 };
301}
302
303/// Creates a `Some(Value::String)` for use in write requests.
304///
305/// This macro wraps a string value in `Some(Value::String)`, making it ready
306/// for use with `swrite!` macro or any function expecting an `Option<Value>`.
307/// The input will be converted to a String using `to_string()`.
308///
309/// # Arguments
310///
311/// * `$value` - A string-like value that can be converted to String
312///
313/// # Returns
314///
315/// * `Some(Value::String)` - The wrapped string value
316#[macro_export]
317macro_rules! sstr {
318 ($value:expr) => {
319 Some($crate::Value::String($value.into()))
320 };
321}
322
323/// Creates a `Some(Value::EntityReference)` for use in write requests.
324///
325/// This macro wraps an entity reference string in `Some(Value::EntityReference)`,
326/// making it ready for use with `swrite!` macro or any function expecting an `Option<Value>`.
327/// The input will be converted to a String using `to_string()`.
328///
329/// # Arguments
330///
331/// * `$value` - A string-like value representing an entity reference
332///
333/// # Returns
334///
335/// * `Some(Value::EntityReference)` - The wrapped entity reference
336#[macro_export]
337macro_rules! sref {
338 ($value:expr) => {
339 Some($crate::Value::EntityReference($value))
340 };
341}
342
343/// Creates a `Some(Value::EntityList)` for use in write requests.
344///
345/// This macro wraps a list of entity references in `Some(Value::EntityList)`,
346/// making it ready for use with `swrite!` macro or any function expecting an
347/// `Option<Value>`. It can be used in three ways:
348/// 1. With no arguments: creates an empty list
349/// 2. With multiple arguments: creates a list from those arguments
350/// 3. With a single Vec: wraps the existing Vec
351///
352/// Each input item will be converted to a String using `to_string()`.
353///
354/// # Arguments
355///
356/// * `$value` - Either nothing, a Vec<String>, or a comma-separated list of values
357///
358/// # Returns
359///
360/// * `Some(Value::EntityList)` - The wrapped entity list
361#[macro_export]
362macro_rules! sreflist {
363 [] => {
364 Some($crate::Value::EntityList(Vec::new()))
365 };
366 [$($value:expr),*] => {
367 {
368 let mut v = Vec::<EntityId>::new();
369 $(
370 v.push($value);
371 )*
372 Some($crate::Value::EntityList(v))
373 }
374 };
375 ($value:expr) => {
376 Some($crate::Value::EntityList($value.clone()))
377 };
378}
379
380/// Creates a `Some(Value::Choice)` for use in write requests.
381///
382/// This macro wraps an integer value in `Some(Value::Choice)`, making it ready
383/// for use with `swrite!` macro or any function expecting an `Option<Value>`.
384/// The Choice variant typically represents a selection from a predefined set of options.
385///
386/// # Arguments
387///
388/// * `$value` - An integer value representing the selected choice (will be converted to i64)
389///
390/// # Returns
391///
392/// * `Some(Value::Choice)` - The wrapped choice value
393#[macro_export]
394macro_rules! schoice {
395 ($value:expr) => {
396 Some($crate::Value::Choice($value))
397 };
398}
399
400/// Creates a `Some(Value::Timestamp)` for use in write requests.
401///
402/// This macro wraps a timestamp value in `Some(Value::Timestamp)`, making it ready
403/// for use with `swrite!` macro or any function expecting an `Option<Value>`.
404///
405/// # Arguments
406///
407/// * `$value` - A SystemTime value
408///
409/// # Returns
410///
411/// * `Some(Value::Timestamp)` - The wrapped timestamp value
412#[macro_export]
413macro_rules! stimestamp {
414 ($value:expr) => {
415 Some($crate::Value::Timestamp($value))
416 };
417}
418
419/// Creates a `Some(Value::BinaryFile)` for use in write requests.
420///
421/// This macro wraps binary data in `Some(Value::BinaryFile)`, making it ready
422/// for use with `swrite!` macro or any function expecting an `Option<Value>`.
423///
424/// # Arguments
425///
426/// * `$value` - A Vec<u8> containing binary data
427///
428/// # Returns
429///
430/// * `Some(Value::BinaryFile)` - The wrapped binary data
431#[macro_export]
432macro_rules! sbinfile {
433 ($value:expr) => {
434 Some($crate::Value::BinaryFile($value))
435 };
436}