Coverage Report

Created: 2024-02-20 21:15

/builds/xfbs/cindy/common/src/api/get.rs
Line
Count
Source (jump to first uncovered line)
1
use crate::{
2
    api::query::TagQuery,
3
    cache::*,
4
    tag::{TagNameInfo, TagValueInfo},
5
    BoxHash, Hash, Tag, TagPredicate,
6
};
7
use bytes::Bytes;
8
use restless::{data::Json, methods::Get, query::Qs, GetRequest, RequestMethod};
9
use serde::{Deserialize, Serialize};
10
use std::{
11
    borrow::{Borrow, Cow},
12
    collections::{BTreeMap, BTreeSet},
13
    path::Path,
14
};
15
16
0
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
17
pub struct QueryState {
18
    #[serde(default)]
19
    pub query: Vec<TagPredicate<'static>>,
20
    #[serde(default)]
21
    pub sort: Option<String>,
22
    #[serde(default)]
23
    pub group: Option<String>,
24
}
25
26
0
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
27
pub struct FileContent<H: Borrow<Hash> = BoxHash> {
28
    pub hash: H,
29
}
30
31
impl<H: Borrow<Hash>> GetRequest for FileContent<H> {
32
    type Response = Bytes;
33
    type Query = ();
34
35
3
    fn query(&self) -> Self::Query {}
36
37
3
    fn path(&self) -> Cow<'_, str> {
38
3
        format!("api/v1/file/{}", self.hash.borrow()).into()
39
3
    }
40
}
41
42
impl<H: Borrow<Hash>> RequestMethod for FileContent<H> {
43
    type Method = Get<Self>;
44
}
45
46
impl<H: Borrow<Hash>> Invalidatable for FileContent<H> {}
47
48
0
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
49
pub struct FileTags<H: Borrow<Hash> = BoxHash, S: Borrow<str> = String> {
50
    pub hash: H,
51
    pub name: Option<S>,
52
    pub value: Option<S>,
53
}
54
55
impl<H: Borrow<Hash>, S: Borrow<str>> GetRequest for FileTags<H, S> {
56
    type Response = Json<Vec<Tag>>;
57
    type Query = Qs<TagQuery<String>>;
58
59
7
    fn path(&self) -> Cow<'_, str> {
60
7
        format!("api/v1/file/{}/tags", self.hash.borrow(),).into()
61
7
    }
62
63
7
    fn query(&self) -> Self::Query {
64
7
        TagQuery {
65
7
            name: self.name.as_ref().map(Borrow::borrow).map(Into::into),
66
7
            value: self.value.as_ref().map(Borrow::borrow).map(Into::into),
67
7
        }
68
7
        .into()
69
7
    }
70
}
71
72
impl<H: Borrow<Hash>, S: Borrow<str>> Invalidatable for FileTags<H, S> {}
73
74
impl<H: Borrow<Hash>, S: Borrow<str>> RequestMethod for FileTags<H, S> {
75
    type Method = Get<Self>;
76
}
77
78
4
#[derive(Serialize, 
D3
eserialize,
Debug0
, Clone,
PartialEq0
, Eq,
PartialOrd0
,
Ord0
,
Hash0
)]
79
pub struct QueryFiles<'a> {
80
    #[serde(default)]
81
    pub query: Cow<'a, [TagPredicate<'a>]>,
82
}
83
84
impl<'a> GetRequest for QueryFiles<'a> {
85
    type Response = Json<Vec<BoxHash>>;
86
    type Query = Qs<Self>;
87
88
4
    fn path(&self) -> Cow<'_, str> {
89
4
        "api/v1/query".into()
90
4
    }
91
92
4
    fn query(&self) -> Self::Query {
93
4
        self.clone().into()
94
4
    }
95
}
96
97
impl<'a> RequestMethod for QueryFiles<'a> {
98
    type Method = Get<Self>;
99
}
100
101
0
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
102
pub struct TagNames;
103
104
impl GetRequest for TagNames {
105
    type Response = Json<BTreeMap<String, TagNameInfo>>;
106
    type Query = ();
107
108
3
    fn path(&self) -> Cow<'_, str> {
109
3
        "api/v1/tags".into()
110
3
    }
111
112
3
    fn query(&self) -> Self::Query {}
113
}
114
115
impl RequestMethod for TagNames {
116
    type Method = Get<Self>;
117
}
118
119
0
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
120
pub struct TagList<N: Borrow<str> = String, V: Borrow<str> = String> {
121
    pub name: Option<N>,
122
    pub value: Option<V>,
123
}
124
125
impl<N, V> GetRequest for TagList<N, V>
126
where
127
    N: Borrow<str>,
128
    V: Borrow<str>,
129
{
130
    type Response = Json<BTreeMap<Tag, TagValueInfo>>;
131
    type Query = Qs<TagQuery<String>>;
132
133
8
    fn path(&self) -> Cow<'_, str> {
134
8
        "api/v1/tags/values".into()
135
8
    }
136
137
8
    fn query(&self) -> Self::Query {
138
8
        TagQuery {
139
8
            name: self.name.as_ref().map(Borrow::borrow).map(Into::into),
140
8
            value: self.value.as_ref().map(Borrow::borrow).map(Into::into),
141
8
        }
142
8
        .into()
143
8
    }
144
}
145
146
impl<N: Borrow<str>, V: Borrow<str>> RequestMethod for TagList<N, V> {
147
    type Method = Get<Self>;
148
}
149
150
0
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
151
pub struct FrontendFile<P: Borrow<Path>> {
152
    pub path: P,
153
}
154
155
impl<P: Borrow<Path>> GetRequest for FrontendFile<P> {
156
    type Response = Bytes;
157
    type Query = ();
158
159
5
    fn path(&self) -> Cow<'_, str> {
160
5
        self.path.borrow().display().to_string().into()
161
5
    }
162
163
5
    fn query(&self) -> Self::Query {}
164
}
165
166
impl<P: Borrow<Path>> RequestMethod for FrontendFile<P> {
167
    type Method = Get<Self>;
168
}
169
170
0
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
171
#[serde(rename_all = "snake_case")]
172
pub enum QueryTagsMode {
173
    Union,
174
    Intersection,
175
}
176
177
0
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
178
pub struct QueryTags {
179
    #[serde(default)]
180
    pub name: Option<String>,
181
    #[serde(default)]
182
    pub value: Option<String>,
183
    #[serde(default)]
184
    pub query: Vec<TagPredicate<'static>>,
185
    pub mode: QueryTagsMode,
186
}
187
188
impl GetRequest for QueryTags {
189
    type Response = Json<BTreeSet<Tag>>;
190
    type Query = Qs<Self>;
191
192
0
    fn path(&self) -> Cow<'_, str> {
193
0
        "api/v1/query/tags".into()
194
0
    }
195
196
0
    fn query(&self) -> Self::Query {
197
0
        self.clone().into()
198
0
    }
199
}
200
201
impl RequestMethod for QueryTags {
202
    type Method = Get<Self>;
203
}