diff --git a/Cargo.toml b/Cargo.toml index 918bd63..c0036bb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,8 +15,9 @@ readme = "README.md" [dependencies] base64 = { version = "0.21.0", features = ["std"], default-features = false } bech32 = { version = "0.9.0", default-features = false } -miniserde = { version = "0.1.0", default-features = false } -url = { version = "2.5.0", default-features = false } +serde = { version = "1.0.0", features = ["derive"], default-features = false } +serde_json = { version = "1.0.0", features = ["std"], default-features = false } +url = { version = "2.5.0", features = ["serde"], default-features = false } axum = { version = "0.7.0", default-features = false, optional = true } reqwest = { version = "0.11.0", default-features = false, optional = true } diff --git a/src/core.rs b/src/core.rs index bbce398..299c327 100644 --- a/src/core.rs +++ b/src/core.rs @@ -79,12 +79,12 @@ impl std::str::FromStr for Query { type Err = &'static str; fn from_str(s: &str) -> Result { - #[derive(miniserde::Deserialize)] + #[derive(serde::Deserialize)] struct Tag { tag: String, } - let tag = miniserde::json::from_str::(s).map_err(|_| "deserialize tag failed")?; + let tag = serde_json::from_str::(s).map_err(|_| "deserialize tag failed")?; if tag.tag == channel::TAG { let cr = s.parse().map_err(|_| "deserialize data failed")?; diff --git a/src/core/channel/client.rs b/src/core/channel/client.rs index eec8bd2..91518b4 100644 --- a/src/core/channel/client.rs +++ b/src/core/channel/client.rs @@ -1,20 +1,20 @@ #[derive(Clone, Debug)] pub struct Query { callback: url::Url, - pub uri: Box, - k1: Box, + pub uri: String, + k1: String, } impl std::str::FromStr for Query { type Err = &'static str; fn from_str(s: &str) -> Result { - let d: de::Query = miniserde::json::from_str(s).map_err(|_| "deserialize failed")?; + let d: de::Query = serde_json::from_str(s).map_err(|_| "deserialize failed")?; Ok(Query { - callback: d.callback.0.into_owned(), - uri: d.uri.into_boxed_str(), - k1: d.k1.into_boxed_str(), + callback: d.callback, + uri: d.uri, + k1: d.k1, }) } } @@ -86,7 +86,7 @@ impl std::fmt::Display for CallbackRequest<'_> { #[derive(Clone, Debug)] pub enum CallbackResponse { - Error { reason: Box }, + Error { reason: String }, Ok, } @@ -94,13 +94,13 @@ impl std::str::FromStr for CallbackResponse { type Err = &'static str; fn from_str(s: &str) -> Result { - let map = miniserde::json::from_str::>(s) + let map = serde_json::from_str::>(s) .map_err(|_| "bad json")?; match map.get("status").map(|s| s as &str) { Some("OK") => Ok(CallbackResponse::Ok), Some("ERROR") => { - let reason = (map.get("reason").ok_or("error without reason")? as &str).into(); + let reason = String::from(map.get("reason").ok_or("error without reason")?); Ok(CallbackResponse::Error { reason }) } _ => Err("bad status field"), @@ -109,12 +109,12 @@ impl std::str::FromStr for CallbackResponse { } mod de { - use crate::serde::Url; - use miniserde::Deserialize; + use serde::Deserialize; + use url::Url; #[derive(Deserialize)] pub(super) struct Query { - pub callback: Url<'static>, + pub callback: Url, pub uri: String, pub k1: String, } @@ -133,8 +133,8 @@ mod tests { let parsed = input.parse::().expect("parse"); assert_eq!(parsed.callback.as_str(), "https://yuri/?o=callback"); - assert_eq!(&parsed.uri as &str, "noh@ipe:porta"); - assert_eq!(&parsed.k1 as &str, "caum"); + assert_eq!(parsed.uri, "noh@ipe:porta"); + assert_eq!(parsed.k1, "caum"); } #[test] @@ -187,7 +187,7 @@ mod tests { assert!(matches!( r#"{ "status": "ERROR", "reason": "razao" }"#.parse().unwrap(), - super::CallbackResponse::Error { reason } if &reason as &str == "razao" + super::CallbackResponse::Error { reason } if reason == "razao" )); } } diff --git a/src/core/channel/server.rs b/src/core/channel/server.rs index 5c8052b..3f8a302 100644 --- a/src/core/channel/server.rs +++ b/src/core/channel/server.rs @@ -7,25 +7,27 @@ pub struct Query { impl std::fmt::Display for Query { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str(&miniserde::json::to_string(&ser::Query { + let ser = serde_json::to_string(&ser::Query { tag: super::TAG, - callback: crate::serde::Url(std::borrow::Cow::Borrowed(&self.callback)), + callback: &self.callback, uri: &self.uri, k1: &self.k1, - })) + }); + + f.write_str(&ser.map_err(|_| std::fmt::Error)?) } } #[derive(Clone, Debug)] pub enum CallbackRequest { Accept { - remoteid: Box, - k1: Box, + remoteid: String, + k1: String, private: bool, }, Cancel { - remoteid: Box, - k1: Box, + remoteid: String, + k1: String, }, } @@ -43,19 +45,19 @@ impl std::str::FromStr for CallbackRequest { if qs.get("cancel").copied() == Some("1") { Some(CallbackRequest::Cancel { - remoteid: (*remoteid).into(), - k1: (*k1).into(), + remoteid: String::from(*remoteid), + k1: String::from(*k1), }) } else { match qs.get("private").copied() { Some("0") => Some(CallbackRequest::Accept { - remoteid: (*remoteid).into(), - k1: (*k1).into(), + remoteid: String::from(*remoteid), + k1: String::from(*k1), private: false, }), Some("1") => Some(CallbackRequest::Accept { - remoteid: (*remoteid).into(), - k1: (*k1).into(), + remoteid: String::from(*remoteid), + k1: String::from(*k1), private: true, }), _ => None, @@ -85,18 +87,18 @@ impl std::fmt::Display for CallbackResponse { } } - f.write_str(&miniserde::json::to_string(&map)) + f.write_str(&serde_json::to_string(&map).map_err(|_| std::fmt::Error)?) } } mod ser { - use crate::serde::Url; - use miniserde::Serialize; + use serde::Serialize; + use url::Url; #[derive(Serialize)] pub(super) struct Query<'a> { pub tag: &'static str, - pub callback: Url<'a>, + pub callback: &'a Url, pub uri: &'a str, pub k1: &'a str, } @@ -130,8 +132,8 @@ mod tests { panic!("wrong parsed"); }; - assert_eq!(&remoteid as &str, "idremoto"); - assert_eq!(&k1 as &str, "caum"); + assert_eq!(remoteid, "idremoto"); + assert_eq!(k1, "caum"); assert!(private); let input = "remoteid=idremoto&k1=caum&private=0"; @@ -146,8 +148,8 @@ mod tests { panic!("wrong parsed"); }; - assert_eq!(&remoteid as &str, "idremoto"); - assert_eq!(&k1 as &str, "caum"); + assert_eq!(remoteid, "idremoto"); + assert_eq!(k1, "caum"); assert!(!private); let input = "remoteid=idremoto&k1=caum&private=2"; @@ -164,8 +166,8 @@ mod tests { panic!("wrong parsed"); }; - assert_eq!(&remoteid as &str, "idremoto"); - assert_eq!(&k1 as &str, "caum"); + assert_eq!(remoteid, "idremoto"); + assert_eq!(k1, "caum"); let input = "remoteid=idremoto&k1=caum&cancel=0"; let parsed = input.parse::(); diff --git a/src/core/pay/client.rs b/src/core/pay/client.rs index 0ff48e0..caabd22 100644 --- a/src/core/pay/client.rs +++ b/src/core/pay/client.rs @@ -1,13 +1,13 @@ #[derive(Clone, Debug)] pub struct Query { callback: url::Url, - pub metadata_raw: Box, + pub metadata_raw: String, pub short_description: String, - pub long_description: Option>, - pub identifier: Option>, - pub email: Option>, - pub jpeg: Option>, - pub png: Option>, + pub long_description: Option, + pub identifier: Option, + pub email: Option, + pub jpeg: Option>, + pub png: Option>, pub comment_size: Option, pub min: u64, pub max: u64, @@ -18,11 +18,11 @@ impl std::str::FromStr for Query { fn from_str(s: &str) -> Result { use base64::{prelude::BASE64_STANDARD, Engine}; - use miniserde::json::Value; + use serde_json::Value; - let p: de::Query = miniserde::json::from_str(s).map_err(|_| "deserialize failed")?; + let p: de::Query = serde_json::from_str(s).map_err(|_| "deserialize failed")?; - let metadata = miniserde::json::from_str::>(&p.metadata) + let metadata = serde_json::from_str::>(&p.metadata) .map_err(|_| "deserialize metadata failed")?; let short_description = metadata @@ -40,8 +40,7 @@ impl std::str::FromStr for Query { .and_then(|v| match v { Value::String(s) => Some(String::from(s)), _ => None, - }) - .map(String::into_boxed_str); + }); let jpeg = metadata .iter() @@ -49,8 +48,7 @@ impl std::str::FromStr for Query { .and_then(|v| match v { Value::String(s) => BASE64_STANDARD.decode(s).ok(), _ => None, - }) - .map(Vec::into_boxed_slice); + }); let png = metadata .iter() @@ -58,8 +56,7 @@ impl std::str::FromStr for Query { .and_then(|v| match v { Value::String(s) => BASE64_STANDARD.decode(s).ok(), _ => None, - }) - .map(Vec::into_boxed_slice); + }); let identifier = metadata .iter() @@ -67,8 +64,7 @@ impl std::str::FromStr for Query { .and_then(|v| match v { Value::String(s) => Some(String::from(s)), _ => None, - }) - .map(String::into_boxed_str); + }); let email = metadata .iter() @@ -76,12 +72,11 @@ impl std::str::FromStr for Query { .and_then(|v| match v { Value::String(s) => Some(String::from(s)), _ => None, - }) - .map(String::into_boxed_str); + }); Ok(Query { - metadata_raw: p.metadata.into_boxed_str(), - callback: p.callback.0.into_owned(), + metadata_raw: p.metadata, + callback: p.callback, comment_size: p.comment_allowed, min: p.min_sendable, max: p.max_sendable, @@ -131,38 +126,37 @@ impl std::fmt::Display for CallbackRequest<'_> { #[derive(Clone, Debug)] pub struct CallbackResponse { - pub pr: Box, + pub pr: String, pub disposable: bool, pub success_action: Option, } #[derive(Clone, Debug)] pub enum SuccessAction { - Url(url::Url, Box), - Message(Box), + Url(url::Url, String), + Message(String), } impl std::str::FromStr for CallbackResponse { type Err = &'static str; fn from_str(s: &str) -> Result { - let a: de::CallbackResponse = - miniserde::json::from_str(s).map_err(|_| "deserialize failed")?; + let a: de::CallbackResponse = serde_json::from_str(s).map_err(|_| "deserialize failed")?; let success_action = a .success_action .and_then(|sa| match sa.get("tag")? as &str { - "message" => Some(SuccessAction::Message((sa.get("message")? as &str).into())), + "message" => Some(SuccessAction::Message(String::from(sa.get("message")?))), "url" => { let url = url::Url::parse(sa.get("url")?).ok()?; - let description = (sa.get("description")? as &str).into(); + let description = String::from(sa.get("description")?); Some(SuccessAction::Url(url, description)) } _ => None, }); Ok(Self { - pr: a.pr.into(), + pr: a.pr, disposable: a.disposable.unwrap_or(true), success_action, }) @@ -170,14 +164,14 @@ impl std::str::FromStr for CallbackResponse { } mod de { - use crate::serde::Url; - use miniserde::Deserialize; + use serde::Deserialize; use std::collections::BTreeMap; + use url::Url; #[derive(Deserialize)] pub(super) struct Query { pub metadata: String, - pub callback: Url<'static>, + pub callback: Url, #[serde(rename = "minSendable")] pub min_sendable: u64, #[serde(rename = "maxSendable")] @@ -211,7 +205,7 @@ mod tests { assert_eq!(parsed.callback.to_string(), "https://yuri/?o=callback"); assert_eq!(parsed.short_description, "boneco do steve magal"); assert_eq!( - &parsed.metadata_raw as &str, + parsed.metadata_raw, "[[\"text/plain\", \"boneco do steve magal\"],[\"text/crazy\", \"👋🇧🇴💾\"]]" ); assert_eq!(parsed.min, 314); @@ -250,7 +244,7 @@ mod tests { let parsed = input.parse::().expect("parse"); assert_eq!( - &parsed.long_description.unwrap() as &str, + parsed.long_description.unwrap(), "mochila a jato brutal incluida" ); } @@ -265,8 +259,8 @@ mod tests { }"#; let parsed = input.parse::().expect("parse"); - assert_eq!(&parsed.jpeg.unwrap() as &[u8], b"imagembrutal"); - assert_eq!(&parsed.png.unwrap() as &[u8], b"fotobrutal"); + assert_eq!(parsed.jpeg.unwrap(), b"imagembrutal"); + assert_eq!(parsed.png.unwrap(), b"fotobrutal"); } #[test] @@ -279,7 +273,7 @@ mod tests { }"#; let parsed = input.parse::().expect("parse"); - assert_eq!(&parsed.identifier.unwrap() as &str, "steve@magal.brutal"); + assert_eq!(parsed.identifier.unwrap(), "steve@magal.brutal"); } #[test] @@ -292,7 +286,7 @@ mod tests { }"#; let parsed = input.parse::().expect("parse"); - assert_eq!(&parsed.email.unwrap() as &str, "steve@magal.brutal"); + assert_eq!(parsed.email.unwrap(), "steve@magal.brutal"); } #[test] @@ -335,7 +329,7 @@ mod tests { let parsed = input.parse::().expect("parse"); assert!(parsed.success_action.is_none()); - assert_eq!(&parsed.pr as &str, "pierre"); + assert_eq!(parsed.pr, "pierre"); assert!(parsed.disposable); } @@ -361,7 +355,7 @@ mod tests { panic!("bad success action"); }; - assert_eq!(&m as &str, "obrigado!"); + assert_eq!(m, "obrigado!"); let input = r#" { "pr": "", "successAction": { "tag": "url", "description": "valeu demais", "url": "http://eh.nois" } } @@ -374,6 +368,6 @@ mod tests { }; assert_eq!(u.to_string(), "http://eh.nois/"); - assert_eq!(&d as &str, "valeu demais"); + assert_eq!(d, "valeu demais"); } } diff --git a/src/core/pay/server.rs b/src/core/pay/server.rs index 9fca973..faa4af6 100644 --- a/src/core/pay/server.rs +++ b/src/core/pay/server.rs @@ -16,7 +16,7 @@ impl std::fmt::Display for Query { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { use base64::{prelude::BASE64_STANDARD, Engine}; - let metadata = miniserde::json::to_string( + let metadata = serde_json::to_string( &[ Some(("text/plain", self.short_description.clone())), self.long_description @@ -36,22 +36,25 @@ impl std::fmt::Display for Query { .into_iter() .flatten() .collect::>(), - ); + ) + .map_err(|_| std::fmt::Error)?; - f.write_str(&miniserde::json::to_string(&ser::Query { + let ser = serde_json::to_string(&ser::Query { tag: super::TAG, metadata, - callback: crate::serde::Url(std::borrow::Cow::Borrowed(&self.callback)), + callback: &self.callback, min_sendable: self.min, max_sendable: self.max, comment_allowed: self.comment_size.unwrap_or(0), - })) + }); + + f.write_str(&ser.map_err(|_| std::fmt::Error)?) } } pub struct CallbackRequest { pub millisatoshis: u64, - pub comment: Option>, + pub comment: Option, } impl std::str::FromStr for CallbackRequest { @@ -69,7 +72,7 @@ impl std::str::FromStr for CallbackRequest { .parse() .map_err(|_| "invalid amount")?; - let comment = qs.get("comment").map(|c| (*c).into()); + let comment = qs.get("comment").map(|c| String::from(*c)); Ok(CallbackRequest { millisatoshis, @@ -117,20 +120,20 @@ impl std::fmt::Display for CallbackResponse { pr: &self.pr, }; - f.write_str(&miniserde::json::to_string(&cr)) + f.write_str(&serde_json::to_string(&cr).map_err(|_| std::fmt::Error)?) } } mod ser { - use crate::serde::Url; - use miniserde::Serialize; + use serde::Serialize; use std::collections::BTreeMap; + use url::Url; #[derive(Serialize)] pub(super) struct Query<'a> { pub tag: &'static str, pub metadata: String, - pub callback: Url<'a>, + pub callback: &'a Url, #[serde(rename = "minSendable")] pub min_sendable: u64, #[serde(rename = "maxSendable")] @@ -291,7 +294,7 @@ mod tests { let parsed = input.parse::().expect("parse"); assert_eq!(parsed.millisatoshis, 314); - assert_eq!(&parsed.comment.unwrap() as &str, "comentario"); + assert_eq!(parsed.comment.unwrap(), "comentario"); } #[test] diff --git a/src/core/withdraw/client.rs b/src/core/withdraw/client.rs index eb59067..44a040c 100644 --- a/src/core/withdraw/client.rs +++ b/src/core/withdraw/client.rs @@ -1,8 +1,8 @@ #[derive(Clone, Debug)] pub struct Query { - k1: Box, + k1: String, callback: url::Url, - pub description: Box, + pub description: String, pub min: u64, pub max: u64, } @@ -11,12 +11,12 @@ impl std::str::FromStr for Query { type Err = &'static str; fn from_str(s: &str) -> Result { - let d: de::Query = miniserde::json::from_str(s).map_err(|_| "deserialize failed")?; + let d: de::Query = serde_json::from_str(s).map_err(|_| "deserialize failed")?; Ok(Query { - k1: d.k1.into_boxed_str(), - callback: d.callback.0.into_owned(), - description: d.default_description.into_boxed_str(), + k1: d.k1, + callback: d.callback, + description: d.default_description, min: d.min_withdrawable, max: d.max_withdrawable, }) @@ -52,7 +52,7 @@ impl std::fmt::Display for CallbackRequest<'_> { #[derive(Clone, Debug)] pub enum CallbackResponse { - Error { reason: Box }, + Error { reason: String }, Ok, } @@ -60,13 +60,13 @@ impl std::str::FromStr for CallbackResponse { type Err = &'static str; fn from_str(s: &str) -> Result { - let map = miniserde::json::from_str::>(s) + let map = serde_json::from_str::>(s) .map_err(|_| "bad json")?; match map.get("status").map(|s| s as &str) { Some("OK") => Ok(CallbackResponse::Ok), Some("ERROR") => { - let reason = (map.get("reason").ok_or("error without reason")? as &str).into(); + let reason = String::from(map.get("reason").ok_or("error without reason")?); Ok(CallbackResponse::Error { reason }) } _ => Err("bad status field"), @@ -75,13 +75,13 @@ impl std::str::FromStr for CallbackResponse { } mod de { - use crate::serde::Url; - use miniserde::Deserialize; + use serde::Deserialize; + use url::Url; #[derive(Deserialize)] pub(super) struct Query { pub k1: String, - pub callback: Url<'static>, + pub callback: Url, #[serde(rename = "defaultDescription")] pub default_description: String, #[serde(rename = "minWithdrawable")] @@ -106,8 +106,8 @@ mod tests { let parsed = input.parse::().expect("parse"); assert_eq!(parsed.callback.to_string(), "https://yuri/?o=callback"); - assert_eq!(&parsed.description as &str, "verde com bolinhas"); - assert_eq!(&parsed.k1 as &str, "caum"); + assert_eq!(parsed.description, "verde com bolinhas"); + assert_eq!(parsed.k1, "caum"); assert_eq!(parsed.max, 315); assert_eq!(parsed.min, 314); } @@ -139,7 +139,7 @@ mod tests { assert!(matches!( r#"{ "status": "ERROR", "reason": "razao" }"#.parse().unwrap(), - super::CallbackResponse::Error { reason } if &reason as &str == "razao" + super::CallbackResponse::Error { reason } if reason == "razao" )); } } diff --git a/src/core/withdraw/server.rs b/src/core/withdraw/server.rs index 68fadf5..00a0583 100644 --- a/src/core/withdraw/server.rs +++ b/src/core/withdraw/server.rs @@ -8,20 +8,21 @@ pub struct Query { impl std::fmt::Display for Query { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str(&miniserde::json::to_string(&ser::Query { + let ser = serde_json::to_string(&ser::Query { tag: super::TAG, - callback: crate::serde::Url(std::borrow::Cow::Borrowed(&self.callback)), + callback: &self.callback, default_description: &self.description, min_withdrawable: self.min, max_withdrawable: self.max, k1: &self.k1, - })) + }); + f.write_str(&ser.map_err(|_| std::fmt::Error)?) } } pub struct CallbackRequest { - pub k1: Box, - pub pr: Box, + pub k1: String, + pub pr: String, } impl std::str::FromStr for CallbackRequest { @@ -33,8 +34,8 @@ impl std::str::FromStr for CallbackRequest { .filter_map(|s| s.split_once('=')) .collect::>(); - let k1 = (*qs.get("k1").ok_or("missing k1")?).into(); - let pr = (*qs.get("pr").ok_or("missing pr")?).into(); + let k1 = String::from(*qs.get("k1").ok_or("missing k1")?); + let pr = String::from(*qs.get("pr").ok_or("missing pr")?); Ok(CallbackRequest { k1, pr }) } @@ -60,19 +61,20 @@ impl std::fmt::Display for CallbackResponse { } } - f.write_str(&miniserde::json::to_string(&map)) + let ser = serde_json::to_string(&map).map_err(|_| std::fmt::Error)?; + f.write_str(&ser) } } mod ser { - use crate::serde::Url; - use miniserde::Serialize; + use serde::Serialize; + use url::Url; #[derive(Serialize)] pub(super) struct Query<'a> { pub tag: &'static str, pub k1: &'a str, - pub callback: Url<'a>, + pub callback: &'a Url, #[serde(rename = "defaultDescription")] pub default_description: &'a str, #[serde(rename = "minWithdrawable")] @@ -105,8 +107,8 @@ mod tests { let input = "k1=caum&pr=pierre"; let parsed = input.parse::().expect("parse"); - assert_eq!(&parsed.pr as &str, "pierre"); - assert_eq!(&parsed.k1 as &str, "caum"); + assert_eq!(parsed.pr, "pierre"); + assert_eq!(parsed.k1, "caum"); } #[test] diff --git a/src/lib.rs b/src/lib.rs index ff0ebf0..afbb7b4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,6 @@ #![cfg_attr(all(doc, docsrs), feature(doc_auto_cfg))] mod core; -mod serde; #[cfg(feature = "server")] pub mod server; diff --git a/src/serde.rs b/src/serde.rs deleted file mode 100644 index 41c5f7e..0000000 --- a/src/serde.rs +++ /dev/null @@ -1,31 +0,0 @@ -use miniserde::{ - de::Visitor, - make_place, - ser::{Fragment, Serialize}, - Deserialize, Error, Result, -}; - -make_place!(Place); - -#[derive(Debug, Clone)] -pub(crate) struct Url<'a>(pub(crate) std::borrow::Cow<'a, url::Url>); - -impl Visitor for Place> { - fn string(&mut self, s: &str) -> Result<()> { - let url = url::Url::parse(s).map_err(|_| Error)?; - self.out = Some(Url(std::borrow::Cow::Owned(url))); - Ok(()) - } -} - -impl Deserialize for Url<'_> { - fn begin(out: &mut Option) -> &mut dyn Visitor { - Place::new(out) - } -} - -impl Serialize for Url<'_> { - fn begin(&self) -> Fragment { - Fragment::Str(self.0.to_string().into()) - } -}