ipfs_api_prelude/request/
dag.rs1use 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 #[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 #[cfg_attr(feature = "with-builder", builder(default, setter(strip_option)))]
42 pub store_codec: Option<DagCodec>,
43 #[cfg_attr(feature = "with-builder", builder(default, setter(strip_option)))]
45 pub input_codec: Option<DagCodec>,
46 #[cfg_attr(feature = "with-builder", builder(default, setter(strip_option)))]
48 pub pin: Option<bool>,
49 #[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}