ipfs_api_prelude/request/
object.rs

1// Copyright 2017 rust-ipfs-api Developers
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7//
8
9use crate::request::ApiRequest;
10use serde::{Serialize, Serializer};
11
12#[derive(Serialize)]
13pub struct ObjectData<'a> {
14    #[serde(rename = "arg")]
15    pub key: &'a str,
16}
17
18impl<'a> ApiRequest for ObjectData<'a> {
19    const PATH: &'static str = "/object/data";
20}
21
22#[derive(Serialize)]
23pub struct ObjectDiff<'a> {
24    #[serde(rename = "arg")]
25    pub key0: &'a str,
26
27    #[serde(rename = "arg")]
28    pub key1: &'a str,
29}
30
31impl<'a> ApiRequest for ObjectDiff<'a> {
32    const PATH: &'static str = "/object/diff";
33}
34
35#[derive(Serialize)]
36pub struct ObjectGet<'a> {
37    #[serde(rename = "arg")]
38    pub key: &'a str,
39}
40
41impl<'a> ApiRequest for ObjectGet<'a> {
42    const PATH: &'static str = "/object/get";
43}
44
45#[derive(Serialize)]
46pub struct ObjectLinks<'a> {
47    #[serde(rename = "arg")]
48    pub key: &'a str,
49}
50
51impl<'a> ApiRequest for ObjectLinks<'a> {
52    const PATH: &'static str = "/object/links";
53}
54
55#[derive(Copy, Clone)]
56pub enum ObjectTemplate {
57    UnixFsDir,
58}
59
60impl Serialize for ObjectTemplate {
61    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
62    where
63        S: Serializer,
64    {
65        let s = match self {
66            ObjectTemplate::UnixFsDir => "unixfs-dir",
67        };
68
69        serializer.serialize_str(s)
70    }
71}
72
73#[derive(Serialize)]
74pub struct ObjectNew {
75    #[serde(rename = "arg")]
76    pub template: Option<ObjectTemplate>,
77}
78
79impl ApiRequest for ObjectNew {
80    const PATH: &'static str = "/object/new";
81}
82
83#[derive(Serialize)]
84pub struct ObjectPatchAddLink<'a> {
85    #[serde(rename = "arg")]
86    pub folder: &'a str,
87
88    #[serde(rename = "arg")]
89    pub name: &'a str,
90
91    #[serde(rename = "arg")]
92    pub key: &'a str,
93
94    #[serde(rename = "create")]
95    pub create: bool,
96}
97
98impl<'a> ApiRequest for ObjectPatchAddLink<'a> {
99    const PATH: &'static str = "/object/patch/add-link";
100}
101
102#[derive(Serialize)]
103pub struct ObjectStat<'a> {
104    #[serde(rename = "arg")]
105    pub key: &'a str,
106}
107
108impl<'a> ApiRequest for ObjectStat<'a> {
109    const PATH: &'static str = "/object/stat";
110}
111
112#[cfg(test)]
113mod tests {
114    use super::ObjectDiff;
115
116    serialize_url_test!(
117        test_serializes_0,
118        ObjectDiff {
119            key0: "test",
120            key1: "test2",
121        },
122        "arg=test&arg=test2"
123    );
124}