Skip to main content

lfsx_server/
model.rs

1use serde::{Deserialize, Serialize};
2
3use crate::locks::Lock;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
6#[serde(rename_all = "lowercase")]
7pub enum Operation {
8    Upload,
9    Download,
10}
11
12#[derive(Debug, Clone, Deserialize, Serialize)]
13pub struct ObjectId {
14    pub oid: String,
15    pub size: u64,
16}
17
18#[derive(Debug, Deserialize)]
19pub struct BatchRequest {
20    pub operation: Operation,
21    #[serde(default)]
22    pub transfers: Vec<String>,
23    pub objects: Vec<ObjectId>,
24}
25
26#[derive(Debug, Deserialize)]
27pub struct RetainRequest {
28    pub oids: Vec<String>,
29    #[serde(default)]
30    pub dry_run: bool,
31}
32
33#[derive(Debug, Serialize)]
34pub struct BatchResponse {
35    pub transfer: &'static str,
36    pub objects: Vec<ObjectSpec>,
37}
38
39#[derive(Debug, Serialize)]
40pub struct ObjectSpec {
41    #[serde(flatten)]
42    pub id: ObjectId,
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub actions: Option<Actions>,
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub error: Option<ObjectError>,
47}
48
49#[derive(Debug, Default, Serialize)]
50pub struct Actions {
51    #[serde(skip_serializing_if = "Option::is_none")]
52    pub upload: Option<Action>,
53    #[serde(skip_serializing_if = "Option::is_none")]
54    pub download: Option<Action>,
55    #[serde(skip_serializing_if = "Option::is_none")]
56    pub verify: Option<Action>,
57}
58
59#[derive(Debug, Serialize)]
60pub struct Action {
61    pub href: String,
62    pub expires_in: u32,
63}
64
65#[derive(Debug, Serialize)]
66pub struct ObjectError {
67    pub code: u16,
68    pub message: String,
69}
70
71impl ObjectSpec {
72    pub fn missing(id: ObjectId) -> Self {
73        Self {
74            id,
75            actions: None,
76            error: Some(ObjectError {
77                code: 404,
78                message: "object not found".into(),
79            }),
80        }
81    }
82
83    pub fn too_large(id: ObjectId, limit: u64) -> Self {
84        Self {
85            error: Some(ObjectError {
86                code: 413,
87                message: format!(
88                    "object is {} bytes, over the {limit} byte limit this server accepts",
89                    id.size
90                ),
91            }),
92            id,
93            actions: None,
94        }
95    }
96}
97
98#[derive(Debug, Deserialize)]
99pub struct CreateLockRequest {
100    pub path: String,
101}
102
103#[derive(Debug, Deserialize)]
104pub struct ListLocksQuery {
105    pub path: Option<String>,
106    pub id: Option<String>,
107}
108
109#[derive(Debug, Default, Deserialize)]
110pub struct VerifyLocksRequest {}
111
112#[derive(Debug, Deserialize)]
113pub struct UnlockRequest {
114    #[serde(default)]
115    pub force: bool,
116}
117
118#[derive(Debug, Serialize)]
119pub struct LockResponse {
120    pub lock: Lock,
121}
122
123#[derive(Debug, Serialize)]
124pub struct ListLocksResponse {
125    pub locks: Vec<Lock>,
126    pub next_cursor: &'static str,
127}
128
129#[derive(Debug, Serialize)]
130pub struct VerifyLocksResponse {
131    pub ours: Vec<Lock>,
132    pub theirs: Vec<Lock>,
133    pub next_cursor: &'static str,
134}
135
136#[derive(Debug, Serialize)]
137pub struct StatsResponse {
138    pub objects: u64,
139    pub bytes: u64,
140    pub locks: usize,
141}