ipfs_api_prelude/response/
dht.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::response::serde;
10use crate::serde::{
11    de::{Deserializer, Error},
12    Deserialize,
13};
14
15/// See
16/// [libp2p](https://github.com/libp2p/go-libp2p-routing/blob/master/notifications/query.go#L16).
17///
18#[derive(Debug)]
19pub enum DhtType {
20    SendingQuery,
21    PeerResponse,
22    FinalPeer,
23    QueryError,
24    Provider,
25    Value,
26    AddingPeer,
27    DialingPeer,
28}
29
30impl<'de> Deserialize<'de> for DhtType {
31    #[inline]
32    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
33    where
34        D: Deserializer<'de>,
35    {
36        match deserializer.deserialize_i64(serde::IntegerVisitor)? {
37            0 => Ok(DhtType::SendingQuery),
38            1 => Ok(DhtType::PeerResponse),
39            2 => Ok(DhtType::FinalPeer),
40            3 => Ok(DhtType::QueryError),
41            4 => Ok(DhtType::Provider),
42            5 => Ok(DhtType::Value),
43            6 => Ok(DhtType::AddingPeer),
44            7 => Ok(DhtType::DialingPeer),
45            i => Err(D::Error::custom(format!("unknown dht type '{}'", i))),
46        }
47    }
48}
49
50#[derive(Debug, Deserialize)]
51#[serde(rename_all = "PascalCase")]
52pub struct DhtPeerResponse {
53    #[serde(rename = "ID")]
54    pub id: String,
55
56    #[serde(deserialize_with = "serde::deserialize_vec")]
57    pub addrs: Vec<String>,
58}
59
60#[derive(Debug, Deserialize)]
61#[serde(rename_all = "PascalCase")]
62pub struct DhtMessage {
63    #[serde(rename = "ID")]
64    pub id: String,
65
66    #[serde(rename = "Type")]
67    pub typ: DhtType,
68
69    #[serde(deserialize_with = "serde::deserialize_vec")]
70    pub responses: Vec<DhtPeerResponse>,
71
72    pub extra: String,
73}
74
75pub type DhtFindPeerResponse = DhtMessage;
76
77pub type DhtFindProvsResponse = DhtMessage;
78
79pub type DhtGetResponse = DhtMessage;
80
81pub type DhtProvideResponse = DhtMessage;
82
83pub type DhtPutResponse = DhtMessage;
84
85pub type DhtQueryResponse = DhtMessage;