From a46d5bda1f823b3d016a0f66d0a6ed9f8d1a11d9 Mon Sep 17 00:00:00 2001 From: zu1k Date: Sat, 11 Jun 2022 21:12:19 +0800 Subject: [PATCH 1/9] feat: impl pin_remote_add api Signed-off-by: zu1k --- ipfs-api-prelude/src/api.rs | 35 ++++++++++++++++++++++++++++ ipfs-api-prelude/src/request/pin.rs | 14 +++++++++++ ipfs-api-prelude/src/response/pin.rs | 8 +++++++ 3 files changed, 57 insertions(+) diff --git a/ipfs-api-prelude/src/api.rs b/ipfs-api-prelude/src/api.rs index 964cd78..4ac6de8 100644 --- a/ipfs-api-prelude/src/api.rs +++ b/ipfs-api-prelude/src/api.rs @@ -2004,6 +2004,41 @@ pub trait IpfsApi: Backend { // TODO /pin/verify + /// Pin object to remote pinning service. + /// + /// "service": the Name of the remote pinning service to use (mandatory) + /// "name": an optional name for the pin + /// "background": add to the queue on the remote service and + /// return immediately (does not wait for pinned status) + /// + /// # Examples + /// + /// ```no_run + /// use ipfs_api::{IpfsApi, IpfsClient}; + /// + /// let client = IpfsClient::default(); + /// let res = client.pin_remote_add("QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", "pinata", Some("pin_task_name"), true); + /// ``` + /// + async fn pin_remote_add( + &self, + key: &str, + service: &str, + name: Option<&str>, + background: bool, + ) -> Result { + self.request( + request::PinRemoteAdd { + key, + service: Some(service), + name, + background: Some(background), + }, + None, + ) + .await + } + /// Pings a peer. /// /// ```no_run diff --git a/ipfs-api-prelude/src/request/pin.rs b/ipfs-api-prelude/src/request/pin.rs index 3e4815d..37af66e 100644 --- a/ipfs-api-prelude/src/request/pin.rs +++ b/ipfs-api-prelude/src/request/pin.rs @@ -46,3 +46,17 @@ pub struct PinRm<'a> { impl<'a> ApiRequest for PinRm<'a> { const PATH: &'static str = "/pin/rm"; } + +#[derive(Serialize)] +pub struct PinRemoteAdd<'a> { + #[serde(rename = "arg")] + pub key: &'a str, + + pub service: Option<&'a str>, + pub name: Option<&'a str>, + pub background: Option, +} + +impl<'a> ApiRequest for PinRemoteAdd<'a> { + const PATH: &'static str = "/pin/remote/add"; +} \ No newline at end of file diff --git a/ipfs-api-prelude/src/response/pin.rs b/ipfs-api-prelude/src/response/pin.rs index 3a303ba..882f552 100644 --- a/ipfs-api-prelude/src/response/pin.rs +++ b/ipfs-api-prelude/src/response/pin.rs @@ -40,6 +40,14 @@ pub struct PinRmResponse { pub pins: Vec, } +#[derive(Debug, Deserialize)] +#[serde(rename_all = "PascalCase")] +pub struct PinRemoteAddResponse { + pub cid: String, + pub name: String, + pub status: String, +} + #[cfg(test)] mod tests { deserialize_test!(v0_pin_ls_0, PinLsResponse); From 3c5c6b71ec7b71fdfea24730bbd1c85d19870ee7 Mon Sep 17 00:00:00 2001 From: zu1k Date: Sat, 11 Jun 2022 21:33:11 +0800 Subject: [PATCH 2/9] feat: impl pin_remote_ls api Signed-off-by: zu1k --- ipfs-api-prelude/src/api.rs | 46 ++++++++++++++++++++++++++-- ipfs-api-prelude/src/request/pin.rs | 29 +++++++++++++++++- ipfs-api-prelude/src/response/pin.rs | 8 +++++ 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/ipfs-api-prelude/src/api.rs b/ipfs-api-prelude/src/api.rs index 4ac6de8..6ea304b 100644 --- a/ipfs-api-prelude/src/api.rs +++ b/ipfs-api-prelude/src/api.rs @@ -10,7 +10,7 @@ use crate::{read::LineDecoder, request, response, Backend, BoxStream}; use async_trait::async_trait; use bytes::Bytes; use common_multipart_rfc7578::client::multipart; -use futures::{future, FutureExt, TryStreamExt, AsyncRead}; +use futures::{future, AsyncRead, FutureExt, TryStreamExt}; use std::{ fs::File, io::{Cursor, Read}, @@ -80,7 +80,8 @@ pub trait IpfsApi: Backend { where R: 'static + AsyncRead + Send + Sync + Unpin, { - self.add_async_with_options(data, request::Add::default()).await + self.add_async_with_options(data, request::Add::default()) + .await } /// Add a file to IPFS with options. @@ -2039,6 +2040,47 @@ pub trait IpfsApi: Backend { .await } + /// Returns a list objects pinned to remote pinning service. + /// + /// "service": the Name of the remote pinning service to use (mandatory) + /// "name": return pins with names that contain the value provided (case-sensitive, exact match). + /// "cid": return pins for the specified CIDs + /// "status": return pins with the specified statuses (queued,pinning,pinned,failed), default pinned. + /// + /// # Examples + /// + /// ```no_run + /// use ipfs_api::{IpfsApi, IpfsClient}; + /// + /// let client = IpfsClient::default(); + /// let res = client.pin_remote_ls("pinata", None, None, None); + /// let res = client.pin_remote_ls( + /// "pinata", + /// None, + /// None, + /// Some(&vec[PinStatus::Pinning, PinStatus::Pinned, PinStatus::Failed]), + /// ); + /// ``` + /// + async fn pin_remote_ls( + &self, + service: &str, + name: Option<&str>, + cid: Option<&[&str]>, + status: Option<&[request::PinStatus]>, + ) -> Result { + self.request( + request::PinRemoteLs { + service: Some(service), + name, + cid, + status, + }, + None, + ) + .await + } + /// Pings a peer. /// /// ```no_run diff --git a/ipfs-api-prelude/src/request/pin.rs b/ipfs-api-prelude/src/request/pin.rs index 37af66e..d5883ee 100644 --- a/ipfs-api-prelude/src/request/pin.rs +++ b/ipfs-api-prelude/src/request/pin.rs @@ -59,4 +59,31 @@ pub struct PinRemoteAdd<'a> { impl<'a> ApiRequest for PinRemoteAdd<'a> { const PATH: &'static str = "/pin/remote/add"; -} \ No newline at end of file +} + +#[derive(Serialize)] +pub struct PinRemoteLs<'a> { + pub service: Option<&'a str>, + pub name: Option<&'a str>, + pub cid: Option<&'a [&'a str]>, + pub status: Option<&'a [PinStatus]>, +} + +impl<'a> ApiRequest for PinRemoteLs<'a> { + const PATH: &'static str = "/pin/remote/ls"; +} + +#[derive(Serialize)] +#[serde(rename_all = "lowercase")] +pub enum PinStatus { + Queued, + Pinning, + Pinned, + Failed, +} + +impl Default for PinStatus { + fn default() -> Self { + Self::Pinned + } +} diff --git a/ipfs-api-prelude/src/response/pin.rs b/ipfs-api-prelude/src/response/pin.rs index 882f552..ad756b5 100644 --- a/ipfs-api-prelude/src/response/pin.rs +++ b/ipfs-api-prelude/src/response/pin.rs @@ -48,6 +48,14 @@ pub struct PinRemoteAddResponse { pub status: String, } +#[derive(Debug, Deserialize)] +#[serde(rename_all = "PascalCase")] +pub struct PinRemoteLsResponse { + pub cid: String, + pub name: String, + pub status: String, +} + #[cfg(test)] mod tests { deserialize_test!(v0_pin_ls_0, PinLsResponse); From b07dddedd9a05b0db16419ee29476c81427ac05f Mon Sep 17 00:00:00 2001 From: zu1k Date: Sat, 11 Jun 2022 21:36:30 +0800 Subject: [PATCH 3/9] chore: format docs Signed-off-by: zu1k --- ipfs-api-prelude/src/api.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ipfs-api-prelude/src/api.rs b/ipfs-api-prelude/src/api.rs index 6ea304b..421a0a9 100644 --- a/ipfs-api-prelude/src/api.rs +++ b/ipfs-api-prelude/src/api.rs @@ -2007,10 +2007,12 @@ pub trait IpfsApi: Backend { /// Pin object to remote pinning service. /// - /// "service": the Name of the remote pinning service to use (mandatory) - /// "name": an optional name for the pin + /// "service": the Name of the remote pinning service to use (mandatory). + /// + /// "name": an optional name for the pin. + /// /// "background": add to the queue on the remote service and - /// return immediately (does not wait for pinned status) + /// return immediately (does not wait for pinned status). /// /// # Examples /// @@ -2042,9 +2044,12 @@ pub trait IpfsApi: Backend { /// Returns a list objects pinned to remote pinning service. /// - /// "service": the Name of the remote pinning service to use (mandatory) + /// "service": the Name of the remote pinning service to use (mandatory). + /// /// "name": return pins with names that contain the value provided (case-sensitive, exact match). + /// /// "cid": return pins for the specified CIDs + /// /// "status": return pins with the specified statuses (queued,pinning,pinned,failed), default pinned. /// /// # Examples From f8a761a3be48b1a0981b0fc735f042d007336b0a Mon Sep 17 00:00:00 2001 From: zu1k Date: Sat, 11 Jun 2022 21:45:40 +0800 Subject: [PATCH 4/9] feat: impl pin_remote_rm api Signed-off-by: zu1k --- ipfs-api-prelude/src/api.rs | 50 +++++++++++++++++++++++++++- ipfs-api-prelude/src/request/pin.rs | 13 ++++++++ ipfs-api-prelude/src/response/pin.rs | 3 ++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/ipfs-api-prelude/src/api.rs b/ipfs-api-prelude/src/api.rs index 421a0a9..ef4866c 100644 --- a/ipfs-api-prelude/src/api.rs +++ b/ipfs-api-prelude/src/api.rs @@ -2063,7 +2063,7 @@ pub trait IpfsApi: Backend { /// "pinata", /// None, /// None, - /// Some(&vec[PinStatus::Pinning, PinStatus::Pinned, PinStatus::Failed]), + /// Some(&vec[PinStatus::Pinning, PinStatus::Pinned, PinStatus::Failed]) /// ); /// ``` /// @@ -2086,6 +2086,54 @@ pub trait IpfsApi: Backend { .await } + /// Remove pins from remote pinning service. + /// + /// "service": the Name of the remote pinning service to use (mandatory). + /// + /// "name": return pins with names that contain the value provided (case-sensitive, exact match). + /// + /// "cid": return pins for the specified CIDs + /// + /// "status": return pins with the specified statuses (queued,pinning,pinned,failed), default pinned. + /// + /// "force": allow removal of multiple pins matching the query without additional confirmation. + /// + /// # Examples + /// + /// ```no_run + /// use ipfs_api::{IpfsApi, IpfsClient}; + /// + /// let client = IpfsClient::default(); + /// let res = client.pin_remote_rm( + /// "pinata", + /// None, + /// None, + /// None, + /// true + /// ); + /// ``` + /// + async fn pin_remote_rm( + &self, + service: &str, + name: Option<&str>, + cid: Option<&[&str]>, + status: Option<&[request::PinStatus]>, + force: bool, + ) -> Result { + self.request( + request::PinRemoteRm { + service: Some(service), + name, + cid, + status, + force: Some(force), + }, + None, + ) + .await + } + /// Pings a peer. /// /// ```no_run diff --git a/ipfs-api-prelude/src/request/pin.rs b/ipfs-api-prelude/src/request/pin.rs index d5883ee..68fc020 100644 --- a/ipfs-api-prelude/src/request/pin.rs +++ b/ipfs-api-prelude/src/request/pin.rs @@ -73,6 +73,19 @@ impl<'a> ApiRequest for PinRemoteLs<'a> { const PATH: &'static str = "/pin/remote/ls"; } +#[derive(Serialize)] +pub struct PinRemoteRm<'a> { + pub service: Option<&'a str>, + pub name: Option<&'a str>, + pub cid: Option<&'a [&'a str]>, + pub status: Option<&'a [PinStatus]>, + pub force: Option, +} + +impl<'a> ApiRequest for PinRemoteRm<'a> { + const PATH: &'static str = "/pin/remote/rm"; +} + #[derive(Serialize)] #[serde(rename_all = "lowercase")] pub enum PinStatus { diff --git a/ipfs-api-prelude/src/response/pin.rs b/ipfs-api-prelude/src/response/pin.rs index ad756b5..489bf54 100644 --- a/ipfs-api-prelude/src/response/pin.rs +++ b/ipfs-api-prelude/src/response/pin.rs @@ -56,6 +56,9 @@ pub struct PinRemoteLsResponse { pub status: String, } +#[derive(Debug, Deserialize)] +pub struct PinRemoteRmResponse(String); + #[cfg(test)] mod tests { deserialize_test!(v0_pin_ls_0, PinLsResponse); From d8c00653df6bb77606297a422e3c10591b51227e Mon Sep 17 00:00:00 2001 From: zu1k Date: Sat, 11 Jun 2022 21:54:56 +0800 Subject: [PATCH 5/9] fix: pass test Signed-off-by: zu1k --- ipfs-api-prelude/src/api.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ipfs-api-prelude/src/api.rs b/ipfs-api-prelude/src/api.rs index ef4866c..483f3fb 100644 --- a/ipfs-api-prelude/src/api.rs +++ b/ipfs-api-prelude/src/api.rs @@ -2050,20 +2050,23 @@ pub trait IpfsApi: Backend { /// /// "cid": return pins for the specified CIDs /// - /// "status": return pins with the specified statuses (queued,pinning,pinned,failed), default pinned. + /// "status": return pins with the specified statuses (queued,pinning,pinned,failed), default [pinned]. /// /// # Examples /// /// ```no_run /// use ipfs_api::{IpfsApi, IpfsClient}; + /// use ipfs_api_prelude::request::PinStatus; /// /// let client = IpfsClient::default(); /// let res = client.pin_remote_ls("pinata", None, None, None); + /// + /// let status = vec![PinStatus::Pinning, PinStatus::Pinned]; /// let res = client.pin_remote_ls( /// "pinata", /// None, /// None, - /// Some(&vec[PinStatus::Pinning, PinStatus::Pinned, PinStatus::Failed]) + /// Some(&status) /// ); /// ``` /// From fb5bfbd3478c8e0bd8e36b1281768678ae5d8660 Mon Sep 17 00:00:00 2001 From: zu1k Date: Sun, 12 Jun 2022 10:42:34 +0800 Subject: [PATCH 6/9] docs: Update pin remote docs Signed-off-by: zu1k --- ipfs-api-prelude/src/api.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/ipfs-api-prelude/src/api.rs b/ipfs-api-prelude/src/api.rs index 483f3fb..f9d2c3f 100644 --- a/ipfs-api-prelude/src/api.rs +++ b/ipfs-api-prelude/src/api.rs @@ -2007,11 +2007,11 @@ pub trait IpfsApi: Backend { /// Pin object to remote pinning service. /// - /// "service": the Name of the remote pinning service to use (mandatory). + /// - `service`: Name of the remote pinning service to use (mandatory). /// - /// "name": an optional name for the pin. + /// - `name`: An optional name for the pin. /// - /// "background": add to the queue on the remote service and + /// - `background`: Add to the queue on the remote service and /// return immediately (does not wait for pinned status). /// /// # Examples @@ -2044,13 +2044,13 @@ pub trait IpfsApi: Backend { /// Returns a list objects pinned to remote pinning service. /// - /// "service": the Name of the remote pinning service to use (mandatory). + /// - `service`: Name of the remote pinning service to use (mandatory). /// - /// "name": return pins with names that contain the value provided (case-sensitive, exact match). + /// - `name`: Return pins with names that contain the value provided (case-sensitive, exact match). /// - /// "cid": return pins for the specified CIDs + /// - `cid`: Return pins for the specified CIDs /// - /// "status": return pins with the specified statuses (queued,pinning,pinned,failed), default [pinned]. + /// - `status`: Return pins with the specified statuses (queued,pinning,pinned,failed), default [pinned]. /// /// # Examples /// @@ -2091,15 +2091,15 @@ pub trait IpfsApi: Backend { /// Remove pins from remote pinning service. /// - /// "service": the Name of the remote pinning service to use (mandatory). + /// - `service`: Name of the remote pinning service to use (mandatory). /// - /// "name": return pins with names that contain the value provided (case-sensitive, exact match). + /// - `name`: Remove pins with names that contain provided value (case-sensitive, exact match). /// - /// "cid": return pins for the specified CIDs + /// - `cid`: Remove pins for the specified CIDs. /// - /// "status": return pins with the specified statuses (queued,pinning,pinned,failed), default pinned. + /// - `status`: Remove pins with the specified statuses (queued,pinning,pinned,failed) /// - /// "force": allow removal of multiple pins matching the query without additional confirmation. + /// - `force`: Allow removal of multiple pins matching the query without additional confirmation. /// /// # Examples /// From fb96bbd420d12faa9cb140884f21ea12e2c479ad Mon Sep 17 00:00:00 2001 From: zu1k Date: Sun, 12 Jun 2022 10:45:28 +0800 Subject: [PATCH 7/9] fix: Make PinRemoteRmResponse inner public Signed-off-by: zu1k --- ipfs-api-prelude/src/response/pin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ipfs-api-prelude/src/response/pin.rs b/ipfs-api-prelude/src/response/pin.rs index 489bf54..9ccd377 100644 --- a/ipfs-api-prelude/src/response/pin.rs +++ b/ipfs-api-prelude/src/response/pin.rs @@ -57,7 +57,7 @@ pub struct PinRemoteLsResponse { } #[derive(Debug, Deserialize)] -pub struct PinRemoteRmResponse(String); +pub struct PinRemoteRmResponse(pub String); #[cfg(test)] mod tests { From 4c721e9d012727902b376db86cab65eea5f09621 Mon Sep 17 00:00:00 2001 From: zu1k Date: Sun, 12 Jun 2022 14:14:55 +0800 Subject: [PATCH 8/9] PinStatus serialize with cunstom func Signed-off-by: zu1k --- ipfs-api-prelude/src/api.rs | 16 +++--- ipfs-api-prelude/src/request/pin.rs | 81 ++++++++++++++++++++++++++-- ipfs-api-prelude/src/response/pin.rs | 3 -- 3 files changed, 86 insertions(+), 14 deletions(-) diff --git a/ipfs-api-prelude/src/api.rs b/ipfs-api-prelude/src/api.rs index f9d2c3f..2332b8b 100644 --- a/ipfs-api-prelude/src/api.rs +++ b/ipfs-api-prelude/src/api.rs @@ -2076,17 +2076,17 @@ pub trait IpfsApi: Backend { name: Option<&str>, cid: Option<&[&str]>, status: Option<&[request::PinStatus]>, - ) -> Result { - self.request( + ) -> Result, Self::Error> { + let req = self.build_base_request( request::PinRemoteLs { service: Some(service), name, - cid, + cid: cid.map(|cid| cid.into()), status, }, None, - ) - .await + )?; + self.request_stream_json(req).try_collect().await } /// Remove pins from remote pinning service. @@ -2123,12 +2123,12 @@ pub trait IpfsApi: Backend { cid: Option<&[&str]>, status: Option<&[request::PinStatus]>, force: bool, - ) -> Result { - self.request( + ) -> Result { + self.request_string( request::PinRemoteRm { service: Some(service), name, - cid, + cid: cid.map(|cid| cid.into()), status, force: Some(force), }, diff --git a/ipfs-api-prelude/src/request/pin.rs b/ipfs-api-prelude/src/request/pin.rs index 68fc020..835daca 100644 --- a/ipfs-api-prelude/src/request/pin.rs +++ b/ipfs-api-prelude/src/request/pin.rs @@ -65,7 +65,11 @@ impl<'a> ApiRequest for PinRemoteAdd<'a> { pub struct PinRemoteLs<'a> { pub service: Option<&'a str>, pub name: Option<&'a str>, - pub cid: Option<&'a [&'a str]>, + pub cid: Option>, + #[serde( + skip_serializing_if = "Option::is_none", + serialize_with = "serialize_pin_status" + )] pub status: Option<&'a [PinStatus]>, } @@ -77,7 +81,11 @@ impl<'a> ApiRequest for PinRemoteLs<'a> { pub struct PinRemoteRm<'a> { pub service: Option<&'a str>, pub name: Option<&'a str>, - pub cid: Option<&'a [&'a str]>, + pub cid: Option>, + #[serde( + skip_serializing_if = "Option::is_none", + serialize_with = "serialize_pin_status" + )] pub status: Option<&'a [PinStatus]>, pub force: Option, } @@ -86,8 +94,25 @@ impl<'a> ApiRequest for PinRemoteRm<'a> { const PATH: &'static str = "/pin/remote/rm"; } +pub struct Cids<'a>(&'a [&'a str]); + +impl<'a> From<&'a [&'a str]> for Cids<'a> { + fn from(data: &'a [&'a str]) -> Self { + Self(data) + } +} + +impl<'a> Serialize for Cids<'a> { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + let cids = self.0.join(","); + serializer.serialize_str(&cids) + } +} + #[derive(Serialize)] -#[serde(rename_all = "lowercase")] pub enum PinStatus { Queued, Pinning, @@ -100,3 +125,53 @@ impl Default for PinStatus { Self::Pinned } } + +fn serialize_pin_status( + pin_status: &Option<&[PinStatus]>, + serializer: S, +) -> Result +where + S: serde::Serializer, +{ + let pin_status = pin_status.unwrap(); + let pin_status = pin_status + .iter() + .map(|item| match *item { + PinStatus::Failed => "failed", + PinStatus::Queued => "queued", + PinStatus::Pinning => "pinning", + PinStatus::Pinned => "pinned", + }) + .collect::>() + .join(","); + serializer.serialize_str(&format!("[{pin_status}]")) +} + +#[cfg(test)] +mod tests { + use super::*; + + serialize_url_test!( + test_serializes_pin_remote_rm, + PinRemoteRm { + service: Some("Pinata"), + name: None, + cid: Some((&vec!["bafybeiaq3hspbuvhvg7nlxjjvsnzit6m6hevrjwedoj4jbx6uycgkkexni"] as &[&str]).into()), + status: Some(&vec![PinStatus::Pinned, PinStatus::Pinning]), + force: Some(true) + }, + "service=Pinata&cid=bafybeiaq3hspbuvhvg7nlxjjvsnzit6m6hevrjwedoj4jbx6uycgkkexni&status=%5Bpinned%2Cpinning%5D&force=true" + ); + + serialize_url_test!( + test_serializes_pin_remote_rm_multi_cid, + PinRemoteRm { + service: Some("Pinata"), + name: None, + cid: Some((&vec!["bafybeiaq3hspbuvhvg7nlxjjvsnzit6m6hevrjwedoj4jbx6uycgkkexni", "QmfWC6JwVxmjVQfPpSiTsxFaSBdPTtFCd1B4aqMqRgaeMU"] as &[&str]).into()), + status:None, + force: None + }, + "service=Pinata&cid=bafybeiaq3hspbuvhvg7nlxjjvsnzit6m6hevrjwedoj4jbx6uycgkkexni%2CQmfWC6JwVxmjVQfPpSiTsxFaSBdPTtFCd1B4aqMqRgaeMU" + ); +} diff --git a/ipfs-api-prelude/src/response/pin.rs b/ipfs-api-prelude/src/response/pin.rs index 9ccd377..ad756b5 100644 --- a/ipfs-api-prelude/src/response/pin.rs +++ b/ipfs-api-prelude/src/response/pin.rs @@ -56,9 +56,6 @@ pub struct PinRemoteLsResponse { pub status: String, } -#[derive(Debug, Deserialize)] -pub struct PinRemoteRmResponse(pub String); - #[cfg(test)] mod tests { deserialize_test!(v0_pin_ls_0, PinLsResponse); From 59f1a033d220d8ab6677d3f6027b54584a112b51 Mon Sep 17 00:00:00 2001 From: zu1k Date: Sun, 31 Jul 2022 08:17:29 +0800 Subject: [PATCH 9/9] chore: impl Display for PinStatus Signed-off-by: zu1k --- ipfs-api-prelude/src/api.rs | 11 ++++++++--- ipfs-api-prelude/src/request/pin.rs | 21 +++++++++++---------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/ipfs-api-prelude/src/api.rs b/ipfs-api-prelude/src/api.rs index 2332b8b..6fd2ae8 100644 --- a/ipfs-api-prelude/src/api.rs +++ b/ipfs-api-prelude/src/api.rs @@ -2017,10 +2017,15 @@ pub trait IpfsApi: Backend { /// # Examples /// /// ```no_run - /// use ipfs_api::{IpfsApi, IpfsClient}; + /// use ipfs-api-backend-hyper::{IpfsApi, IpfsClient}; /// /// let client = IpfsClient::default(); - /// let res = client.pin_remote_add("QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", "pinata", Some("pin_task_name"), true); + /// let res = client.pin_remote_add( + /// "QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", + /// "pinata", + /// Some("pin_task_name"), + /// true + /// ); /// ``` /// async fn pin_remote_add( @@ -2055,7 +2060,7 @@ pub trait IpfsApi: Backend { /// # Examples /// /// ```no_run - /// use ipfs_api::{IpfsApi, IpfsClient}; + /// use ipfs-api-backend-hyper::{IpfsApi, IpfsClient}; /// use ipfs_api_prelude::request::PinStatus; /// /// let client = IpfsClient::default(); diff --git a/ipfs-api-prelude/src/request/pin.rs b/ipfs-api-prelude/src/request/pin.rs index 835daca..69347db 100644 --- a/ipfs-api-prelude/src/request/pin.rs +++ b/ipfs-api-prelude/src/request/pin.rs @@ -120,9 +120,15 @@ pub enum PinStatus { Failed, } -impl Default for PinStatus { - fn default() -> Self { - Self::Pinned +impl std::fmt::Display for PinStatus { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let pin_status = match self { + PinStatus::Failed => "failed", + PinStatus::Queued => "queued", + PinStatus::Pinning => "pinning", + PinStatus::Pinned => "pinned", + }; + write!(f, "{pin_status}") } } @@ -136,13 +142,8 @@ where let pin_status = pin_status.unwrap(); let pin_status = pin_status .iter() - .map(|item| match *item { - PinStatus::Failed => "failed", - PinStatus::Queued => "queued", - PinStatus::Pinning => "pinning", - PinStatus::Pinned => "pinned", - }) - .collect::>() + .map(|item| item.to_string()) + .collect::>() .join(","); serializer.serialize_str(&format!("[{pin_status}]")) }