Skip to main content

computer_transfer/
lib.rs

1//! Packing and unpacking of files and folders for transfers between the host and the computer.
2//!
3//! Both sides speak tar. Packing and unpacking are synchronous, so callers run them with
4//! `spawn_blocking` and connect them to async streams with the types in [`pipe`].
5
6mod pack;
7pub mod pipe;
8mod rules;
9mod unpack;
10
11use std::fmt;
12
13pub use pack::{Packed, pack, root_name};
14pub use rules::Platform;
15pub use unpack::{Unpacked, check_destination, unpack};
16
17/// What a transfer did before it stopped.
18#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
19pub struct Progress {
20    pub files: u64,
21    pub folders: u64,
22    pub bytes: u64,
23}
24
25/// A transfer that stopped, with how far it got.
26#[derive(Debug)]
27pub struct Failure {
28    pub message: String,
29    pub progress: Progress,
30}
31
32impl Failure {
33    pub(crate) fn new(message: impl Into<String>, progress: Progress) -> Self {
34        Self {
35            message: message.into(),
36            progress,
37        }
38    }
39}
40
41impl fmt::Display for Failure {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        let Progress {
44            files,
45            folders,
46            bytes,
47        } = self.progress;
48        if files == 0 && folders == 0 && bytes == 0 {
49            write!(f, "{}. Nothing was copied.", self.message)
50        } else {
51            write!(
52                f,
53                "{}. Before it stopped, {files} files, {folders} folders and {bytes} bytes were copied.",
54                self.message
55            )
56        }
57    }
58}
59
60impl std::error::Error for Failure {}