git_lfs_spec/spec/transfer/
custom.rs

1use crate::spec::Object;
2use serde_derive::{Deserialize, Serialize};
3use std::path::PathBuf;
4
5/// https://github.com/git-lfs/git-lfs/blob/master/docs/custom-transfers.md#stage-1-intiation
6#[derive(PartialEq, Eq, Debug, Deserialize, Serialize, Clone)]
7#[serde(rename_all = "camelCase")]
8pub enum Operation {
9    Upload,
10    Download,
11}
12
13/// https://github.com/git-lfs/git-lfs/blob/master/docs/custom-transfers.md#stage-1-intiation
14#[derive(PartialEq, Eq, Debug, Deserialize, Serialize, Clone)]
15#[serde(rename_all = "camelCase")]
16pub struct Init {
17    pub operation: Operation,
18    pub remote: String,
19    pub concurrent: bool,
20    pub concurrenttransfers: Option<usize>,
21}
22
23/// https://github.com/git-lfs/git-lfs/blob/master/docs/custom-transfers.md#uploads
24#[derive(PartialEq, Eq, Debug, Deserialize, Serialize, Clone)]
25#[serde(rename_all = "camelCase")]
26pub struct Upload {
27    #[serde(flatten)]
28    pub object: Object,
29    pub path: std::path::PathBuf,
30}
31
32/// https://github.com/git-lfs/git-lfs/blob/master/docs/custom-transfers.md#downloads
33#[derive(PartialEq, Eq, Debug, Deserialize, Serialize, Clone)]
34#[serde(rename_all = "camelCase")]
35pub struct Download {
36    #[serde(flatten)]
37    pub object: Object,
38}
39
40/// https://github.com/git-lfs/git-lfs/blob/master/docs/custom-transfers.md#stage-2-0n-transfers
41#[derive(PartialEq, Eq, Debug, Deserialize, Serialize, Clone)]
42#[serde(rename_all = "camelCase")]
43pub struct Complete {
44    pub oid: String,
45    #[serde(flatten)]
46    pub result: Option<Result>,
47}
48
49#[derive(PartialEq, Eq, Debug, Deserialize, Serialize, Clone)]
50#[serde(rename_all = "camelCase")]
51pub enum Result {
52    Path(PathBuf),
53    Error(Error),
54}
55
56/// https://github.com/git-lfs/git-lfs/blob/master/docs/custom-transfers.md#stage-2-0n-transfers
57#[derive(PartialEq, Eq, Debug, Deserialize, Serialize, Clone)]
58#[serde(rename_all = "camelCase")]
59pub struct Error {
60    pub code: i32,
61    pub message: String,
62}
63
64/// https://github.com/git-lfs/git-lfs/blob/master/docs/custom-transfers.md#progress
65#[derive(PartialEq, Eq, Debug, Deserialize, Serialize, Clone)]
66#[serde(rename_all = "camelCase")]
67pub struct Progress {
68    pub oid: String,
69    pub bytes_so_far: u64,
70    pub bytes_since_last: u64,
71}
72
73/// https://github.com/git-lfs/git-lfs/blob/master/docs/custom-transfers.md#protocol
74#[derive(PartialEq, Eq, Debug, Deserialize, Serialize, Clone)]
75#[serde(tag = "event", rename_all = "camelCase")]
76pub enum Event {
77    Init(Init),
78    #[serde(skip)]
79    AcknowledgeInit,
80    Upload(Box<Upload>),
81    Download(Box<Download>),
82    Complete(Box<Complete>),
83    Progress(Box<Progress>),
84    /// https://github.com/git-lfs/git-lfs/blob/master/docs/custom-transfers.md#stage-3-finish--cleanup
85    Terminate,
86}
87
88#[cfg(test)]
89mod test {
90    use super::*;
91    use pretty_assertions::assert_eq;
92    use std::str::FromStr;
93
94    #[test]
95    fn custom_complete_serializes_ok() {
96        assert_eq!(
97            include_str!("../test/custom_complete.json"),
98            serde_json::to_string(&Event::Complete(
99                Complete {
100                    oid: "bf3e3e2af9366a3b704ae0c31de5afa64193ebabffde2091936ad2e7510bc03a"
101                        .to_string(),
102                    result: Some(Result::Error(Error {
103                        code: 2,
104                        message: "Explain what happened to this transfer".to_string()
105                    })),
106                }
107                .into()
108            ))
109            .unwrap(),
110        );
111    }
112    #[test]
113    fn custom_download_serializes_ok() {
114        assert_eq!(
115            include_str!("../test/custom_download.json"),
116            serde_json::to_string(&Event::Download(
117                Download {
118                    object: Object {
119                        oid: "22ab5f63670800cc7be06dbed816012b0dc411e774754c7579467d2536a9cf3e"
120                            .to_string(),
121                        size: 21245,
122                    }
123                }
124                .into()
125            ))
126            .unwrap(),
127        );
128    }
129
130    #[test]
131    fn custom_init_serializes_ok() {
132        assert_eq!(
133            include_str!("../test/custom_init.json"),
134            serde_json::to_string(&Event::Init(Init {
135                operation: Operation::Download,
136                remote: "origin".to_string(),
137                concurrent: true,
138                concurrenttransfers: Some(3)
139            }))
140            .unwrap(),
141        );
142    }
143
144    #[test]
145    fn custom_progress_serializes_ok() {
146        assert_eq!(
147            include_str!("../test/custom_progress.json"),
148            serde_json::to_string(&Event::Progress(
149                Progress {
150                    oid: "22ab5f63670800cc7be06dbed816012b0dc411e774754c7579467d2536a9cf3e"
151                        .to_string(),
152                    bytes_so_far: 1234,
153                    bytes_since_last: 64,
154                }
155                .into()
156            ))
157            .unwrap(),
158        );
159    }
160
161    #[test]
162    fn custom_terminate_serializes_ok() {
163        assert_eq!(
164            include_str!("../test/custom_terminate.json"),
165            serde_json::to_string(&Event::Terminate).unwrap(),
166        );
167    }
168
169    #[test]
170    fn custom_upload_serializes_ok() {
171        assert_eq!(
172            include_str!("../test/custom_upload.json"),
173            serde_json::to_string(&Event::Upload(
174                Upload {
175                    object: Object {
176                        oid: "bf3e3e2af9366a3b704ae0c31de5afa64193ebabffde2091936ad2e7510bc03a"
177                            .to_string(),
178                        size: 346232
179                    },
180                    path: std::path::PathBuf::from_str("/path/to/file.png").unwrap()
181                }
182                .into()
183            ))
184            .unwrap(),
185        );
186    }
187}