Преглед изворни кода

`Mint_Info` feilds as options

thesimplekid пре 1 година
родитељ
комит
753c70115e
2 измењених фајлова са 42 додато и 7 уклоњено
  1. 35 0
      src/serde_utils.rs
  2. 7 7
      src/types.rs

+ 35 - 0
src/serde_utils.rs

@@ -64,4 +64,39 @@ pub mod serde_public_key {
         let decoded = hex::decode(encoded).map_err(serde::de::Error::custom)?;
         PublicKey::from_sec1_bytes(&decoded).map_err(serde::de::Error::custom)
     }
+
+    pub mod opt {
+        use k256::PublicKey;
+        use serde::{Deserialize, Deserializer};
+
+        pub fn serialize<S>(pubkey: &Option<PublicKey>, serializer: S) -> Result<S::Ok, S::Error>
+        where
+            S: serde::Serializer,
+        {
+            match pubkey {
+                Some(pubkey) => {
+                    let encoded = hex::encode(pubkey.to_sec1_bytes());
+                    serializer.serialize_str(&encoded)
+                }
+                None => serializer.serialize_none(),
+            }
+        }
+
+        pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<PublicKey>, D::Error>
+        where
+            D: Deserializer<'de>,
+        {
+            let option_str: Option<String> = Option::deserialize(deserializer)?;
+
+            match option_str {
+                Some(encoded) => {
+                    let bytes = hex::decode(encoded).map_err(serde::de::Error::custom)?;
+                    let pubkey =
+                        PublicKey::from_sec1_bytes(&bytes).map_err(serde::de::Error::custom)?;
+                    Ok(Some(pubkey))
+                }
+                None => Ok(None),
+            }
+        }
+    }
 }

+ 7 - 7
src/types.rs

@@ -270,22 +270,22 @@ impl<'de> Deserialize<'de> for MintVersion {
 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
 pub struct MintInfo {
     /// name of the mint and should be recognizable
-    pub name: String,
+    pub name: Option<String>,
     /// hex pubkey of the mint
-    #[serde(with = "serde_utils::serde_public_key")]
-    pub pubkey: PublicKey,
+    #[serde(with = "serde_utils::serde_public_key::opt")]
+    pub pubkey: Option<PublicKey>,
     /// implementation name and the version running
-    pub version: MintVersion,
+    pub version: Option<MintVersion>,
     /// short description of the mint
-    pub description: String,
+    pub description: Option<String>,
     /// long description
-    pub description_long: String,
+    pub description_long: Option<String>,
     /// contact methods to reach the mint operator
     pub contact: Vec<Vec<String>>,
     /// shows which NUTs the mint supports
     pub nuts: Vec<String>,
     /// message of the day that the wallet must display to the user
-    pub motd: String,
+    pub motd: Option<String>,
 }
 
 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]