11//! The primary Card object as defined in RFC 9553
22
3- use std:: collections:: HashMap ;
3+ use std:: { collections:: HashMap , str :: FromStr } ;
44
55use serde:: { Deserialize , Serialize } ;
66use serde_json:: Value ;
@@ -14,7 +14,7 @@ use crate::{
1414use crate :: { AddressComponent , AddressComponentKind , NameComponent } ;
1515
1616/// Represents the primary Card object as defined in RFC 9553, storing metadata and contact properties.
17- #[ derive( Serialize , Deserialize , Debug , Clone ) ]
17+ #[ derive( Serialize , Deserialize , Debug , Clone , PartialEq ) ]
1818#[ serde( rename_all = "camelCase" ) ]
1919pub struct Card {
2020 /// The JSContact type of the Card object. Must be "Card".
@@ -180,41 +180,13 @@ impl Card {
180180 }
181181 }
182182
183- /// Wrapper around serde_json
184- /// # Errors
185- /// Will return an error if the input is not a valid Card object.
186- pub fn from_slice ( json_slice : & [ u8 ] ) -> Result < Self , serde_json:: Error > {
187- serde_json:: from_slice ( json_slice)
188- }
189-
190183 /// Wrapper around serde_json
191184 /// # Errors
192185 /// Will return an error if the input is not a valid Card object.
193186 pub fn from_reader < R : std:: io:: Read > ( reader : R ) -> Result < Self , serde_json:: Error > {
194187 serde_json:: from_reader ( reader)
195188 }
196189
197- /// Wrapper around serde_json
198- /// # Errors
199- /// Will return an error if the input is not a valid Card object.
200- pub fn from_value ( value : Value ) -> Result < Self , serde_json:: Error > {
201- serde_json:: from_value ( value)
202- }
203-
204- /// Wrapper around serde_json
205- /// # Errors
206- /// Will return an error if the input is not a valid Card object.
207- pub fn try_from_str ( json_string : String ) -> Result < Self , serde_json:: Error > {
208- serde_json:: from_str ( & json_string)
209- }
210-
211- /// Wrapper around serde_json
212- /// # Errors
213- /// Will return an error if the input is not a valid Card object.
214- pub fn serialize_str ( & self ) -> Result < String , serde_json:: Error > {
215- serde_json:: to_string ( self )
216- }
217-
218190 /// Creates a new Card object with the latest version and the specified unique identifier.
219191 pub fn new_with_latest_version ( uid : & str ) -> Self {
220192 Self {
@@ -244,7 +216,7 @@ impl Card {
244216 } ;
245217 }
246218
247- /// Get available languages in the Card object.
219+ /// Get available languages from the [` Card::localizations`]
248220 pub fn get_available_languages ( & self ) -> Vec < String > {
249221 match & self . localizations {
250222 Some ( localizations_map) => localizations_map. keys ( ) . cloned ( ) . collect ( ) ,
@@ -276,6 +248,39 @@ impl Card {
276248 }
277249}
278250
251+ impl FromStr for Card {
252+ type Err = serde_json:: Error ;
253+
254+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
255+ serde_json:: from_str ( s)
256+ }
257+ }
258+
259+ impl TryFrom < & [ u8 ] > for Card {
260+ type Error = serde_json:: Error ;
261+
262+ fn try_from ( slice : & [ u8 ] ) -> Result < Self , Self :: Error > {
263+ serde_json:: from_slice ( slice)
264+ }
265+ }
266+
267+ impl TryFrom < Value > for Card {
268+ type Error = String ;
269+
270+ fn try_from ( value : Value ) -> Result < Self , Self :: Error > {
271+ let card: Card = serde_json:: from_value ( value. clone ( ) ) . map_err ( |e| e. to_string ( ) ) ?;
272+ Ok ( card)
273+ }
274+ }
275+
276+ impl TryFrom < Card > for String {
277+ type Error = serde_json:: Error ;
278+
279+ fn try_from ( card : Card ) -> Result < Self , Self :: Error > {
280+ serde_json:: to_string ( & card)
281+ }
282+ }
283+
279284/// Localize the Card object with jsonptr
280285#[ cfg( feature = "jsonptr" ) ]
281286fn localize_card (
0 commit comments