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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use chrono::{DateTime, FixedOffset};
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;
use url::Url;

use crate::spec::Object;

/// https://github.com/git-lfs/git-lfs/blob/master/docs/api/batch.md#requests
#[derive(PartialEq, Eq, Debug, Deserialize)]
pub struct BatchRequest {
    pub operation: Operation,
    #[serde(default = "Transfer::default_vec")]
    pub transfer: Vec<Transfer>,
    #[serde(rename = "ref")]
    pub ref_property: Option<Ref>,
    pub objects: Vec<Object>,
}

/// https://github.com/git-lfs/git-lfs/blob/master/docs/api/batch.md#successful-responses
#[derive(PartialEq, Eq, Debug, Serialize)]
pub struct BatchResponse {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub transfer: Option<Transfer>,
    pub objects: Vec<ObjectResponse>,
}

/// https://github.com/git-lfs/git-lfs/blob/master/docs/api/batch.md#requests
#[derive(PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Operation {
    Download,
    Upload,
}

/// https://github.com/git-lfs/git-lfs/blob/master/docs/api/basic-transfers.md#basic-transfer-api
#[derive(PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Transfer {
    Basic,
    Custom,
}

impl Transfer {
    fn default_vec() -> Vec<Self> {
        vec![Transfer::Basic]
    }
}

impl Default for Transfer {
    fn default() -> Self {
        Transfer::Basic
    }
}

/// https://github.com/git-lfs/git-lfs/blob/master/docs/api/batch.md#ref-property
#[derive(PartialEq, Eq, Debug, Deserialize)]
pub struct Ref {
    pub name: String,
}

/// https://github.com/git-lfs/git-lfs/blob/master/docs/api/batch.md#successful-responses
#[derive(PartialEq, Eq, Debug, Serialize)]
#[serde(untagged)]
pub enum ObjectResponse {
    Success {
        #[serde(flatten)]
        object: Object,
        #[serde(skip_serializing_if = "Option::is_none")]
        authenticated: Option<bool>,
        actions: Actions,
    },
    Error {
        #[serde(flatten)]
        object: Object,
        error: ObjectError,
    },
}

impl ObjectResponse {
    pub fn success(object: Object, actions: Actions) -> Self {
        ObjectResponse::Success {
            object,
            authenticated: None,
            actions,
        }
    }

    pub fn error(object: Object, error: ObjectError) -> Self {
        ObjectResponse::Error { object, error }
    }
}

/// https://github.com/git-lfs/git-lfs/blob/master/docs/api/batch.md#successful-responses
#[derive(PartialEq, Eq, Debug, Serialize)]
pub struct ObjectSuccess {
    #[serde(skip_serializing_if = "Option::is_none")]
    authenticated: Option<bool>,
    actions: Actions,
}

/// https://github.com/git-lfs/git-lfs/blob/master/docs/api/batch.md#response-errors
#[derive(PartialEq, Eq, Debug, Serialize)]
pub struct ObjectError {
    code: u16,
    message: &'static str,
}

impl ObjectError {
    pub fn DoesNotExist() -> Self {
        Self {
            code: 404u16,
            message: "Object does not exist",
        }
    }

    pub fn RemovedByOwner() -> Self {
        Self {
            code: 410u16,
            message: "Object removed by owner",
        }
    }
    pub fn ValidationError() -> Self {
        Self {
            code: 422u16,
            message: "Validation error",
        }
    }
}

/// https://github.com/git-lfs/git-lfs/blob/master/docs/api/basic-transfers.md#basic-transfer-api
#[derive(PartialEq, Eq, Debug, Serialize)]
#[serde(untagged)]
pub enum Actions {
    Download { download: Action },
    None,
    Upload { upload: Action },
    UploadAndVerify { upload: Action, verify: Action },
}

/// https://github.com/git-lfs/git-lfs/blob/master/docs/api/basic-transfers.md#basic-transfer-api
#[derive(PartialEq, Eq, Debug, Serialize)]
pub struct Action {
    #[serde(with = "url_serde")]
    href: Url,
    #[serde(skip_serializing_if = "Option::is_none")]
    header: Option<HashMap<String, String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    expires_in: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    expires_at: Option<DateTime<FixedOffset>>,
}

impl Action {
    pub fn new(href: Url) -> Self {
        Self {
            href,
            header: None,
            expires_in: None,
            expires_at: None,
        }
    }
}

/// https://github.com/git-lfs/git-lfs/blob/master/docs/api/batch.md#response-errors
#[derive(PartialEq, Eq, Debug, Serialize)]
pub struct LfsErrorResponse {
    message: &'static str,
    #[serde(with = "url_serde")]
    documentation_url: Option<Url>,
    request_id: Option<String>,
    #[serde(skip)]
    status: u16,
}

impl LfsErrorResponse {
    const ACCEPT_HEADER_INCORRECT: Self = Self {
        message: "The Accept header needs to be `application/vnd.git-lfs+json`.",
        documentation_url: None,
        request_id: None,
        status: 406u16,
    };
    const RATE_LIMIT_HIT: Self = Self {
        message: "A rate limit has been hit with the server.",
        documentation_url: None,
        request_id: None,
        status: 429u16,
    };
    const NOT_IMPLEMENTED: Self = Self {
        message: "The server has not implemented the current method.",
        documentation_url: None,
        request_id: None,
        status: 501u16,
    };
    const INSUFFICIENT_STORAGE: Self = Self {
        message: "The server has insufficient storage capacity to complete the request.",
        documentation_url: None,
        request_id: None,
        status: 507u16,
    };

    const BANDWIDTH_LIMIT_EXCEEDED: Self = Self {
        message: "A bandwidth limit has been exceeded.",
        documentation_url: None,
        request_id: None,
        status: 509u16,
    };
}

#[cfg(test)]
mod test {
    use super::*;
    use pretty_assertions::assert_eq;

    #[test]
    fn batch_response_serializes_correctly() {
        assert_eq!(
            include_str!("test/batch_response_success.json"),
            serde_json::to_string_pretty(&BatchResponse {
                transfer: Some(Transfer::Basic),
                objects: vec![ObjectResponse::Success {
                    object: Object {
                        oid: "1111111".to_string(),
                        size: 123,
                    },
                    authenticated: Some(true),
                    actions: Actions::Download {
                        download: Action {
                            href: Url::parse("https://some-download.com").unwrap(),
                            header: Some(
                                [("Key", "value")]
                                    .iter()
                                    .map(|(k, v)| (k.to_string(), v.to_string()))
                                    .collect()
                            ),
                            expires_in: None,
                            expires_at: DateTime::parse_from_rfc3339("2016-11-10T15:29:07Z")
                                .unwrap()
                                .into()
                        }
                    }
                }],
            })
            .unwrap(),
        );

        assert_eq!(
            include_str!("test/batch_response_error.json"),
            serde_json::to_string_pretty(&BatchResponse {
                transfer: Some(Transfer::Basic),
                objects: vec![ObjectResponse::Error {
                    error: ObjectError::DoesNotExist(),
                    object: Object {
                        oid: "1111111".to_string(),
                        size: 123,
                    },
                }],
            })
            .unwrap()
        );
    }

    #[test]
    fn lfs_error_serializes_correctly() {
        assert_eq!(
            include_str!("test/lfs_error.json"),
            serde_json::to_string_pretty(&LfsErrorResponse {
                message: "Not found",
                documentation_url: Url::parse("https://lfs-server.com/docs/errors")
                    .unwrap()
                    .into(),
                request_id: Some("123".to_string()),
                status: 404u16
            })
            .unwrap(),
        );
    }
}