lxmf-sdk 0.9.6

High-level Rust SDK for LXMF clients and RPC-backed LXMF workflows.
Documentation
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[non_exhaustive]
pub struct PropagationRemoteRequest {
    pub remote: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub identity_private_key_hex: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timeout_secs: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub transfer_limit_kb: Option<f64>,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[non_exhaustive]
pub struct PropagationRemotePeerRequest {
    pub remote: String,
    pub peer: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub identity_private_key_hex: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timeout_secs: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub transfer_limit_kb: Option<f64>,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[non_exhaustive]
pub struct PropagationAcknowledgeSyncRequest {
    #[serde(default, skip_serializing_if = "is_false")]
    pub reset_state: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub failure_state: Option<u32>,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[non_exhaustive]
pub struct PropagationNodeSetRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub peer: Option<String>,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[non_exhaustive]
pub struct PropagationEnableRequest {
    pub enabled: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auth_required: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub store_root: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub target_cost: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stamp_cost_flexibility: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message_storage_limit_mb: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub delivery_limit: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub propagation_limit: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sync_limit: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub autopeer: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub autopeer_maxdepth: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub static_peers: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_peers: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from_static_only: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub retain_synced_on_node: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub peering_cost: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub remote_peering_cost_max: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub control_allowed: Option<Vec<String>>,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[non_exhaustive]
pub struct PropagationDeliveryPolicyRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auth_required: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allowed_destinations: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub denied_destinations: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ignored_destinations: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prioritised_destinations: Option<Vec<String>>,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[non_exhaustive]
pub struct PropagationDeliveryPolicyEntryRequest {
    pub destination: String,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[non_exhaustive]
pub struct PropagationIngestRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub transient_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub payload_hex: Option<String>,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[non_exhaustive]
pub struct PropagationFetchRequest {
    pub transient_id: String,
}

#[derive(Clone, Debug, Serialize, PartialEq)]
#[non_exhaustive]
pub struct PropagationRemoteStatusResult {
    pub remote: String,
    #[serde(default)]
    pub status: JsonValue,
    #[serde(default)]
    pub status_state: PropagationRemoteStatusState,
    #[serde(default)]
    pub stats: PropagationRemoteStats,
}

#[derive(Deserialize)]
struct RawPropagationRemoteStatusResult {
    remote: String,
    #[serde(default)]
    status: JsonValue,
}

impl<'de> Deserialize<'de> for PropagationRemoteStatusResult {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let raw = RawPropagationRemoteStatusResult::deserialize(deserializer)?;
        let status_state = PropagationRemoteStatusState::from_status(&raw.status);
        let stats = PropagationRemoteStats::from_status(&raw.status);
        Ok(Self {
            remote: raw.remote,
            status: raw.status,
            status_state,
            stats,
        })
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Default)]
#[non_exhaustive]
pub struct PropagationRemoteTransferState {
    #[serde(default)]
    pub synced: bool,
    #[serde(default)]
    pub postponed: bool,
    #[serde(default)]
    pub postpone_reason: Option<String>,
    #[serde(default)]
    pub imported_count: u64,
    #[serde(default)]
    pub imported_ids: Vec<String>,
    #[serde(default)]
    pub transferred_bytes: u64,
    #[serde(default)]
    pub state_name: Option<String>,
    #[serde(default)]
    pub selected_node: Option<String>,
    #[serde(default)]
    pub selected_peer: Option<String>,
    #[serde(default)]
    pub sync_progress: Option<f64>,
    #[serde(default)]
    pub last_sync_started: Option<i64>,
    #[serde(default)]
    pub last_sync_completed: Option<i64>,
    #[serde(default)]
    pub last_sync_error: Option<String>,
    #[serde(default)]
    pub failure_kind: Option<String>,
    #[serde(default)]
    pub timed_out: bool,
    #[serde(default)]
    pub access_denied: bool,
    #[serde(default)]
    pub retry_count: u64,
    #[serde(default)]
    pub next_sync_attempt: Option<i64>,
}

impl PropagationRemoteTransferState {
    pub(crate) fn from_result_and_propagation(result: &JsonValue, propagation: &JsonValue) -> Self {
        let state_name = json_string(propagation, "state_name").ok().flatten();
        let sync_state = json_u64(propagation, "sync_state").ok().flatten().unwrap_or(0) as u32;
        let failure_kind = json_string(result, "failure_kind").ok().flatten()
            .or_else(|| json_string(propagation, "failure_kind").ok().flatten())
            .or_else(|| match state_name.as_deref() {
                Some("no_access") => Some("no_access".to_string()),
                Some("timeout") => Some("timeout".to_string()),
                _ => None,
            });
        let timed_out = failure_kind.as_deref() == Some("timeout")
            || json_string(result, "postpone_reason").ok().flatten().as_deref() == Some("timeout")
            || state_name.as_deref() == Some("timeout");
        let access_denied = json_bool(propagation, "access_denied").ok().flatten().unwrap_or(false)
            || state_name.as_deref() == Some("no_access")
            || sync_state == 0xf4
            || matches!(failure_kind.as_deref(), Some("access_denied" | "access-denied" | "no_access"));
        Self {
            synced: json_bool(result, "synced").ok().flatten().unwrap_or(false),
            postponed: json_bool(result, "postponed").ok().flatten().unwrap_or(false),
            postpone_reason: json_string(result, "postpone_reason").ok().flatten(),
            imported_count: json_u64(result, "imported_count").ok().flatten().unwrap_or(0),
            imported_ids: remote_transfer_json_string_array(result, "imported_ids"),
            transferred_bytes: json_u64(result, "transferred_bytes").ok().flatten().unwrap_or(0),
            state_name,
            selected_node: json_string(propagation, "selected_node").ok().flatten(),
            selected_peer: json_string(propagation, "selected_peer").ok().flatten(),
            sync_progress: json_f64(propagation, "sync_progress").ok().flatten(),
            last_sync_started: json_i64(propagation, "last_sync_started").ok().flatten(),
            last_sync_completed: json_i64(propagation, "last_sync_completed").ok().flatten(),
            last_sync_error: json_string(propagation, "last_sync_error").ok().flatten(),
            failure_kind,
            timed_out,
            access_denied,
            retry_count: json_u64(propagation, "retry_count").ok().flatten().unwrap_or(0),
            next_sync_attempt: json_i64(propagation, "next_sync_attempt").ok().flatten(),
        }
    }
}

#[derive(Clone, Debug, Serialize, PartialEq)]
#[non_exhaustive]
pub struct PropagationRemoteTransferResult {
    pub remote: String,
    #[serde(default)]
    pub propagation: JsonValue,
    #[serde(default)]
    pub result: JsonValue,
    #[serde(default)]
    pub transfer_state: PropagationRemoteTransferState,
    #[serde(default)]
    pub queue: PropagationPeerQueueSnapshot,
}

#[derive(Deserialize)]
struct RawPropagationRemoteTransferResult {
    remote: String,
    #[serde(default)]
    propagation: JsonValue,
    #[serde(default)]
    result: JsonValue,
}

impl<'de> Deserialize<'de> for PropagationRemoteTransferResult {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let raw = RawPropagationRemoteTransferResult::deserialize(deserializer)?;
        let queue =
            PropagationPeerQueueSnapshot::from_messages_and_propagation(&JsonValue::Null, &raw.propagation);
        let transfer_state =
            PropagationRemoteTransferState::from_result_and_propagation(&raw.result, &raw.propagation);
        Ok(Self {
            remote: raw.remote,
            propagation: raw.propagation,
            result: raw.result,
            transfer_state,
            queue,
        })
    }
}

#[derive(Clone, Debug, Serialize, PartialEq)]
#[non_exhaustive]
pub struct PropagationAcknowledgeSyncResult {
    #[serde(default)]
    pub propagation: JsonValue,
    #[serde(default)]
    pub recovery_state: PropagationRecoveryStateResult,
}

#[derive(Deserialize)]
struct RawPropagationAcknowledgeSyncResult {
    #[serde(default)]
    propagation: JsonValue,
}

impl<'de> Deserialize<'de> for PropagationAcknowledgeSyncResult {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let raw = RawPropagationAcknowledgeSyncResult::deserialize(deserializer)?;
        let recovery_state = PropagationRecoveryStateResult::try_from_propagation(
            raw.propagation.clone(),
        )
        .map_err(serde::de::Error::custom)?;
        Ok(Self {
            propagation: raw.propagation,
            recovery_state,
        })
    }
}

#[derive(Clone, Debug, Serialize, PartialEq)]
#[non_exhaustive]
pub struct PropagationStatusResult {
    #[serde(default)]
    pub propagation: JsonValue,
    #[serde(default)]
    pub recovery_state: PropagationRecoveryStateResult,
}

#[derive(Deserialize)]
struct RawPropagationStatusResult {
    #[serde(default)]
    propagation: JsonValue,
}

impl<'de> Deserialize<'de> for PropagationStatusResult {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let raw = RawPropagationStatusResult::deserialize(deserializer)?;
        let recovery_state = PropagationRecoveryStateResult::try_from_propagation(
            raw.propagation.clone(),
        )
        .map_err(serde::de::Error::custom)?;
        Ok(Self {
            propagation: raw.propagation,
            recovery_state,
        })
    }
}

#[derive(Clone, Debug, Serialize, PartialEq)]
#[non_exhaustive]
pub struct PropagationRemoteSyncResult {
    pub remote: String,
    #[serde(default)]
    pub peer: Option<String>,
    #[serde(default)]
    pub propagation: JsonValue,
    #[serde(default)]
    pub peer_sync: JsonValue,
    #[serde(default)]
    pub peer_sync_state: Option<PropagationPeerSyncResult>,
    #[serde(default)]
    pub transfer_state: PropagationRemoteTransferState,
    #[serde(default)]
    pub queue: PropagationPeerQueueSnapshot,
    #[serde(default)]
    pub result: JsonValue,
}

#[derive(Deserialize)]
struct RawPropagationRemoteSyncResult {
    remote: String,
    #[serde(default)]
    peer: Option<String>,
    #[serde(default)]
    propagation: JsonValue,
    #[serde(default)]
    peer_sync: JsonValue,
    #[serde(default)]
    result: JsonValue,
}

impl<'de> Deserialize<'de> for PropagationRemoteSyncResult {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let raw = RawPropagationRemoteSyncResult::deserialize(deserializer)?;
        let peer_sync_state = if raw.peer_sync.get("peer").is_some() {
            Some(
                serde_json::from_value::<PropagationPeerSyncResult>(raw.peer_sync.clone())
                    .map_err(serde::de::Error::custom)?,
            )
        } else {
            None
        };
        let transfer_state =
            PropagationRemoteTransferState::from_result_and_propagation(&raw.result, &raw.propagation);
        let empty_messages = JsonValue::Null;
        let queue_messages = raw.peer_sync.get("messages").unwrap_or(&empty_messages);
        let queue = PropagationPeerQueueSnapshot::from_messages_and_propagation(queue_messages, &raw.propagation);
        Ok(Self {
            remote: raw.remote,
            peer: raw.peer,
            propagation: raw.propagation,
            peer_sync: raw.peer_sync,
            peer_sync_state,
            transfer_state,
            queue,
            result: raw.result,
        })
    }
}