Skip to content

Commit

Permalink
refactor: cave in and replace miniserde by serde
Browse files Browse the repository at this point in the history
  • Loading branch information
lsunsi committed Dec 10, 2023
1 parent 1063847 commit 682fe4d
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 155 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 2 additions & 2 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ impl std::str::FromStr for Query {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
#[derive(miniserde::Deserialize)]
#[derive(serde::Deserialize)]
struct Tag {
tag: String,
}

let tag = miniserde::json::from_str::<Tag>(s).map_err(|_| "deserialize tag failed")?;
let tag = serde_json::from_str::<Tag>(s).map_err(|_| "deserialize tag failed")?;

if tag.tag == channel::TAG {
let cr = s.parse().map_err(|_| "deserialize data failed")?;
Expand Down
30 changes: 15 additions & 15 deletions src/core/channel/client.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#[derive(Clone, Debug)]
pub struct Query {
callback: url::Url,
pub uri: Box<str>,
k1: Box<str>,
pub uri: String,
k1: String,
}

impl std::str::FromStr for Query {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
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,
})
}
}
Expand Down Expand Up @@ -86,21 +86,21 @@ impl std::fmt::Display for CallbackRequest<'_> {

#[derive(Clone, Debug)]
pub enum CallbackResponse {
Error { reason: Box<str> },
Error { reason: String },
Ok,
}

impl std::str::FromStr for CallbackResponse {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let map = miniserde::json::from_str::<std::collections::BTreeMap<String, String>>(s)
let map = serde_json::from_str::<std::collections::BTreeMap<String, String>>(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"),
Expand All @@ -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,
}
Expand All @@ -133,8 +133,8 @@ mod tests {
let parsed = input.parse::<super::Query>().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]
Expand Down Expand Up @@ -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"
));
}
}
48 changes: 25 additions & 23 deletions src/core/channel/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<str>,
k1: Box<str>,
remoteid: String,
k1: String,
private: bool,
},
Cancel {
remoteid: Box<str>,
k1: Box<str>,
remoteid: String,
k1: String,
},
}

Expand All @@ -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,
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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";
Expand All @@ -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";
Expand All @@ -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::<super::CallbackRequest>();
Expand Down
Loading

0 comments on commit 682fe4d

Please sign in to comment.