1use crate::{announce_address::AnnounceAddressDecodeError, wire_messages::IndexQuery};
4use serde::{Deserialize, Serialize};
5use std::{fmt, time::Duration};
6use thiserror::Error;
7
8#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
9pub struct FilesQuery {
10 pub peer_name: Option<String>,
11 pub query: IndexQuery,
12}
13
14#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
17pub enum UiEvent {
18 PeerConnected { name: String },
20 PeerDisconnected { name: String, error: String },
22 PeerConnectionFailed { name: String, error: String },
24 Uploaded(UploadInfo),
26 Download(DownloadEvent),
28}
29
30#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
31pub struct Info {
32 pub name: String,
33 pub os_home_dir: Option<String>,
34 pub announce_address: String,
35}
36
37#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
39pub struct UiDownloadRequest {
40 pub path: String,
42 pub progress: u64,
44 pub total_size: u64,
46 pub request_id: u32,
48 pub timestamp: Duration,
50 pub peer_name: String,
52}
53
54#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
55pub struct UiRequestedFile {
56 pub path: String,
57 pub size: u64,
59 pub request_id: u32,
62 pub downloaded: bool,
63}
64
65#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
67pub struct UploadInfo {
68 pub path: String,
69 pub bytes_read: u64,
70 pub speed: usize,
71 pub peer_name: String,
72}
73
74#[derive(Serialize, Deserialize, PartialEq, Debug, Error, Clone)]
76pub enum UiServerError {
77 #[error("Cannot connect: {0}")]
78 ConnectionError(String),
79 #[error("Request error: {0}")]
80 RequestError(String),
81 #[error("Error when updating shared directory")]
82 ShareError(String),
83 #[error("Serialization: {0}")]
84 Serialization(String),
85 #[error("Peer discovery: {0}")]
86 PeerDiscovery(String),
87 #[error("Poisoned lock")]
88 Poison,
89 #[error("Database: {0}")]
90 Db(String),
91 #[error("Error adding directory to share: {0}")]
92 AddShare(String),
93 #[error("Cannot decode announce address {0}")]
94 AnnounceAddressDecode(#[from] AnnounceAddressDecodeError),
95}
96
97#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
98pub enum DownloadInfo {
99 Downloading {
100 path: String,
102 bytes_read: u64,
104 total_bytes_read: u64,
106 speed: u32,
108 },
109 Completed(Duration),
110}
111
112#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
114pub struct DownloadEvent {
115 pub request_id: u32,
116 pub path: String,
118 pub peer_name: String,
120 pub download_info: DownloadInfo,
121 }
123
124impl fmt::Display for DownloadEvent {
125 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126 match &self.download_info {
127 DownloadInfo::Downloading {
128 path,
129 bytes_read,
130 total_bytes_read,
131 speed,
132 } => {
133 write!(
134 f,
135 "Downloading {}/{} {} bytes read, {} total bytes read, {} bps",
136 self.peer_name, path, bytes_read, total_bytes_read, speed
137 )
138 }
139 DownloadInfo::Completed(_time) => {
140 write!(f, "Completed {}/{}", self.peer_name, self.path)
141 }
142 }
143 }
144}
145
146#[derive(Serialize, Deserialize, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
148pub struct PeerPath {
149 pub peer_name: String,
151 pub path: String,
153}