1use self::{error::Error, format::SerializationFormats};
2use crate::derivation::HashFunctionCode;
3use core::str::FromStr;
4use serde::{Deserialize, Deserializer, Serialize, Serializer};
5pub mod error;
6pub mod format;
7#[derive(Debug, Clone, PartialEq)]
10pub struct SerializationInfo {
11 pub protocol_code: String,
12 pub major_version: u8,
13 pub minor_version: u8,
14 pub size: usize,
15 pub kind: SerializationFormats,
16}
17
18impl SerializationInfo {
19 pub fn new(
20 protocol: String,
21 major_version: u8,
22 minor_version: u8,
23 kind: SerializationFormats,
24 size: usize,
25 ) -> Self {
26 Self {
27 protocol_code: protocol,
28 major_version,
29 minor_version,
30 size,
31 kind,
32 }
33 }
34
35 pub fn new_empty(
36 protocol: String,
37 major_version: u8,
38 minor_version: u8,
39 kind: SerializationFormats,
40 ) -> Self {
41 Self {
42 protocol_code: protocol,
43 major_version,
44 minor_version,
45 size: 0,
46 kind,
47 }
48 }
49
50 pub fn serialize<T: Serialize>(&self, t: &T) -> Result<Vec<u8>, Error> {
51 self.kind.encode(t)
52 }
53
54 pub fn to_str(&self) -> String {
55 format!(
56 "{}{:x}{:x}{}{:06x}_",
57 self.protocol_code,
58 self.major_version,
59 self.minor_version,
60 self.kind.to_str(),
61 self.size
62 )
63 }
64}
65
66impl FromStr for SerializationInfo {
67 type Err = Error;
68 fn from_str(s: &str) -> Result<Self, Self::Err> {
69 Ok(Self {
70 protocol_code: s[..4].to_string(),
71 major_version: u8::from_str_radix(&s[4..5], 16)?,
72 minor_version: u8::from_str_radix(&s[5..6], 16)?,
73 kind: SerializationFormats::from_str(&s[6..10])?,
74 size: u16::from_str_radix(&s[10..16], 16)? as usize,
75 })
76 }
77}
78
79impl Serialize for SerializationInfo {
81 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
82 where
83 S: Serializer,
84 {
85 serializer.serialize_str(&self.to_str())
86 }
87}
88
89impl<'de> Deserialize<'de> for SerializationInfo {
91 fn deserialize<D>(deserializer: D) -> Result<SerializationInfo, D::Error>
92 where
93 D: Deserializer<'de>,
94 {
95 let s = String::deserialize(deserializer)?;
96
97 SerializationInfo::from_str(&s).map_err(serde::de::Error::custom)
98 }
99}
100
101impl Default for SerializationInfo {
102 fn default() -> Self {
103 Self {
104 protocol_code: "KERI".to_string(),
105 major_version: 1,
106 minor_version: 0,
107 size: 0,
108 kind: SerializationFormats::JSON,
109 }
110 }
111}
112
113pub trait Encode {
114 fn encode(
115 &self,
116 code: &HashFunctionCode,
117 serialization_format: &SerializationFormats,
118 ) -> Result<Vec<u8>, Error>;
119}
120
121#[cfg(test)]
122mod tests {
123 use std::str::FromStr;
124
125 use crate::version::{error::Error, format::SerializationFormats, SerializationInfo};
126
127 #[test]
128 fn test_version_from_str() {
129 let json = r#"KERI10JSON00014b_"#;
130 let json_result = json.parse::<SerializationInfo>();
131 assert!(json_result.is_ok());
132 assert_eq!(&json, &json_result.unwrap().to_str());
133 }
134
135 #[test]
136 fn test_serialization_info_to_str() -> Result<(), Error> {
137 let si = SerializationInfo::new("KERI".to_string(), 1, 0, SerializationFormats::JSON, 100);
138
139 let version_string = si.to_str();
140 assert_eq!("KERI10JSON000064_", &version_string);
141 Ok(())
142 }
143
144 #[test]
145 fn test_serialization_info_from_str() -> Result<(), Error> {
146 let si = SerializationInfo::from_str("KERIa4CBOR000123_")?;
147
148 assert_eq!(si.protocol_code, "KERI".to_string());
149 assert_eq!(si.kind, SerializationFormats::CBOR);
150 assert_eq!(si.major_version, 10);
151 assert_eq!(si.minor_version, 4);
152 assert_eq!(si.size, 291);
153 Ok(())
154 }
155}