fix: mint info deserlization

This commit is contained in:
thesimplekid
2024-07-08 16:04:15 +01:00
parent 0d16b44884
commit 658d85c16d
6 changed files with 21 additions and 63 deletions

View File

@@ -66,7 +66,7 @@ enum Commands {
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::WARN)
.with_max_level(tracing::Level::DEBUG)
.init();
// Parse input

View File

@@ -142,8 +142,8 @@ async fn main() -> anyhow::Result<()> {
Vec<MppMethodSettings>,
) = ln_backends.iter().fold(
(
nut04::Settings::default(),
nut05::Settings::default(),
nut04::Settings::new(vec![], false),
nut05::Settings::new(vec![], false),
Vec::new(),
),
|(mut nut_04, mut nut_05, mut mpp), (key, ln)| {

View File

@@ -217,6 +217,13 @@ pub struct Settings {
pub disabled: bool,
}
impl Settings {
/// Create new [`Settings`]
pub fn new(methods: Vec<MintMethodSettings>, disabled: bool) -> Self {
Self { methods, disabled }
}
}
impl Default for Settings {
fn default() -> Self {
let bolt11_mint = MintMethodSettings {

View File

@@ -252,6 +252,13 @@ pub struct MeltMethodSettings {
pub max_amount: Option<Amount>,
}
impl Settings {
/// Create new [`Settings`]
pub fn new(methods: Vec<MeltMethodSettings>, disabled: bool) -> Self {
Self { methods, disabled }
}
}
/// Melt Settings
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Settings {

View File

@@ -2,9 +2,6 @@
//!
//! <https://github.com/cashubtc/nuts/blob/main/06.md>
use std::fmt;
use serde::de::{self, SeqAccess, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use super::nut01::PublicKey;
@@ -73,7 +70,6 @@ pub struct MintInfo {
pub description_long: Option<String>,
/// Contact info
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(deserialize_with = "deserialize_contact_info")]
pub contact: Option<Vec<ContactInfo>>,
/// shows which NUTs the mint supports
pub nuts: Nuts,
@@ -336,61 +332,6 @@ impl ContactInfo {
}
}
fn deserialize_contact_info<'de, D>(deserializer: D) -> Result<Option<Vec<ContactInfo>>, D::Error>
where
D: Deserializer<'de>,
{
struct ContactInfoVisitor;
impl<'de> Visitor<'de> for ContactInfoVisitor {
type Value = Option<Vec<ContactInfo>>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a list of ContactInfo or a list of lists of strings")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let mut contacts = Vec::new();
while let Some(value) = seq.next_element::<serde_json::Value>()? {
if value.is_object() {
// Deserialize as ContactInfo
let contact: ContactInfo =
serde_json::from_value(value).map_err(de::Error::custom)?;
contacts.push(contact);
} else if value.is_array() {
// Deserialize as Vec<String>
let vec = value
.as_array()
.ok_or_else(|| de::Error::custom("expected a list of strings"))?;
if vec.len() == 2 {
let method = vec[0]
.as_str()
.ok_or_else(|| de::Error::custom("expected a string"))?
.to_string();
let info = vec[1]
.as_str()
.ok_or_else(|| de::Error::custom("expected a string"))?
.to_string();
contacts.push(ContactInfo { method, info });
} else {
return Err(de::Error::custom("expected a list of two strings"));
}
} else {
return Err(de::Error::custom("expected an object or a list of strings"));
}
}
Ok(Some(contacts))
}
}
deserializer.deserialize_seq(ContactInfoVisitor)
}
#[cfg(test)]
mod tests {

View File

@@ -294,7 +294,10 @@ impl HttpClient {
match serde_json::from_value::<MintInfo>(res.clone()) {
Ok(melt_quote_response) => Ok(melt_quote_response),
Err(_) => Err(ErrorResponse::from_value(res)?.into()),
Err(err) => {
tracing::error!("Could not get mint info: {}", err);
Err(ErrorResponse::from_value(res)?.into())
}
}
}