Skip to content

Commit

Permalink
chore: rename, reorganize, simplify, evolve
Browse files Browse the repository at this point in the history
  • Loading branch information
lsunsi committed Dec 5, 2023
1 parent aaa85fe commit 8210ec4
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 201 deletions.
58 changes: 26 additions & 32 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::core;

#[derive(Clone, Default)]
pub struct Client(reqwest::Client);

Expand All @@ -8,60 +6,56 @@ impl Client {
///
/// Returns errors on network or deserialization failures.
pub async fn query(&self, s: &str) -> Result<Query, &'static str> {
let url = core::resolve(s)?;
let url = crate::core::resolve(s)?;

let client = &self.0;
let response = client.get(url).send().await.map_err(|_| "request failed")?;
let text = response.text().await.map_err(|_| "body failed")?;

text.parse::<core::Query>()
text.parse::<crate::core::Query>()
.map_err(|_| "parse failed")
.map(|query| match query {
core::Query::ChannelRequest(core) => {
Query::ChannelRequest(ChannelRequest { client, core })
}
core::Query::WithdrawRequest(core) => {
Query::WithdrawRequest(WithdrawRequest { client, core })
}
core::Query::PayRequest(core) => Query::PayRequest(PayRequest { client, core }),
crate::core::Query::Channel(core) => Query::Channel(Channel { client, core }),
crate::core::Query::Pay(core) => Query::Pay(Pay { client, core }),
crate::core::Query::Withdraw(core) => Query::Withdraw(Withdraw { client, core }),
})
}
}

#[derive(Clone, Debug)]
pub enum Query<'a> {
ChannelRequest(ChannelRequest<'a>),
WithdrawRequest(WithdrawRequest<'a>),
PayRequest(PayRequest<'a>),
Channel(Channel<'a>),
Pay(Pay<'a>),
Withdraw(Withdraw<'a>),
}

#[derive(Clone, Debug)]
pub struct ChannelRequest<'a> {
pub struct Channel<'a> {
client: &'a reqwest::Client,
pub core: core::channel_request::ChannelRequest,
pub core: crate::core::channel::Query,
}

#[derive(Clone, Debug)]
pub struct WithdrawRequest<'a> {
pub struct Pay<'a> {
client: &'a reqwest::Client,
pub core: core::withdraw_request::WithdrawRequest,
pub core: crate::core::pay::Query,
}

#[derive(Clone, Debug)]
pub struct PayRequest<'a> {
pub struct Withdraw<'a> {
client: &'a reqwest::Client,
pub core: core::pay_request::PayRequest,
pub core: crate::core::withdraw::Query,
}

impl ChannelRequest<'_> {
impl Channel<'_> {
/// # Errors
///
/// Returns errors on network or deserialization failures.
pub async fn callback_accept(
self,
remoteid: &str,
private: bool,
) -> Result<core::channel_request::CallbackResponse, &'static str> {
) -> Result<crate::core::channel::CallbackResponse, &'static str> {
let callback = self.core.callback_accept(remoteid, private);

let response = self
Expand All @@ -81,7 +75,7 @@ impl ChannelRequest<'_> {
pub async fn callback_cancel(
self,
remoteid: &str,
) -> Result<core::channel_request::CallbackResponse, &'static str> {
) -> Result<crate::core::channel::CallbackResponse, &'static str> {
let callback = self.core.callback_cancel(remoteid);

let response = self
Expand All @@ -96,15 +90,16 @@ impl ChannelRequest<'_> {
}
}

impl WithdrawRequest<'_> {
impl Pay<'_> {
/// # Errors
///
/// Returns errors on network or deserialization failures.
pub async fn callback(
self,
pr: &str,
) -> Result<core::withdraw_request::CallbackResponse, &'static str> {
let callback = self.core.callback(pr);
comment: &str,
millisatoshis: u64,
) -> Result<crate::core::pay::CallbackResponse, &'static str> {
let callback = self.core.callback(comment, millisatoshis);

let response = self
.client
Expand All @@ -118,16 +113,15 @@ impl WithdrawRequest<'_> {
}
}

impl PayRequest<'_> {
impl Withdraw<'_> {
/// # Errors
///
/// Returns errors on network or deserialization failures.
pub async fn callback(
self,
comment: &str,
millisatoshis: u64,
) -> Result<core::pay_request::CallbackResponse, &'static str> {
let callback = self.core.callback(comment, millisatoshis);
pr: &str,
) -> Result<crate::core::withdraw::CallbackResponse, &'static str> {
let callback = self.core.callback(pr);

