use crate::{announce_address::AnnounceAddressDecodeError, wire_messages::IndexQuery};
use serde::{Deserialize, Serialize};
use std::{fmt, time::Duration};
use thiserror::Error;
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct FilesQuery {
pub peer_name: Option<String>,
pub query: IndexQuery,
}
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub enum UiEvent {
PeerConnected { name: String },
PeerDisconnected { name: String, error: String },
PeerConnectionFailed { name: String, error: String },
Uploaded(UploadInfo),
Download(DownloadEvent),
}
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Info {
pub name: String,
pub os_home_dir: Option<String>,
pub announce_address: String,
}
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct UiDownloadRequest {
pub path: String,
pub progress: u64,
pub total_size: u64,
pub request_id: u32,
pub timestamp: Duration,
pub peer_name: String,
}
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct UiRequestedFile {
pub path: String,
pub size: u64,
pub request_id: u32,
pub downloaded: bool,
}
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct UploadInfo {
pub path: String,
pub bytes_read: u64,
pub speed: usize,
pub peer_name: String,
}
#[derive(Serialize, Deserialize, PartialEq, Debug, Error, Clone)]
pub enum UiServerError {
#[error("Cannot connect: {0}")]
ConnectionError(String),
#[error("Request error: {0}")]
RequestError(String),
#[error("Error when updating shared directory")]
ShareError(String),
#[error("Serialization: {0}")]
Serialization(String),
#[error("Peer discovery: {0}")]
PeerDiscovery(String),
#[error("Poisoned lock")]
Poison,
#[error("Database: {0}")]
Db(String),
#[error("Error adding directory to share: {0}")]
AddShare(String),
#[error("Cannot decode announce address {0}")]
AnnounceAddressDecode(#[from] AnnounceAddressDecodeError),
}
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub enum DownloadInfo {
Downloading {
path: String,
bytes_read: u64,
total_bytes_read: u64,
speed: u32,
},
Completed(Duration),
}
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct DownloadEvent {
pub request_id: u32,
pub path: String,
pub peer_name: String,
pub download_info: DownloadInfo,
}
impl fmt::Display for DownloadEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.download_info {
DownloadInfo::Downloading {
path,
bytes_read,
total_bytes_read,
speed,
} => {
write!(
f,
"Downloading {}/{} {} bytes read, {} total bytes read, {} bps",
self.peer_name, path, bytes_read, total_bytes_read, speed
)
}
DownloadInfo::Completed(_time) => {
write!(f, "Completed {}/{}", self.peer_name, self.path)
}
}
}
}
#[derive(Serialize, Deserialize, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct PeerPath {
pub peer_name: String,
pub path: String,
}