2020-02-14 09:12:49 +01:00
|
|
|
use crate::common::*;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2020-03-15 11:22:33 +01:00
|
|
|
pub(crate) enum Status {
|
|
|
|
Single {
|
|
|
|
pieces: bool,
|
|
|
|
error: Option<FileError>,
|
|
|
|
},
|
|
|
|
Multiple {
|
|
|
|
pieces: bool,
|
|
|
|
files: Vec<FileStatus>,
|
|
|
|
},
|
2020-02-14 09:12:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Status {
|
2020-03-15 11:22:33 +01:00
|
|
|
pub(crate) fn single(pieces: bool, error: Option<FileError>) -> Self {
|
|
|
|
Status::Single { pieces, error }
|
2020-02-14 09:12:49 +01:00
|
|
|
}
|
|
|
|
|
2020-03-15 11:22:33 +01:00
|
|
|
pub(crate) fn multiple(pieces: bool, files: Vec<FileStatus>) -> Self {
|
|
|
|
Status::Multiple { pieces, files }
|
2020-02-14 09:12:49 +01:00
|
|
|
}
|
|
|
|
|
2020-03-15 11:22:33 +01:00
|
|
|
pub(crate) fn pieces(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Self::Single { pieces, .. } | Self::Multiple { pieces, .. } => *pieces,
|
|
|
|
}
|
2020-02-14 09:12:49 +01:00
|
|
|
}
|
|
|
|
|
2020-03-06 06:44:20 +01:00
|
|
|
pub(crate) fn good(&self) -> bool {
|
2020-03-15 11:22:33 +01:00
|
|
|
self.pieces()
|
|
|
|
&& match self {
|
|
|
|
Self::Single { error, .. } => error.is_none(),
|
|
|
|
Self::Multiple { files, .. } => files.iter().all(FileStatus::is_good),
|
|
|
|
}
|
2020-02-14 09:12:49 +01:00
|
|
|
}
|
|
|
|
|
2020-03-15 11:22:33 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn count_bad(&self) -> usize {
|
|
|
|
match self {
|
|
|
|
Self::Single { error, .. } => {
|
|
|
|
if error.is_some() {
|
|
|
|
1
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Self::Multiple { files, .. } => files.iter().filter(|file| file.is_bad()).count(),
|
|
|
|
}
|
|
|
|
}
|
2020-02-14 09:12:49 +01:00
|
|
|
|
2020-03-16 06:41:47 +01:00
|
|
|
pub(crate) fn print(&self, env: &mut Env) -> Result<()> {
|
2020-03-15 11:22:33 +01:00
|
|
|
match self {
|
|
|
|
Self::Single { error, .. } => {
|
|
|
|
if let Some(error) = error {
|
|
|
|
error.println(env.err_mut()).context(error::Stderr)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Self::Multiple { files, .. } => {
|
|
|
|
for file in files {
|
|
|
|
if let Some(error) = file.error() {
|
|
|
|
let style = env.err().style();
|
|
|
|
err!(
|
|
|
|
env,
|
|
|
|
"{}{}:{} ",
|
|
|
|
style.message().prefix(),
|
|
|
|
file.path(),
|
|
|
|
style.message().suffix(),
|
|
|
|
)?;
|
|
|
|
error.println(env.err_mut()).context(error::Stderr)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-14 09:12:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !self.pieces() {
|
2020-03-15 11:22:33 +01:00
|
|
|
errln!(env, "Pieces corrupted.")?;
|
2020-02-14 09:12:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|