Skip to content

Commit

Permalink
ls_with_options
Browse files Browse the repository at this point in the history
  • Loading branch information
jcaesar committed Oct 24, 2020
1 parent 38e8ad7 commit eabde51
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
38 changes: 36 additions & 2 deletions ipfs-api/src/client/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1942,13 +1942,47 @@ impl IpfsClient {
/// use ipfs_api::IpfsClient;
///
/// let client = IpfsClient::default();
/// let res = client.ls(None);
/// let res = client.ls(Some("/ipfs/QmVrLsEDn27sScp3k23sgZNefVTjSAL3wpgW1iWPi4MgoY"));
/// ```
///
#[inline]
pub async fn ls(&self, path: Option<&str>) -> Result<response::LsResponse, Error> {
self.request(request::Ls { path }, None).await
self.request(request::Ls {
path: path.expect("Path is not actually optional"),
stream: None,
resolve_type: None,
size: None,
}, None).await
}

/// List the contents of an Ipfs multihash.
///
/// ```no_run
/// use ipfs_api::IpfsClient;
///
/// let client = IpfsClient::default();
/// #[cfg(feature = "builder")]
/// let _ = client.ls_with_options(ipfs_api::request::Ls::builder()
/// .path("/ipfs/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n")
/// .build()
/// );
/// let _ = client.ls_with_options(ipfs_api::request::Ls {
/// path: "/ipfs/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n",
/// // Example options for fast listing
/// stream: Some(true),
/// resolve_type: Some(false),
/// size: Some(false),
/// });
/// ```
///
#[inline]
pub async fn ls_with_options(
&self,
options: request::Ls<'_>
) -> impl Stream<Item = Result<response::LsResponse, Error>> {
impl_stream_api_response! {
(self, options, None) => request_stream_json
}
}

// TODO /mount
Expand Down
19 changes: 15 additions & 4 deletions ipfs-api/src/request/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,21 @@
use crate::request::ApiRequest;
use crate::serde::Serialize;

#[derive(Serialize)]
#[cfg_attr(feature = "builder", derive(TypedBuilder))]
#[derive(Serialize, Default)]
#[serde(rename_all = "kebab-case")]
pub struct Ls<'a> {
#[serde(rename = "arg")]
pub path: Option<&'a str>,
pub path: &'a str,
/// Resolve linked objects to find out their types. Default: `true`
#[cfg_attr(feature = "builder", builder(default, setter(strip_option)))]
pub resolve_type: Option<bool>,
/// Resolve linked objects to find out their file size. Default: `true`
#[cfg_attr(feature = "builder", builder(default, setter(strip_option)))]
pub size: Option<bool>,
/// Enable experimental streaming of directory entries as they are traversed.
#[cfg_attr(feature = "builder", builder(default, setter(strip_option)))]
pub stream: Option<bool>,
}

impl<'a> ApiRequest for Ls<'a> {
Expand All @@ -23,6 +34,6 @@ impl<'a> ApiRequest for Ls<'a> {
mod tests {
use super::Ls;

serialize_url_test!(test_serializes_0, Ls { path: Some("test") }, "arg=test");
serialize_url_test!(test_serializes_1, Ls { path: None }, "");
serialize_url_test!(test_serializes_0, Ls { path: "test", .. Default::default() }, "arg=test");
serialize_url_test!(test_serializes_1, Ls { path: "asdf", resolve_type: Some(true), size: Some(true), stream: Some(false) }, "arg=asdf&resolve-type=true&size=true&stream=false");
}

0 comments on commit eabde51

Please sign in to comment.