ipfs_api_prelude/request/
dag.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;
11
12#[derive(Debug, Serialize)]
13pub enum DagCodec {
14    #[serde(rename = "dag-json")]
15    Json,
16    #[serde(rename = "dag-cbor")]
17    Cbor,
18}
19
20#[cfg_attr(feature = "with-builder", derive(TypedBuilder))]
21#[derive(Serialize, Default)]
22pub struct DagGet<'a> {
23    #[serde(rename = "arg")]
24    pub path: &'a str,
25
26    /// Format that the object will be encoded as. Default: dag-json. Required: no.
27    #[serde(rename = "output-codec")]
28    #[cfg_attr(feature = "with-builder", builder(default, setter(strip_option)))]
29    pub codec: Option<DagCodec>,
30}
31
32impl<'a> ApiRequest for DagGet<'a> {
33    const PATH: &'static str = "/dag/get";
34}
35
36#[cfg_attr(feature = "with-builder", derive(TypedBuilder))]
37#[derive(Serialize, Default)]
38#[serde(rename_all = "kebab-case")]
39pub struct DagPut<'a> {
40    /// Codec that the stored object will be encoded with. Default: dag-cbor. Required: no.
41    #[cfg_attr(feature = "with-builder", builder(default, setter(strip_option)))]
42    pub store_codec: Option<DagCodec>,
43    /// Codec that the input object is encoded in. Default: dag-json. Required: no.
44    #[cfg_attr(feature = "with-builder", builder(default, setter(strip_option)))]
45    pub input_codec: Option<DagCodec>,
46    /// Pin this object when adding. Required: no.
47    #[cfg_attr(feature = "with-builder", builder(default, setter(strip_option)))]
48    pub pin: Option<bool>,
49    /// Hash function to use. Default: sha2-256. Required: no.
50    #[cfg_attr(feature = "with-builder", builder(default, setter(strip_option)))]
51    pub hash: Option<&'a str>,
52}
53
54impl ApiRequest for DagPut<'_> {
55    const PATH: &'static str = "/dag/put";
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    serialize_url_test!(
63        test_serializes_dag_get,
64        DagGet {
65            path: "bafkreiglbo2l5lp25vteuexq3svg5hoad76mehz4tlrbwheslvluxcd63a",
66            ..Default::default()
67        },
68        "arg=bafkreiglbo2l5lp25vteuexq3svg5hoad76mehz4tlrbwheslvluxcd63a"
69    );
70
71    serialize_url_test!(
72        test_serializes_dag_get_with_options,
73        DagGet {
74            path: "bafkreiglbo2l5lp25vteuexq3svg5hoad76mehz4tlrbwheslvluxcd63a",
75            codec: Some(DagCodec::Cbor),
76        },
77        "arg=bafkreiglbo2l5lp25vteuexq3svg5hoad76mehz4tlrbwheslvluxcd63a&output-codec=dag-cbor"
78    );
79
80    serialize_url_test!(
81        test_serializes_dag_put,
82        DagPut {
83            ..Default::default()
84        },
85        ""
86    );
87
88    serialize_url_test!(
89        test_serializes_dag_put_with_options,
90        DagPut {
91            store_codec: Some(DagCodec::Json),
92            input_codec: Some(DagCodec::Cbor),
93            pin: Some(false),
94            hash: Some("sha3_384"),
95        },
96        "store-codec=dag-json&input-codec=dag-cbor&pin=false&hash=sha3_384"
97    );
98}