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
use crate::api::request::{ensure_success, ResponseError};
use crate::api::url::UrlBuilder;
use crate::client::Client;
use crate::file::remote_file::RemoteFile;
pub struct Exists<'a> {
file: &'a RemoteFile,
}
impl<'a> Exists<'a> {
pub fn new(file: &'a RemoteFile) -> Self {
Self { file }
}
pub fn invoke(self, client: &Client) -> Result<ExistsResponse, Error> {
self.check_exists(&client)
}
fn check_exists(&self, client: &Client) -> Result<ExistsResponse, Error> {
let exists_url = UrlBuilder::api_exists(self.file);
let mut response = client.get(exists_url).send().map_err(|_| Error::Request)?;
match ensure_success(&response) {
Ok(_) => {}
Err(ResponseError::Expired) => return Ok(ExistsResponse::new(false, false)),
Err(err) => return Err(Error::Response(err)),
}
let mut response = response
.json::<ExistsResponse>()
.map_err(|_| Error::Malformed)?;
response.set_exists(true);
Ok(response)
}
}
#[derive(Debug, Deserialize)]
pub struct ExistsResponse {
#[serde(skip)]
exists: bool,
#[serde(rename = "requiresPassword", alias = "password")]
requires_password: bool,
}
impl ExistsResponse {
pub fn new(exists: bool, requires_password: bool) -> Self {
ExistsResponse {
exists,
requires_password,
}
}
pub fn exists(&self) -> bool {
self.exists
}
pub fn set_exists(&mut self, exists: bool) {
self.exists = exists;
}
#[deprecated(since = "0.2", note = "please use `requires_password` instead")]
pub fn has_password(&self) -> bool {
self.requires_password()
}
pub fn requires_password(&self) -> bool {
self.requires_password
}
}
impl Default for ExistsResponse {
fn default() -> Self {
ExistsResponse {
exists: false,
requires_password: false,
}
}
}
#[derive(Fail, Debug)]
pub enum Error {
#[fail(display = "failed to send request whether the file exists")]
Request,
#[fail(display = "bad response from server while checking file existence")]
Response(#[cause] ResponseError),
#[fail(display = "received malformed authentication nonce")]
Malformed,
}