2020-02-14 09:12:49 +01:00
|
|
|
use crate::common::*;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct Status {
|
|
|
|
pieces: bool,
|
|
|
|
files: Vec<FileStatus>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Status {
|
|
|
|
pub(crate) fn new(pieces: bool, files: Vec<FileStatus>) -> Status {
|
|
|
|
Status { pieces, files }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn pieces(&self) -> bool {
|
|
|
|
self.pieces
|
|
|
|
}
|
|
|
|
|
2020-03-06 06:44:20 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn files(&self) -> &[FileStatus] {
|
|
|
|
&self.files
|
2020-02-14 09:12:49 +01:00
|
|
|
}
|
|
|
|
|
2020-03-06 06:44:20 +01:00
|
|
|
pub(crate) fn good(&self) -> bool {
|
|
|
|
self.pieces && self.files.iter().all(FileStatus::good)
|
2020-02-14 09:12:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Status {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
let bad = self.files.iter().filter(|status| status.bad()).count();
|
|
|
|
|
|
|
|
if bad != 0 {
|
|
|
|
write!(f, "{} of {} files corrupted", bad, self.files.len())?;
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.pieces() {
|
|
|
|
write!(f, "pieces corrupted")?;
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "ok")?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|