Skip to content

Commit

Permalink
Merge pull request #39 from tamada/release/v0.7.2
Browse files Browse the repository at this point in the history
refactor: update functions to accept generic Path references instead …
  • Loading branch information
tamada authored Jan 26, 2025
2 parents fb35075 + 77027fd commit cd74eb5
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "totebag"
version = "0.7.1"
version = "0.7.2"
description = "A tool for extracting/archiving files and directories in multiple formats."
repository = "https://github.com/tamada/totebag"
readme = "README.md"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# totebag

[![Version](https://shields.io/badge/Version-0.7.1-blue)](https://github.com/tamada/totebag/releases/tag/v0.7.1)
[![Version](https://shields.io/badge/Version-0.7.2-blue)](https://github.com/tamada/totebag/releases/tag/v0.7.2)
[![MIT License](https://shields.io/badge/License-MIT-blue)](https://github.com/tamada/totebag/blob/main/LICENSE)
[![docker](https://shields.io/badge/Docker-0.7.1-blue?logo=docker)](https://github.com/tamada/totebag/pkgs/container/totebag)
[![docker](https://shields.io/badge/Docker-0.7.2-blue?logo=docker)](https://github.com/tamada/totebag/pkgs/container/totebag)

[![build](https://github.com/tamada/totebag/actions/workflows/build.yaml/badge.svg)](https://github.com/tamada/totebag/actions/workflows/build.yaml)
[![Rust Report Card](https://rust-reportcard.xuri.me/badge/github.com/tamada/totebag)](https://rust-reportcard.xuri.me/report/github.com/tamada/totebag)
Expand Down Expand Up @@ -67,7 +67,7 @@ brew install tamada/tap/totebag
## :whale: Docker
```sh
docker run -it --rm -v $PWD:/workdir ghcr.io/tamada/totebag:0.7.1 [OPTIONS] [ARGUMENTS]...
docker run -it --rm -v $PWD:/workdir ghcr.io/tamada/totebag:0.7.2 [OPTIONS] [ARGUMENTS]...
```
- **Working directory**: `/workdir`
Expand Down
4 changes: 2 additions & 2 deletions dockers/alpine/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM alpine:3.16 AS builder

ARG VERSION=0.7.1
ARG VERSION=0.7.2
ARG TARGETPLATFORM
ARG PLATFORM=${TARGETPLATFORM#linux/}

Expand All @@ -12,7 +12,7 @@ RUN apk add --no-cache curl tar gzip \

FROM alpine:3.16

ARG VERSION=0.7.1
ARG VERSION=0.7.2

LABEL org.opencontainers.image.source https://github.com/tamada/totebag

Expand Down
8 changes: 5 additions & 3 deletions src/archiver.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashSet;
use std::fs::{create_dir_all, File};
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use ignore::{Walk, WalkBuilder};
use typed_builder::TypedBuilder;
Expand Down Expand Up @@ -46,14 +46,15 @@ pub(crate) trait ToteArchiver {
pub struct Archiver {
#[builder(setter(into))]
pub archive_file: PathBuf,
#[builder(setter(into))]
pub targets: Vec<PathBuf>,
#[builder(default = None, setter(strip_option, into))]
pub rebase_dir: Option<PathBuf>,
#[builder(default = false)]
pub overwrite: bool,
#[builder(default = false)]
pub no_recursive: bool,
#[builder(default = vec![IgnoreType::Default])]
#[builder(default = vec![IgnoreType::Default], setter(into))]
pub ignore_types: Vec<IgnoreType>,
}

Expand Down Expand Up @@ -213,7 +214,7 @@ fn build_walker_impl(opts: &Archiver, w: &mut WalkBuilder) {
}
}

fn create_archiver(dest: &PathBuf) -> Result<Box<dyn ToteArchiver>> {
fn create_archiver<P: AsRef<Path>>(dest: P) -> Result<Box<dyn ToteArchiver>> {
use crate::archiver::cab::CabArchiver;
use crate::archiver::lha::LhaArchiver;
use crate::archiver::rar::RarArchiver;
Expand All @@ -223,6 +224,7 @@ fn create_archiver(dest: &PathBuf) -> Result<Box<dyn ToteArchiver>> {
};
use crate::archiver::zip::ZipArchiver;

let dest = dest.as_ref();
let format = find_format(dest);
match format {
Ok(format) => match format {
Expand Down
5 changes: 3 additions & 2 deletions src/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl Extractor {
Err(e) => return Err(e),
};
match self.can_extract() {
Ok(_) => extractor.perform(self.archive_file.clone(), PathUtils { e: &self }),
Ok(_) => extractor.perform(self.archive_file.clone(), PathUtils { e: self }),
Err(e) => Err(e),
}
}
Expand Down Expand Up @@ -196,7 +196,8 @@ pub(crate) trait ToteExtractor {

/// Returns the extractor for the given archive file.
/// The supported format is `cab`, `lha`, `rar`, `7z`, `tar`, `tar.gz`, `tar.bz2`, `tar.xz`, `tar.zst`, and `zip`.
fn create(file: &Path) -> Result<Box<dyn ToteExtractor>> {
fn create<P: AsRef<Path>>(file: P) -> Result<Box<dyn ToteExtractor>> {
let file = file.as_ref();
let format = find_format(file);
match format {
Ok(format) => match format {
Expand Down
2 changes: 1 addition & 1 deletion src/extractor/tar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn extract_tar<R: Read>(mut archive: tar::Archive<R>, opts: PathUtils) -> Result
let size = entry.header().size().unwrap();
log::info!("extracting {:?} ({} bytes)", path, size);

let dest = opts.destination(path.to_path_buf())?;
let dest = opts.destination(&path)?;
if entry.header().entry_type().is_file() {
create_dir_all(dest.parent().unwrap()).unwrap();
entry.unpack(dest).unwrap();
Expand Down
43 changes: 22 additions & 21 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use std::fmt::Display;
use std::path::{Path, PathBuf};
use std::path::Path;

use super::{Result, ToteError};

/// return `true` if all of `args` is an acceptable archive file name for totebag.
/// ```
/// args.iter().all(is_archive_file)
/// ```
pub fn is_all_args_archives(args: &[PathBuf]) -> bool {
pub fn is_all_args_archives<P: AsRef<Path>>(args: &[P]) -> bool {
args.iter().all(is_archive_file)
}

/// returns `true`` when the given path is an acceptable archive file name for totebag.
pub fn is_archive_file(arg: &PathBuf) -> bool {
pub fn is_archive_file<P: AsRef<Path>>(arg: P) -> bool {
let arg = arg.as_ref();
let name = arg.to_str().unwrap().to_lowercase();
for (_, ext) in exts().iter() {
if name.ends_with(ext) {
Expand All @@ -24,8 +25,8 @@ pub fn is_archive_file(arg: &PathBuf) -> bool {

/// Find the format of the given file name.
/// If the given file name has an unknown extension for totebag, it returns an `Err(ToteErro::Unknown)`.
pub fn find_format(path: &Path) -> Result<Format> {
match path.file_name() {
pub fn find_format<P: AsRef<Path>>(path: P) -> Result<Format> {
match path.as_ref().file_name() {
Some(file_name) => {
let name = file_name.to_str().unwrap().to_lowercase();
for ext in exts().iter() {
Expand Down Expand Up @@ -103,42 +104,42 @@ mod tests {

#[test]
fn test_format() {
if let Err(e) = find_format(&PathBuf::from("hoge.unknown")) {
if let Err(e) = find_format("hoge.unknown") {
if let ToteError::UnknownFormat(s) = e {
assert_eq!(s, "hoge.unknown".to_string());
} else {
assert!(false);
}
}
if let Ok(f) = find_format(&PathBuf::from("hoge.zip")) {
if let Ok(f) = find_format("hoge.zip") {
assert_eq!(f, Format::Zip);
assert_eq!(f.to_string(), "Zip".to_string());
}
if let Ok(f) = find_format(&PathBuf::from("hoge.tar")) {
if let Ok(f) = find_format("hoge.tar") {
assert_eq!(f, Format::Tar);
assert_eq!(f.to_string(), "Tar".to_string());
}
if let Ok(f) = find_format(&PathBuf::from("hoge.rar")) {
if let Ok(f) = find_format("hoge.rar") {
assert_eq!(f, Format::Rar);
assert_eq!(f.to_string(), "Rar".to_string());
}
if let Ok(f) = find_format(&PathBuf::from("hoge.tar.gz")) {
if let Ok(f) = find_format("hoge.tar.gz") {
assert_eq!(f, Format::TarGz);
assert_eq!(f.to_string(), "TarGz".to_string());
}
if let Ok(f) = find_format(&PathBuf::from("hoge.tar.bz2")) {
if let Ok(f) = find_format("hoge.tar.bz2") {
assert_eq!(f, Format::TarBz2);
assert_eq!(f.to_string(), "TarBz2".to_string());
}
if let Ok(f) = find_format(&PathBuf::from("hoge.tar.xz")) {
if let Ok(f) = find_format("hoge.tar.xz") {
assert_eq!(f, Format::TarXz);
assert_eq!(f.to_string(), "TarXz".to_string());
}
if let Ok(f) = find_format(&PathBuf::from("hoge.7z")) {
if let Ok(f) = find_format("hoge.7z") {
assert_eq!(f, Format::SevenZ);
assert_eq!(f.to_string(), "SevenZ".to_string());
}
if let Err(e) = find_format(&PathBuf::from(".")) {
if let Err(e) = find_format(".") {
if let ToteError::NoArgumentsGiven = e {
assert!(true);
} else {
Expand All @@ -150,13 +151,13 @@ mod tests {
#[test]
fn test_is_all_args_archives() {
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")
"test.zip",
"test.tar",
"test.tar.gz",
"test.tgz",
"test.tar.bz2",
"test.tbz2",
"test.rar",
]));
}
}
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ mod cli;
mod list;

fn update_loglevel(level: LogLevel) {
use env_logger;
match level {
cli::LogLevel::Error => std::env::set_var("RUST_LOG", "error"),
cli::LogLevel::Warn => std::env::set_var("RUST_LOG", "warn"),
Expand Down

0 comments on commit cd74eb5

Please sign in to comment.