Skip to content

Commit

Permalink
refactor: rename response to entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lsunsi committed Dec 13, 2023
1 parent 66c1e96 commit 87a64ad
Show file tree
Hide file tree
Showing 20 changed files with 238 additions and 252 deletions.
38 changes: 20 additions & 18 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ impl Client {
/// # Errors
///
/// Returns errors on network or deserialization failures.
pub async fn query(&self, s: &str) -> Result<Response, &'static str> {
pub async fn entrypoint(&self, s: &str) -> Result<Entrypoint, &'static str> {
let client = &self.0;

let url = match crate::resolve(s)? {
crate::Resolved::Url(url) => url,
crate::Resolved::Withdraw(_, core) => {
return Ok(Response::Withdraw(Withdraw { client, core }))
return Ok(Entrypoint::Withdraw(Withdraw { client, core }))
}
};

Expand All @@ -21,16 +21,18 @@ impl Client {
(&bytes as &[u8])
.try_into()
.map_err(|_| "parse failed")
.map(|query: crate::Response| match query {
crate::Response::Channel(core) => Response::Channel(Channel { client, core }),
crate::Response::Pay(core) => Response::Pay(Pay { client, core }),
crate::Response::Withdraw(core) => Response::Withdraw(Withdraw { client, core }),
.map(|query: crate::Entrypoint| match query {
crate::Entrypoint::Channel(core) => Entrypoint::Channel(Channel { client, core }),
crate::Entrypoint::Pay(core) => Entrypoint::Pay(Pay { client, core }),
crate::Entrypoint::Withdraw(core) => {
Entrypoint::Withdraw(Withdraw { client, core })
}
})
}
}

#[derive(Clone, Debug)]
pub enum Response<'a> {
pub enum Entrypoint<'a> {
Channel(Channel<'a>),
Pay(Pay<'a>),
Withdraw(Withdraw<'a>),
Expand All @@ -39,31 +41,31 @@ pub enum Response<'a> {
#[derive(Clone, Debug)]
pub struct Channel<'a> {
client: &'a reqwest::Client,
pub core: crate::channel::client::Response,
pub core: crate::channel::client::Entrypoint,
}

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

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

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

let response = self
.client
Expand All @@ -79,11 +81,11 @@ impl Channel<'_> {
/// # Errors
///
/// Returns errors on network or deserialization failures.
pub async fn callback_cancel(
pub async fn cancel(
&self,
remoteid: &str,
) -> Result<crate::channel::client::CallbackResponse, &'static str> {
let callback = self.core.callback_cancel(remoteid);
let callback = self.core.cancel(remoteid);

let response = self
.client
Expand All @@ -101,12 +103,12 @@ impl Pay<'_> {
/// # Errors
///
/// Returns errors on network or deserialization failures.
pub async fn callback(
pub async fn invoice(
&self,
millisatoshis: u64,
comment: Option<&str>,
) -> Result<crate::pay::client::CallbackResponse, &'static str> {
let callback = self.core.callback(millisatoshis, comment);
let callback = self.core.invoice(millisatoshis, comment);

let response = self
.client
Expand All @@ -124,11 +126,11 @@ impl Withdraw<'_> {
/// # Errors
///
/// Returns errors on network or deserialization failures.
pub async fn callback(
pub async fn submit(
&self,
pr: &str,
) -> Result<crate::withdraw::client::CallbackResponse, &'static str> {
let callback = self.core.callback(pr);
let callback = self.core.submit(pr);

let response = self
.client
Expand Down
20 changes: 10 additions & 10 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub mod withdraw;

pub enum Resolved {
Url(url::Url),
Withdraw(url::Url, withdraw::client::Response),
Withdraw(url::Url, withdraw::client::Entrypoint),
}

/// # Errors
Expand All @@ -26,7 +26,7 @@ pub fn resolve(s: &str) -> Result<Resolved, &'static str> {
.find_map(|(k, v)| (k == "tag").then_some(v));

Ok(match tag.as_deref() {
Some(withdraw::TAG) => match url.as_str().parse::<withdraw::client::Response>() {
Some(withdraw::TAG) => match url.as_str().parse::<withdraw::client::Entrypoint>() {
Ok(w) => Resolved::Withdraw(url, w),
Err(_) => Resolved::Url(url),
},
Expand Down Expand Up @@ -86,13 +86,13 @@ fn resolve_address(s: &str) -> Result<url::Url, &'static str> {
}

#[derive(Debug)]
pub enum Response {
Channel(channel::client::Response),
Pay(pay::client::Response),
Withdraw(withdraw::client::Response),
pub enum Entrypoint {
Channel(channel::client::Entrypoint),
Pay(pay::client::Entrypoint),
Withdraw(withdraw::client::Entrypoint),
}

impl TryFrom<&[u8]> for Response {
impl TryFrom<&[u8]> for Entrypoint {
type Error = &'static str;

fn try_from(s: &[u8]) -> Result<Self, Self::Error> {
Expand All @@ -105,13 +105,13 @@ impl TryFrom<&[u8]> for Response {

if tag.tag == channel::TAG {
let cr = s.try_into().map_err(|_| "deserialize data failed")?;
Ok(Response::Channel(cr))
Ok(Entrypoint::Channel(cr))
} else if tag.tag == pay::TAG {
let pr = s.try_into().map_err(|_| "deserialize data failed")?;
Ok(Response::Pay(pr))
Ok(Entrypoint::Pay(pr))
} else if tag.tag == withdraw::TAG {
let wr = s.try_into().map_err(|_| "deserialize data failed")?;
Ok(Response::Withdraw(wr))
Ok(Entrypoint::Withdraw(wr))
} else {
Err("unknown tag")
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod serde {

#[derive(Deserialize, Serialize)]
#[serde(untagged)]
pub(super) enum CallbackQuery<'a> {
pub(super) enum Callback<'a> {
Accept {
k1: &'a str,
remoteid: &'a str,
Expand Down
50 changes: 25 additions & 25 deletions src/core/channel/client.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
#[derive(Clone, Debug)]
pub struct Response {
pub struct Entrypoint {
pub callback: url::Url,
pub uri: String,
pub k1: String,
}

impl TryFrom<&[u8]> for Response {
impl TryFrom<&[u8]> for Entrypoint {
type Error = &'static str;

fn try_from(s: &[u8]) -> Result<Self, Self::Error> {
let d: de::Response = serde_json::from_slice(s).map_err(|_| "deserialize failed")?;
let d: de::Entrypoint = serde_json::from_slice(s).map_err(|_| "deserialize failed")?;

Ok(Response {
Ok(Entrypoint {
callback: d.callback,
uri: d.uri,
k1: d.k1,
})
}
}

impl Response {
impl Entrypoint {
#[must_use]
pub fn callback_accept<'a>(&'a self, remoteid: &'a str, private: bool) -> CallbackQuery<'a> {
CallbackQuery::Accept {
pub fn accept<'a>(&'a self, remoteid: &'a str, private: bool) -> Callback<'a> {
Callback::Accept {
url: &self.callback,
k1: &self.k1,
remoteid,
Expand All @@ -31,8 +31,8 @@ impl Response {
}

#[must_use]
pub fn callback_cancel<'a>(&'a self, remoteid: &'a str) -> CallbackQuery<'a> {
CallbackQuery::Cancel {
pub fn cancel<'a>(&'a self, remoteid: &'a str) -> Callback<'a> {
Callback::Cancel {
url: &self.callback,
k1: &self.k1,
remoteid,
Expand All @@ -41,7 +41,7 @@ impl Response {
}

#[derive(Clone, Debug)]
pub enum CallbackQuery<'a> {
pub enum Callback<'a> {
Accept {
url: &'a url::Url,
k1: &'a str,
Expand All @@ -55,17 +55,17 @@ pub enum CallbackQuery<'a> {
},
}

impl std::fmt::Display for CallbackQuery<'_> {
impl std::fmt::Display for Callback<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let (url, query) = match self {
CallbackQuery::Accept {
Callback::Accept {
url,
k1,
remoteid,
private,
} => (
url,
super::serde::CallbackQuery::Accept {
super::serde::Callback::Accept {
k1,
remoteid,
private: if *private {
Expand All @@ -75,9 +75,9 @@ impl std::fmt::Display for CallbackQuery<'_> {
},
},
),
CallbackQuery::Cancel { url, k1, remoteid } => (
Callback::Cancel { url, k1, remoteid } => (
url,
super::serde::CallbackQuery::Cancel {
super::serde::Callback::Cancel {
k1,
remoteid,
cancel: super::serde::OneOnly::One,
Expand Down Expand Up @@ -120,7 +120,7 @@ mod de {
use url::Url;

#[derive(Deserialize)]
pub(super) struct Response {
pub(super) struct Entrypoint {
pub callback: Url,
pub uri: String,
pub k1: String,
Expand All @@ -130,37 +130,37 @@ mod de {
#[cfg(test)]
mod tests {
#[test]
fn response_parse() {
fn entrypoint_parse() {
let input = r#"{
"callback": "https://yuri?o=callback",
"uri": "noh@ipe:porta",
"k1": "caum"
}"#;

let parsed: super::Response = input.as_bytes().try_into().expect("parse");
let parsed: super::Entrypoint = input.as_bytes().try_into().expect("parse");

assert_eq!(parsed.callback.as_str(), "https://yuri/?o=callback");
assert_eq!(parsed.uri, "noh@ipe:porta");
assert_eq!(parsed.k1, "caum");
}

#[test]
fn callback_query_accept_render() {
fn callback_accept_render() {
let input = r#"{
"callback": "https://yuri?o=callback",
"uri": "noh@ipe:porta",
"k1": "caum"
}"#;

let parsed: super::Response = input.as_bytes().try_into().expect("parse");
let url = parsed.callback_accept("idremoto", true);
let parsed: super::Entrypoint = input.as_bytes().try_into().expect("parse");
let url = parsed.accept("idremoto", true);

assert_eq!(
url.to_string(),
"https://yuri/?o=callback&k1=caum&remoteid=idremoto&private=1"
);

let url = parsed.callback_accept("idremoto", false);
let url = parsed.accept("idremoto", false);

assert_eq!(
url.to_string(),
Expand All @@ -169,15 +169,15 @@ mod tests {
}

#[test]
fn callback_query_cancel_render() {
fn callback_cancel_render() {
let input = r#"{
"callback": "https://yuri?o=callback",
"uri": "noh@ipe:porta",
"k1": "caum"
}"#;

let parsed: super::Response = input.as_bytes().try_into().expect("parse");
let url = parsed.callback_cancel("idremoto");
let parsed: super::Entrypoint = input.as_bytes().try_into().expect("parse");
let url = parsed.cancel("idremoto");

assert_eq!(
url.to_string(),
Expand Down
Loading

0 comments on commit 87a64ad

Please sign in to comment.