-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwire_messages.rs
64 lines (57 loc) · 1.89 KB
/
wire_messages.rs
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
//! Wire messages for communicating with other Peers
use serde::{Deserialize, Serialize};
use thiserror::Error;
// TODO read error
/// A request to a remote peer
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Hash, Eq)]
pub enum Request {
/// A request to read the remote peer's shared file index
Ls(IndexQuery),
/// A request to download a remote peer's file (or a portion of the file)
Read(ReadQuery),
}
/// A request to read the remote peer's shared file index
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Eq, Hash)]
pub struct IndexQuery {
/// Base directory to query - defaults to all shared directories
pub path: Option<String>,
/// Filter term to search with
pub searchterm: Option<String>,
/// Whether to expand directories
pub recursive: bool,
}
/// A request to download a remote peers file (or a portion of the file)
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Eq, Hash)]
pub struct ReadQuery {
/// Path of the requested file
pub path: String,
/// Offset to start reading
pub start: Option<u64>,
/// Offset to finish reading
pub end: Option<u64>,
}
/// A response to a `Request::Ls(IndexQuery)`
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub enum LsResponse {
/// The found files or directories if the query was successful
Success(Vec<Entry>),
Err(LsResponseError),
}
/// A file or directory entry in a share query response
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Entry {
/// Path and filename
pub name: String,
/// Size in bytes
pub size: u64,
/// Whether this is a directory or a file
pub is_dir: bool,
}
/// Error from making a share index query
#[derive(Error, Serialize, Deserialize, PartialEq, Debug, Clone)]
pub enum LsResponseError {
#[error("Database error")]
DbError,
#[error("Path not found")]
PathNotFound,
}