|
1 | 1 | use std::hash::{Hash, Hasher}; |
2 | 2 | use std::num::NonZeroUsize; |
| 3 | +use std::str::FromStr; |
| 4 | + |
| 5 | +use serde::{Deserialize, Deserializer, Serialize, Serializer}; |
3 | 6 |
|
4 | 7 | use crate::ai::models::AiStoreInputType; |
5 | 8 | use crate::client::ConnectedClient; |
6 | 9 | use crate::keyval::store_input::Value; |
7 | | -use crate::keyval::StoreInput; |
| 10 | +use crate::keyval::{StoreInput, StoreName}; |
| 11 | +use crate::metadata::metadata_value::Value as MetadataValueInner; |
8 | 12 | use crate::metadata::MetadataValue; |
9 | 13 | use crate::predicates::{AndCondition, Equals, In, NotEquals, NotIn, OrCondition}; |
10 | 14 | use crate::shared::info::StoreUpsert; |
@@ -40,6 +44,74 @@ impl TryFrom<MetadataValue> for StoreInput { |
40 | 44 | } |
41 | 45 | } |
42 | 46 |
|
| 47 | +impl std::fmt::Display for MetadataValueInner { |
| 48 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 49 | + match self { |
| 50 | + MetadataValueInner::Image(bytes) => write!(f, "img:{}", ascii85::encode(bytes)), |
| 51 | + MetadataValueInner::RawString(s) => write!(f, "str:{}", s), |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl Serialize for MetadataValue { |
| 57 | + fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error> |
| 58 | + where |
| 59 | + S: Serializer, |
| 60 | + { |
| 61 | + match &self.value { |
| 62 | + Some(v) => s.serialize_str(&v.to_string()), |
| 63 | + None => Err(serde::ser::Error::custom( |
| 64 | + "Metadata value struct is empty, cannot serialize", |
| 65 | + )), |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl Serialize for StoreName { |
| 71 | + fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error> |
| 72 | + where |
| 73 | + S: Serializer, |
| 74 | + { |
| 75 | + s.serialize_str(&self.value) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl<'de> Deserialize<'de> for StoreName { |
| 80 | + fn deserialize<D>(d: D) -> Result<Self, D::Error> |
| 81 | + where |
| 82 | + D: Deserializer<'de>, |
| 83 | + { |
| 84 | + let value = String::deserialize(d)?; |
| 85 | + Ok(StoreName { value }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// If you need deserialization back into MyKey: |
| 90 | +impl FromStr for MetadataValueInner { |
| 91 | + type Err = String; |
| 92 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 93 | + if let Some(rest) = s.strip_prefix("img:") { |
| 94 | + let bytes = ascii85::decode(rest).map_err(|e| e.to_string())?; |
| 95 | + Ok(MetadataValueInner::Image(bytes)) |
| 96 | + } else if let Some(rest) = s.strip_prefix("str:") { |
| 97 | + Ok(MetadataValueInner::RawString(rest.to_string())) |
| 98 | + } else { |
| 99 | + Err("unknown key format".into()) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | +impl<'de> Deserialize<'de> for MetadataValue { |
| 104 | + fn deserialize<D>(d: D) -> Result<Self, D::Error> |
| 105 | + where |
| 106 | + D: Deserializer<'de>, |
| 107 | + { |
| 108 | + let s = String::deserialize(d)?; |
| 109 | + Ok(MetadataValue { |
| 110 | + value: Some(MetadataValueInner::from_str(&s).map_err(serde::de::Error::custom)?), |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
| 114 | + |
43 | 115 | impl TryFrom<&StoreInput> for AiStoreInputType { |
44 | 116 | type Error = (); |
45 | 117 |
|
|
0 commit comments