Skip to main content

harddrive_party_shared/
wire_messages.rs

1//! Wire messages for communicating with other Peers
2pub use crate::announce_address::{AnnounceAddress, PeerConnectionDetails};
3use serde::{Deserialize, Serialize};
4use thiserror::Error;
5
6// TODO read error
7
8/// A request to a remote peer
9#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Hash, Eq)]
10pub enum Request {
11    /// A request to read the remote peer's shared file index
12    Ls(IndexQuery),
13    /// A request to download a remote peer's file (or a portion of the file)
14    Read(ReadQuery),
15    /// Contact details of another peer
16    AnnouncePeer(AnnouncePeer),
17}
18
19/// A request to read the remote peer's shared file index
20#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Eq, Hash, Default)]
21pub struct IndexQuery {
22    /// Base directory to query - defaults to all shared directories
23    pub path: Option<String>,
24    /// Filter term to search with
25    pub searchterm: Option<String>,
26    /// Whether to expand directories
27    pub recursive: bool,
28}
29
30/// A request to download a remote peers file (or a portion of the file)
31#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Eq, Hash)]
32pub struct ReadQuery {
33    /// Path of the requested file
34    pub path: String,
35    /// Offset to start reading
36    pub start: Option<u64>,
37    /// Offset to finish reading
38    pub end: Option<u64>,
39}
40
41/// A response to a `Request::Ls(IndexQuery)`
42#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
43pub enum LsResponse {
44    /// The found files or directories if the query was successful
45    Success(Vec<Entry>),
46    Err(LsResponseError),
47}
48
49/// A file or directory entry in a share query response
50#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Hash, Eq)]
51pub struct Entry {
52    /// Path and filename
53    pub name: String,
54    /// Size in bytes
55    pub size: u64,
56    /// Whether this is a directory or a file
57    pub is_dir: bool,
58}
59
60/// Error from making a share index query
61#[derive(Error, Serialize, Deserialize, PartialEq, Debug, Clone)]
62pub enum LsResponseError {
63    #[error("Database error")]
64    DbError,
65    #[error("Path not found")]
66    PathNotFound,
67    #[error("Internal error: {0}")]
68    InternalServer(String),
69}
70
71#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Hash)]
72pub struct AnnouncePeer {
73    pub announce_address: AnnounceAddress,
74    // TODO signature
75}