radicle/node/events/
upload_pack.rs1use std::fmt;
2use std::io;
3use std::process::ExitStatus;
4
5use crate::node::NodeId;
6use crate::prelude::RepoId;
7
8#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
10pub enum UploadPack {
11 Done {
13 rid: RepoId,
15 remote: NodeId,
17 status: String,
22 },
23 Write {
25 rid: RepoId,
27 remote: NodeId,
29 progress: Progress,
31 },
32 Error {
34 rid: RepoId,
36 remote: NodeId,
38 err: String,
40 },
41 PackProgress {
43 rid: RepoId,
45 remote: NodeId,
47 transmitted: usize,
49 },
50}
51
52impl UploadPack {
53 pub fn pack_progress(rid: RepoId, remote: NodeId, transmitted: usize) -> Self {
55 Self::PackProgress {
56 rid,
57 remote,
58 transmitted,
59 }
60 }
61
62 pub fn write(rid: RepoId, remote: NodeId, progress: Progress) -> Self {
64 Self::Write {
65 rid,
66 remote,
67 progress,
68 }
69 }
70
71 pub fn done(rid: RepoId, remote: NodeId, status: ExitStatus) -> Self {
76 Self::Done {
77 rid,
78 remote,
79 status: status.to_string(),
80 }
81 }
82
83 pub fn error(rid: RepoId, remote: NodeId, err: io::Error) -> Self {
84 Self::Error {
85 rid,
86 remote,
87 err: err.to_string(),
88 }
89 }
90}
91
92#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
94#[serde(rename_all = "camelCase")]
95pub enum Progress {
96 Enumerating { total: usize },
97 Counting { processed: usize, total: usize },
98 Compressing { processed: usize, total: usize },
99}
100
101impl fmt::Display for Progress {
102 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103 match self {
104 Progress::Enumerating { total } => write!(f, "Enumerating objects: {total}"),
105 Progress::Counting { processed, total } => {
106 let percent = (processed / total) * 100;
107 write!(f, "Counting objects: {percent}% ({processed}/{total})")
108 }
109 Progress::Compressing { processed, total } => {
110 let percent = (processed / total) * 100;
111 write!(f, "Compressing objects: {percent}% ({processed}/{total})")
112 }
113 }
114 }
115}