use std::fmt;
use std::io;
use std::process::ExitStatus;
use crate::node::NodeId;
use crate::prelude::RepoId;
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum UploadPack {
Done {
rid: RepoId,
remote: NodeId,
status: String,
},
Write {
rid: RepoId,
remote: NodeId,
progress: Progress,
},
Error {
rid: RepoId,
remote: NodeId,
err: String,
},
PackProgress {
rid: RepoId,
remote: NodeId,
transmitted: usize,
},
}
impl UploadPack {
pub fn pack_progress(rid: RepoId, remote: NodeId, transmitted: usize) -> Self {
Self::PackProgress {
rid,
remote,
transmitted,
}
}
pub fn write(rid: RepoId, remote: NodeId, progress: Progress) -> Self {
Self::Write {
rid,
remote,
progress,
}
}
pub fn done(rid: RepoId, remote: NodeId, status: ExitStatus) -> Self {
Self::Done {
rid,
remote,
status: status.to_string(),
}
}
pub fn error(rid: RepoId, remote: NodeId, err: io::Error) -> Self {
Self::Error {
rid,
remote,
err: err.to_string(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Progress {
Enumerating { total: usize },
Counting { processed: usize, total: usize },
Compressing { processed: usize, total: usize },
}
impl fmt::Display for Progress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Progress::Enumerating { total } => write!(f, "Enumerating objects: {total}"),
Progress::Counting { processed, total } => {
let percent = (processed / total) * 100;
write!(f, "Counting objects: {percent}% ({processed}/{total})")
}
Progress::Compressing { processed, total } => {
let percent = (processed / total) * 100;
write!(f, "Compressing objects: {percent}% ({processed}/{total})")
}
}
}
}