Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix: custom deserialize NonEmpty (#2967)

Eric Hayesed650a6c765f
diff --git a/rust/cloud-storage/Cargo.lock b/rust/cloud-storage/Cargo.lock
index 64cf315e48..8917880531 100644
--- a/rust/cloud-storage/Cargo.lock
+++ b/rust/cloud-storage/Cargo.lock
@@ -7215,6 +7215,7 @@ name = "non_empty"
version = "0.1.0"
dependencies = [
"serde",
+ "serde_json",
]
[[package]]
diff --git a/rust/cloud-storage/github/Cargo.toml b/rust/cloud-storage/github/Cargo.toml
index c0665bba08..2da6daa483 100644
--- a/rust/cloud-storage/github/Cargo.toml
+++ b/rust/cloud-storage/github/Cargo.toml
@@ -56,10 +56,10 @@ tokio = { workspace = true, features = ["sync"], optional = true }
axum = { workspace = true, optional = true }
axum-extra = { workspace = true, optional = true }
model-error-response = { path = "../model-error-response", optional = true }
-model-entity = { path = "../model-entity"}
utoipa = { workspace = true, optional = true }
[dev-dependencies]
+model-entity = { path = "../model-entity"}
document_sub_type = { path = "../document_sub_type" }
http-body-util = { workspace = true }
macro_db_migrator = { path = "../macro_db_migrator" }
diff --git a/rust/cloud-storage/non_empty/Cargo.toml b/rust/cloud-storage/non_empty/Cargo.toml
index a90ef3e864..20dbf2f257 100644
--- a/rust/cloud-storage/non_empty/Cargo.toml
+++ b/rust/cloud-storage/non_empty/Cargo.toml
@@ -6,3 +6,6 @@ publish = false
[dependencies]
serde = { workspace = true }
+
+[dev-dependencies]
+serde_json = { workspace = true }
diff --git a/rust/cloud-storage/non_empty/src/lib.rs b/rust/cloud-storage/non_empty/src/lib.rs
index c7efa3e5bc..f56755a6a3 100644
--- a/rust/cloud-storage/non_empty/src/lib.rs
+++ b/rust/cloud-storage/non_empty/src/lib.rs
@@ -1,5 +1,6 @@
//! Wrapper around types that implement IsEmpty trait to ensure they are non-empty.
+use serde::de::Error as _;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::ops::Deref;
@@ -46,12 +47,25 @@ impl std::error::Error for EmptyError {}
/// // Can use HashMap methods directly via Deref
/// assert_eq!(non_empty_map["key"], "value");
/// ```
-#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(transparent)]
pub struct NonEmpty<T> {
inner: T,
}
+impl<'de, T> Deserialize<'de> for NonEmpty<T>
+where
+ T: Deserialize<'de> + IsEmpty,
+{
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: serde::Deserializer<'de>,
+ {
+ let value = T::deserialize(deserializer)?;
+ NonEmpty::new(value).map_err(|_| D::Error::custom("expected a non-empty value"))
+ }
+}
+
impl<T> NonEmpty<Vec<T>> {
/// Create a new `NonEmpty` wrapper with one element
pub fn one(value: T) -> Self {
diff --git a/rust/cloud-storage/non_empty/src/test.rs b/rust/cloud-storage/non_empty/src/test.rs
index c790ff459f..a6d77f45cf 100644
--- a/rust/cloud-storage/non_empty/src/test.rs
+++ b/rust/cloud-storage/non_empty/src/test.rs
@@ -1,4 +1,5 @@
use super::*;
+use serde::Deserialize;
use std::collections::HashMap;
#[test]
@@ -62,3 +63,44 @@ fn test_into_inner() {
let inner = non_empty.into_inner();
assert_eq!(inner, vec![1, 2, 3]);
}
+
+#[derive(Debug, Deserialize)]
+struct Wrapper {
+ items: NonEmpty<Vec<i32>>,
+}
+
+#[test]
+fn test_deserialize_non_empty_vec() {
+ let json = r#"{"items": [1, 2, 3]}"#;
+ let wrapper: Wrapper = serde_json::from_str(json).unwrap();
+ assert_eq!(wrapper.items.len(), 3);
+}
+
+#[test]
+fn test_deserialize_empty_vec_fails() {
+ let json = r#"{"items": []}"#;
+ let result: Result<Wrapper, _> = serde_json::from_str(json);
+ assert!(result.is_err());
+}
+
+#[test]
+fn test_deserialize_roundtrip() {
+ let original = NonEmpty::new(vec![1, 2, 3]).unwrap();
+ let json = serde_json::to_string(&original).unwrap();
+ let deserialized: NonEmpty<Vec<i32>> = serde_json::from_str(&json).unwrap();
+ assert_eq!(original, deserialized);
+}
+
+#[test]
+fn test_deserialize_empty_string_fails() {
+ let json = r#""""#;
+ let result: Result<NonEmpty<String>, _> = serde_json::from_str(json);
+ assert!(result.is_err());
+}
+
+#[test]
+fn test_deserialize_non_empty_string() {
+ let json = r#""hello""#;
+ let result: NonEmpty<String> = serde_json::from_str(json).unwrap();
+ assert_eq!(result.inner(), "hello");
+}