Skip to main content

agentic_core/utils/
common.rs

1use chrono::Utc;
2use uuid::Uuid;
3
4#[must_use]
5pub fn uuid7_str(prefix: &str) -> String {
6    format!("{}{}", prefix, Uuid::now_v7())
7}
8
9#[must_use]
10pub fn utcnow_str() -> i64 {
11    Utc::now().timestamp()
12}
13
14/// Serialize any type to JSON string.
15///
16/// Strict serialization - returns error if serialization fails.
17/// Used in persistence operations where we control the data types.
18///
19/// # Errors
20///
21/// Returns `serde_json::Error` if serialization fails.
22pub fn serialize_to_string<T: serde::Serialize>(value: &T) -> Result<String, serde_json::Error> {
23    serde_json::to_string(value)
24}
25
26/// Deserialize JSON string to any type.
27///
28/// Strict deserialization - returns error if deserialization fails.
29/// Used when we need explicit error handling for data integrity.
30///
31/// # Errors
32///
33/// Returns `serde_json::Error` if deserialization fails.
34pub fn deserialize_from_str<T: serde::de::DeserializeOwned>(json_str: &str) -> Result<T, serde_json::Error> {
35    serde_json::from_str(json_str)
36}
37
38/// Deserialize JSON string to any type with Default fallback.
39///
40/// Graceful deserialization - returns default value on error or empty string.
41/// Used in read operations where we accept corrupted data gracefully.
42#[must_use]
43pub fn deserialize_from_str_or_default<T: serde::de::DeserializeOwned + Default>(json_str: &str) -> T {
44    serde_json::from_str(json_str).unwrap_or_default()
45}
46
47/// Deserialize JSON string to any type, returning None on error.
48///
49/// Optional deserialization - returns None if JSON is invalid.
50/// Convenience function for cases where None represents missing data.
51#[must_use]
52pub fn deserialize_from_str_opt<T: serde::de::DeserializeOwned>(json_str: &str) -> Option<T> {
53    serde_json::from_str(json_str).ok()
54}
55
56/// Deserialize optional JSON String to any type, returning default on error or if None.
57///
58/// Graceful optional deserialization - returns default value for T if missing or invalid.
59#[must_use]
60pub fn deserialize_from_string_opt_or_default<T: serde::de::DeserializeOwned + Default>(
61    json_str: &Option<String>,
62) -> T {
63    json_str
64        .as_ref()
65        .and_then(|s| deserialize_from_str_opt::<T>(s))
66        .unwrap_or_default()
67}
68
69/// Deserialize optional JSON String to any type, returning None on error or if None.
70///
71/// Optional deserialization - returns None if missing or invalid JSON.
72#[must_use]
73pub fn deserialize_from_string_opt<T: serde::de::DeserializeOwned>(json_str: &Option<String>) -> Option<T> {
74    json_str.as_ref().and_then(|s| deserialize_from_str_opt::<T>(s))
75}
76
77/// Deserialize a `serde_json::Value` into `T`.
78///
79/// # Errors
80///
81/// Returns `serde_json::Error` if the value's shape does not match `T`.
82pub fn deserialize_from_value<T: serde::de::DeserializeOwned>(
83    value: serde_json::Value,
84) -> Result<T, serde_json::Error> {
85    serde_json::from_value(value)
86}
87
88/// Deserialize a `serde_json::Value` into `T`, returning `None` on type mismatch.
89#[must_use]
90pub fn deserialize_from_value_opt<T: serde::de::DeserializeOwned>(value: serde_json::Value) -> Option<T> {
91    serde_json::from_value(value).ok()
92}
93
94/// Serialize any type to JSON bytes, returning an empty `Vec` on error.
95#[must_use]
96pub fn serialize_to_vec_or_default<T: serde::Serialize>(value: &T) -> Vec<u8> {
97    serde_json::to_vec(value).unwrap_or_default()
98}