• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tamada / totebag / 10414561419

16 Aug 2024 04:16AM UTC coverage: 81.322% (-0.1%) from 81.417%
10414561419

push

github

web-flow
Merge pull request #34 from tamada/release/v0.6.0

Release/v0.6.0

275 of 322 new or added lines in 12 files covered. (85.4%)

2 existing lines in 1 file now uncovered.

1206 of 1483 relevant lines covered (81.32%)

6.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.0
/src/format.rs
1
use std::{ffi::OsStr, path::PathBuf};
2
use std::fmt::Display;
3

4
use crate::cli::{Result, ToteError};
5

6
pub fn is_all_args_archives(args: &[PathBuf]) -> bool {
4✔
7
    args.iter().all(is_archive_file)
4✔
8
}
4✔
9

10
pub fn is_archive_file(arg: &PathBuf) -> bool {
13✔
11
    let name = arg.to_str().unwrap().to_lowercase();
13✔
12
    for (_, ext) in exts().iter() {
131✔
13
        if name.ends_with(ext) {
131✔
14
            return true
11✔
15
        }
120✔
16
    }
17
    return false
2✔
18
}
13✔
19

20
pub fn find_format(file_name: Option<&OsStr>) -> Result<Format> {
29✔
21
    match file_name {
29✔
22
        Some(file_name) => {
27✔
23
            let name = file_name.to_str().unwrap().to_lowercase();
27✔
24
            for ext in exts().iter() {
243✔
25
                if name.ends_with(&ext.1) {
243✔
26
                    return Ok(ext.0.clone());
24✔
27
                }
219✔
28
            }
29
            return Ok(Format::Unknown(file_name.to_str().unwrap().to_string()));
3✔
30
        }
31
        None => Err(ToteError::NoArgumentsGiven),
2✔
32
    }
33
}
29✔
34

35
fn exts() -> Vec<(Format, String)> {
40✔
36
    vec![
40✔
37
        (Format::Cab, String::from(".cab")),
40✔
38
        (Format::LHA, String::from(".lha")),
40✔
39
        (Format::LHA, String::from(".lzh")),
40✔
40
        (Format::SevenZ, String::from(".7z")),
40✔
41
        (Format::Rar, String::from(".rar")),
40✔
42
        (Format::Tar, String::from(".tar")),
40✔
43
        (Format::TarGz, String::from(".tar.gz")),
40✔
44
        (Format::TarGz, String::from(".tgz")),
40✔
45
        (Format::TarBz2, String::from(".tar.bz2")),
40✔
46
        (Format::TarBz2, String::from(".tbz2")),
40✔
47
        (Format::TarXz, String::from(".tar.xz")),
40✔
48
        (Format::TarXz, String::from(".txz")),
40✔
49
        (Format::TarZstd, String::from(".tar.zst")),
40✔
50
        (Format::TarZstd, String::from(".tzst")),
40✔
51
        (Format::Zip, String::from(".zip")),
40✔
52
        (Format::Zip, String::from(".jar")),
40✔
53
        (Format::Zip, String::from(".war")),
40✔
54
        (Format::Zip, String::from(".ear")),
40✔
55
    ]
40✔
56
}
40✔
57

58
#[derive(Debug, PartialEq, Clone)]
59
pub enum Format {
60
    Cab,
61
    LHA,
62
    SevenZ,
63
    Rar,
64
    Tar,
65
    TarGz,
66
    TarBz2,
67
    TarXz,
68
    TarZstd,
69
    Zip,
70
    Unknown(String),
71
}
72

73
impl Display for Format {
74
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
9✔
75
        match self {
9✔
NEW
76
            Format::Cab => write!(f, "Cab"),
×
NEW
77
            Format::LHA => write!(f, "LHA"),
×
78
            Format::SevenZ => write!(f, "SevenZ"),
1✔
79
            Format::Rar => write!(f, "Rar"),
1✔
80
            Format::Tar => write!(f, "Tar"),
1✔
81
            Format::TarBz2 => write!(f, "TarBz2"),
1✔
82
            Format::TarGz => write!(f, "TarGz"),
1✔
83
            Format::TarXz => write!(f, "TarXz"),
1✔
84
            Format::TarZstd => write!(f, "TarZstd"),
×
85
            Format::Zip => write!(f, "Zip"),
1✔
86
            Format::Unknown(s) => write!(f, "{}: unknown format", s),
2✔
87
        }
88
    }
9✔
89
}
90

91
#[cfg(test)]
92
mod tests {
93
    use super::*;
94

95
    #[test]
96
    fn test_format() {
1✔
97
        assert!(find_format(None).is_err());
1✔
98
        if let Ok(f) = find_format(Some(OsStr::new("hoge.zip"))) {
1✔
99
            assert_eq!(f, Format::Zip);
1✔
100
            assert_eq!(f.to_string(), "Zip".to_string());
1✔
101
        }
×
102
        if let Ok(f) = find_format(Some(OsStr::new("hoge.unknown"))) {
1✔
103
            assert_eq!(f.to_string(), "hoge.unknown: unknown format".to_string());
1✔
104
        }
×
105
        if let Ok(f) = find_format(Some(OsStr::new("hoge.tar"))) {
1✔
106
            assert_eq!(f, Format::Tar);
1✔
107
            assert_eq!(f.to_string(), "Tar".to_string());
1✔
108
        }
×
109
        if let Ok(f) = find_format(Some(OsStr::new("hoge.rar"))) {
1✔
110
            assert_eq!(f, Format::Rar);
1✔
111
            assert_eq!(f.to_string(), "Rar".to_string());
1✔
112
        }
×
113
        if let Ok(f) = find_format(Some(OsStr::new("hoge.tar.gz"))) {
1✔
114
            assert_eq!(f, Format::TarGz);
1✔
115
            assert_eq!(f.to_string(), "TarGz".to_string());
1✔
116
        }
×
117
        if let Ok(f) = find_format(Some(OsStr::new("hoge.tar.bz2"))) {
1✔
118
            assert_eq!(f, Format::TarBz2);
1✔
119
            assert_eq!(f.to_string(), "TarBz2".to_string());
1✔
120
        }
×
121
        if let Ok(f) = find_format(Some(OsStr::new("hoge.tar.xz"))) {
1✔
122
            assert_eq!(f, Format::TarXz);
1✔
123
            assert_eq!(f.to_string(), "TarXz".to_string());
1✔
124
        }
×
125
        if let Ok(f) = find_format(Some(OsStr::new("hoge.7z"))) {
1✔
126
            assert_eq!(f, Format::SevenZ);
1✔
127
            assert_eq!(f.to_string(), "SevenZ".to_string());
1✔
128
        }
×
129
        if let Err(e) = find_format(None) {
1✔
130
            if let ToteError::NoArgumentsGiven = e {
1✔
131
                assert!(true);
1✔
132
            } else {
133
                assert!(false);
×
134
            }
135
        }
×
136
    }
1✔
137

138
    #[test]
139
    fn test_is_all_args_archives() {
1✔
140
        assert!(is_all_args_archives(&[PathBuf::from("test.zip"), PathBuf::from("test.tar"), PathBuf::from("test.tar.gz"), PathBuf::from("test.tgz"), PathBuf::from("test.tar.bz2"), PathBuf::from("test.tbz2"), PathBuf::from("test.rar")]));
1✔
141
    }
1✔
142
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc