Skip to content

Commit

Permalink
Use fclones::path::Path for GroupConfig::base_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
pkolaczk committed Apr 14, 2022
1 parent b0e18fb commit 3b44dd4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
19 changes: 8 additions & 11 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ pub struct GroupConfig {

/// Base directory to use when resolving relative input paths.
#[structopt(long, parse(from_os_str), default_value("."))]
pub base_dir: PathBuf,
pub base_dir: Path,

/// A list of input paths.
///
Expand Down Expand Up @@ -402,28 +402,25 @@ impl GroupConfig {

/// Makes the base directory absolute.
/// Returns error if the base directory does not exist.
pub fn resolve_base_dir(&mut self) -> io::Result<&PathBuf> {
pub fn resolve_base_dir(&mut self) -> io::Result<&Path> {
if self.base_dir.is_relative() {
self.base_dir = std::env::current_dir()?.join(&self.base_dir)
let curr_dir = Arc::from(Path::from(std::env::current_dir()?));
self.base_dir = curr_dir.join(&self.base_dir)
}
if !self.base_dir.is_dir() {
if !self.base_dir.to_path_buf().is_dir() {
return Err(io::Error::new(
ErrorKind::NotFound,
format!("Directory not found: {}", self.base_dir.display()),
format!("Directory not found: {}", self.base_dir.to_escaped_string()),
));
}
self.base_dir = self.base_dir.canonicalize()?;
self.base_dir = self.base_dir.canonicalize();
Ok(&self.base_dir)
}

/// Returns an iterator over the absolute input paths.
/// Input paths may be provided as arguments or from standard input.
pub fn input_paths(&self) -> Box<dyn Iterator<Item = Path> + Send> {
let base_dir = Arc::new(if self.base_dir.as_os_str().is_empty() {
Path::from(".")
} else {
Path::from(&self.base_dir)
});
let base_dir = Arc::new(self.base_dir.clone());
if self.stdin {
Box::new(
BufReader::new(stdin())
Expand Down
2 changes: 1 addition & 1 deletion src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ pub fn write_report(config: &GroupConfig, log: &Log, groups: &[FileGroup<Path>])
timestamp: DateTime::from_utc(now.naive_utc(), *now.offset()),
version: env!("CARGO_PKG_VERSION").to_owned(),
command: args_os().map(Arg::from).collect(),
base_dir: Path::from(&config.base_dir),
base_dir: config.base_dir.clone(),
stats: Some(FileStats {
group_count: groups.len(),
total_file_count: total_count,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ fn get_command_config(header: &ReportHeader) -> Result<Config, Error> {
// Configure the same base directory as set when running the previous command.
// This is important to get the correct input paths.
if let Command::Group(ref mut group_config) = command.command {
group_config.base_dir = header.base_dir.to_path_buf();
group_config.base_dir = header.base_dir.clone();
}
Ok(command)
}
Expand Down
6 changes: 6 additions & 0 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ impl AsRef<Path> for Path {
}
}

impl Default for Path {
fn default() -> Self {
Path::from(".")
}
}

/// Converts std path Component to a new CString
fn component_to_c_string(c: &Component<'_>) -> CString {
os_to_c_str(c.as_os_str())
Expand Down

0 comments on commit 3b44dd4

Please sign in to comment.