1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use std::fmt::{self, Debug};

use bytes::Bytes;
use cid::Cid;
use derive_more::{From, TryInto};
use quic_rpc::{
    message::{Msg, RpcMsg, ServerStreaming},
    Service,
};
use serde::{Deserialize, Serialize};

use crate::{RpcResult, VersionRequest, VersionResponse, WatchRequest, WatchResponse};

pub type StoreAddr = super::addr::Addr<StoreService>;

#[derive(Serialize, Deserialize)]
pub struct PutRequest {
    pub cid: Cid,
    pub blob: Bytes,
    pub links: Vec<Cid>,
}

impl fmt::Debug for PutRequest {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PutRequest")
            .field("cid", &self.cid)
            .field("blob", &self.blob.len())
            .field("links", &self.links)
            .finish()
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct PutManyRequest {
    pub blocks: Vec<PutRequest>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct GetRequest {
    pub cid: Cid,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct GetResponse {
    pub data: Option<Bytes>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct HasRequest {
    pub cid: Cid,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct HasResponse {
    pub has: bool,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct GetLinksRequest {
    pub cid: Cid,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct GetLinksResponse {
    pub links: Option<Vec<Cid>>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct GetSizeRequest {
    pub cid: Cid,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct GetSizeResponse {
    pub size: Option<u64>,
}

#[derive(Serialize, Deserialize, Debug, From, TryInto)]
pub enum StoreRequest {
    Watch(WatchRequest),
    Version(VersionRequest),
    Put(PutRequest),
    PutMany(PutManyRequest),
    Get(GetRequest),
    Has(HasRequest),
    GetLinks(GetLinksRequest),
    GetSize(GetSizeRequest),
}

#[derive(Serialize, Deserialize, Debug, From, TryInto)]
pub enum StoreResponse {
    Watch(WatchResponse),
    Version(VersionResponse),
    Get(RpcResult<GetResponse>),
    Has(RpcResult<HasResponse>),
    GetLinks(RpcResult<GetLinksResponse>),
    GetSize(RpcResult<GetSizeResponse>),
    Unit(()),
    UnitResult(RpcResult<()>),
}

#[derive(Debug, Clone, Copy)]
pub struct StoreService;

impl Service for StoreService {
    type Req = StoreRequest;

    type Res = StoreResponse;
}

impl Msg<StoreService> for WatchRequest {
    type Response = WatchResponse;

    type Update = Self;

    type Pattern = ServerStreaming;
}

impl RpcMsg<StoreService> for VersionRequest {
    type Response = VersionResponse;
}

impl RpcMsg<StoreService> for GetRequest {
    type Response = RpcResult<GetResponse>;
}

impl RpcMsg<StoreService> for PutRequest {
    type Response = RpcResult<()>;
}

impl RpcMsg<StoreService> for HasRequest {
    type Response = RpcResult<HasResponse>;
}

impl RpcMsg<StoreService> for PutManyRequest {
    type Response = RpcResult<()>;
}

impl RpcMsg<StoreService> for GetLinksRequest {
    type Response = RpcResult<GetLinksResponse>;
}

impl RpcMsg<StoreService> for GetSizeRequest {
    type Response = RpcResult<GetSizeResponse>;
}