let response = self
.client
Expand Down
26 changes: 13 additions & 13 deletions src/core.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod channel_request;
pub mod pay_request;
pub mod withdraw_request;
pub mod channel;
pub mod pay;
pub mod withdraw;

/// # Errors
///
Expand Down Expand Up @@ -70,9 +70,9 @@ fn resolve_address(s: &str) -> Result<url::Url, &'static str> {

#[derive(Debug)]
pub enum Query {
PayRequest(pay_request::PayRequest),
ChannelRequest(channel_request::ChannelRequest),
WithdrawRequest(withdraw_request::WithdrawRequest),
Channel(channel::Query),
Pay(pay::Query),
Withdraw(withdraw::Query),
}

impl std::str::FromStr for Query {
Expand All @@ -86,15 +86,15 @@ impl std::str::FromStr for Query {

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

if tag.tag == channel_request::TAG {
if tag.tag == channel::TAG {
let cr = s.parse().map_err(|_| "deserialize data failed")?;
Ok(Query::ChannelRequest(cr))
} else if tag.tag == withdraw_request::TAG {
let wr = s.parse().map_err(|_| "deserialize data failed")?;
Ok(Query::WithdrawRequest(wr))
} else if tag.tag == pay_request::TAG {
Ok(Query::Channel(cr))
} else if tag.tag == pay::TAG {
let pr = s.parse().map_err(|_| "deserialize data failed")?;
Ok(Query::PayRequest(pr))
Ok(Query::Pay(pr))
} else if tag.tag == withdraw::TAG {
let wr = s.parse().map_err(|_| "deserialize data failed")?;
Ok(Query::Withdraw(wr))
} else {
Err("unknown tag")
}
Expand Down
25 changes: 12 additions & 13 deletions src/core/channel_request.rs → src/core/channel.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
pub const TAG: &str = "channelRequest";

#[derive(Clone, Debug)]
pub struct ChannelRequest {
pub struct Query {
pub callback: url::Url,
pub uri: String,
pub k1: String,
}

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

fn from_str(s: &str) -> Result<Self, Self::Err> {
let d: de::QueryResponse =
miniserde::json::from_str(s).map_err(|_| "deserialize failed")?;
let d: de::Query = miniserde::json::from_str(s).map_err(|_| "deserialize failed")?;

Ok(ChannelRequest {
Ok(Query {
callback: d.callback.0,
uri: d.uri,
k1: d.k1,
})
}
}

impl std::fmt::Display for ChannelRequest {
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::QueryResponse {
f.write_str(&miniserde::json::to_string(&ser::Query {
tag: TAG,
callback: crate::serde::Url(self.callback.clone()),
uri: &self.uri,
Expand All @@ -33,7 +32,7 @@ impl std::fmt::Display for ChannelRequest {
}
}

impl ChannelRequest {
impl Query {
/// # Errors
///
/// Returns errors on network or deserialization failures.
Expand Down Expand Up @@ -115,7 +114,7 @@ mod ser {
use miniserde::Serialize;

#[derive(Serialize)]
pub(super) struct QueryResponse<'a> {
pub(super) struct Query<'a> {
pub tag: &'static str,
pub callback: Url,
pub uri: &'a str,
Expand All @@ -128,7 +127,7 @@ mod de {
use miniserde::Deserialize;

#[derive(Deserialize)]
pub(super) struct QueryResponse {
pub(super) struct Query {
pub callback: Url,
pub uri: String,
pub k1: String,
Expand All @@ -147,7 +146,7 @@ mod tests {
}
"#;

let parsed = input.parse::<super::ChannelRequest>().expect("parse");
let parsed = input.parse::<super::Query>().expect("parse");

assert_eq!(parsed.callback.to_string(), "https://yuri/?o=callback");
assert_eq!(parsed.uri, "noh@ipe:porta");
Expand All @@ -164,7 +163,7 @@ mod tests {
}
"#;

let parsed = input.parse::<super::ChannelRequest>().expect("parse");
let parsed = input.parse::<super::Query>().expect("parse");
let url = parsed.clone().callback_accept("idremoto", true);

assert_eq!(
Expand All @@ -190,7 +189,7 @@ mod tests {
}
"#;

let parsed = input.parse::<super::ChannelRequest>().expect("parse");
let parsed = input.parse::<super::Query>().expect("parse");
let url = parsed.callback_cancel("idremoto");

assert_eq!(
Expand Down
29 changes: 14 additions & 15 deletions src/core/pay_request.rs → src/core/pay.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub const TAG: &str = "payRequest";

#[derive(Clone, Debug)]
pub struct PayRequest {
pub struct Query {
pub callback: url::Url,
pub short_description: String,
pub long_description: Option<String>,
Expand All @@ -12,15 +12,14 @@ pub struct PayRequest {
pub max: u64,
}

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

fn from_str(s: &str) -> Result<Self, Self::Err> {
use base64::{prelude::BASE64_STANDARD, Engine};
use miniserde::json::Value;

let p: de::QueryResponse =
miniserde::json::from_str(s).map_err(|_| "deserialize failed")?;
let p: de::Query = miniserde::json::from_str(s).map_err(|_| "deserialize failed")?;

let comment_size = p.comment_allowed.unwrap_or(0);

Expand Down Expand Up @@ -60,7 +59,7 @@ impl std::str::FromStr for PayRequest {
_ => None,
});

Ok(PayRequest {
Ok(Query {
callback: p.callback.0,
min: p.min_sendable,
max: p.max_sendable,
Expand All @@ -73,7 +72,7 @@ impl std::str::FromStr for PayRequest {
}
}

impl std::fmt::Display for PayRequest {
impl std::fmt::Display for Query {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use base64::{prelude::BASE64_STANDARD, Engine};

Expand All @@ -95,7 +94,7 @@ impl std::fmt::Display for PayRequest {
.collect::<Vec<_>>(),
);

f.write_str(&miniserde::json::to_string(&ser::QueryResponse {
f.write_str(&miniserde::json::to_string(&ser::Query {
tag: TAG,
metadata,
callback: &crate::serde::Url(self.callback.clone()),
Expand All @@ -106,7 +105,7 @@ impl std::fmt::Display for PayRequest {
}
}

impl PayRequest {
impl Query {
/// # Errors
///
/// Returns errors on network or deserialization failures.
Expand Down Expand Up @@ -200,7 +199,7 @@ mod ser {
use std::collections::BTreeMap;

#[derive(Serialize)]
pub(super) struct QueryResponse<'a> {
pub(super) struct Query<'a> {
pub tag: &'static str,
pub metadata: String,
pub callback: &'a Url,
Expand All @@ -227,7 +226,7 @@ mod de {
use std::collections::BTreeMap;

#[derive(Deserialize)]
pub(super) struct QueryResponse {
pub(super) struct Query {
pub metadata: String,
pub callback: Url,
#[serde(rename = "minSendable")]
Expand Down Expand Up @@ -260,7 +259,7 @@ mod tests {
}
"#;

let parsed = input.parse::<super::PayRequest>().expect("parse");
let parsed = input.parse::<super::Query>().expect("parse");

assert_eq!(parsed.callback.to_string(), "https://yuri/?o=callback");
assert_eq!(parsed.short_description, "boneco do steve magal");
Expand All @@ -285,7 +284,7 @@ mod tests {
}
"#;

let parsed = input.parse::<super::PayRequest>().expect("parse");
let parsed = input.parse::<super::Query>().expect("parse");
assert_eq!(parsed.comment_size, 140);
}

Expand All @@ -300,7 +299,7 @@ mod tests {
}
"#;

let parsed = input.parse::<super::PayRequest>().expect("parse");
let parsed = input.parse::<super::Query>().expect("parse");
assert_eq!(
parsed.long_description.unwrap(),
"mochila a jato brutal incluida"
Expand All @@ -318,7 +317,7 @@ mod tests {
}
"#;

let parsed = input.parse::<super::PayRequest>().expect("parse");
let parsed = input.parse::<super::Query>().expect("parse");
assert_eq!(parsed.jpeg.unwrap(), b"imagembrutal");
assert_eq!(parsed.png.unwrap(), b"fotobrutal");
}
Expand All @@ -334,7 +333,7 @@ mod tests {
}
"#;

let parsed = input.parse::<super::PayRequest>().expect("parse");
let parsed = input.parse::<super::Query>().expect("parse");

assert_eq!(
parsed.clone().callback("comentario", 314).to_string(),
Expand Down
Loading

0 comments on commit 8210ec4

Please sign in to comment.