Skip to content

Commit

Permalink
Update file node error handling and extension filtering (#3)
Browse files Browse the repository at this point in the history
The commit improves handling of file node errors by providing different responses based on the type of error. It also adds new command-line options for including or excluding files based on their extensions. Furthermore, the commit improves the management of file collections, filtering, and handling of cyclic dependencies with additional utility functions.
  • Loading branch information
joshainglis authored Mar 13, 2024
1 parent 0c0cac8 commit 05fbe7a
Show file tree
Hide file tree
Showing 14 changed files with 616 additions and 331 deletions.
105 changes: 102 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "topcat"
version = "0.1.1"
version = "0.1.2"
readme = "README.md"
license = "MIT OR Apache-2.0"
authors = ["josha@jci.dev", ]
Expand Down Expand Up @@ -35,6 +35,7 @@ regex = "1.10.3"
structopt = "0.3.26"
log = "0.4.21"
env_logger = "0.11.3"
graph-cycles = "0.1.0"

[dev-dependencies]
tempfile = "3.10.1"
tempfile = "3.10.1"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ build-backend = "maturin"

[project]
name = "topcat"
version = "0.1.1"
version = "0.1.2"
description = "A tool for concatenating files in topological order"
authors = [{ name = "Josha Inglis", email = "joshainglis@gmail.com" }]
readme = "README.md"
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ pub struct Config<'a> {
pub input_dirs: Vec<PathBuf>,
pub include_globs: Option<&'a [String]>,
pub exclude_globs: Option<&'a [String]>,
pub include_extensions: Option<&'a [String]>,
pub exclude_extensions: Option<&'a [String]>,
pub output: PathBuf,
pub comment_str: String,
pub file_separator_str: String,
pub file_end_str: String,
pub verbose: bool,
pub dry_run: bool,
pub include_hidden: bool,
}
35 changes: 29 additions & 6 deletions src/exceptions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::file_node::FileNode;
use std::collections::HashSet;
use std::error::Error;
use std::path::PathBuf;
use std::{fmt, io};
Expand All @@ -8,11 +10,10 @@ pub enum TopCatError {
InvalidFileHeader(PathBuf, String),
GraphMissing,
NameClash(String, PathBuf, PathBuf),
NoNameDefined(PathBuf),
MissingExist(String, String),
MissingDependency(String, String),
InvalidDependency(String, String),
CyclicDependency(String),
CyclicDependency(Vec<Vec<FileNode>>),
UnknownError(String),
}

Expand All @@ -22,11 +23,35 @@ impl fmt::Display for TopCatError {
Self::GraphMissing => write!(f, "Graph is None"),
Self::InvalidFileHeader(x, s) => write!(f, "Invalid file header in {}: {}", x.display(), s),
Self::NameClash(name, f1, f2) => write!(f, "Name {} found in both {} and {}", name, f1.display(), f2.display()),
Self::NoNameDefined(x) => write!(f, "No name defined in {}", x.display()),
Self::MissingExist(x, s) => write!(f, "MissingExist: {} expects {} to exist but it is not found", s, x),
Self::MissingDependency(x, s) => write!(f, "MissingDependency: {} depends on {} bit it is missing", s, x),
Self::InvalidDependency(x, s) => write!(f, "InvalidDependency: {} is marked as prepend so it cannot depend on {} which isn't marked as prepend", s, x),
Self::CyclicDependency(x) => write!(f, "CyclicDependency: {} has a cyclic dependency", x),
Self::CyclicDependency(x) => {
let mut error_message = "Cyclic dependency detected:\n".to_string();
for (i, cycle) in x.iter().enumerate() {
error_message.push_str(&format!(" Cycle {}:\n", i + 1));
let cycle_participants: HashSet<FileNode> = cycle.iter().cloned().collect();
error_message.push_str(" Participants:\n");
for participant in cycle_participants {
error_message.push_str(&format!(
" - {} ({})\n",
participant.name,
participant.path.display()
));
}
error_message.push_str(" Edges:\n");
for (i, node) in cycle.iter().enumerate() {
let next_node = &cycle[(i + 1) % cycle.len()];
error_message.push_str(&format!(
" - {} -> {}\n",
node.name,
next_node.name
));
}
}

write!(f, "{}", error_message)
},
Self::Io(err) => write!(f, "IO error: {}", err),
Self::UnknownError(s) => write!(f, "UnknownError: {}", s),
}
Expand All @@ -44,7 +69,6 @@ impl Error for TopCatError {}
#[derive(Debug)]
pub enum FileNodeError {
TooManyNames(PathBuf, Vec<String>),
InvalidPath(PathBuf),
NoNameDefined(PathBuf),
}

Expand All @@ -57,7 +81,6 @@ impl fmt::Display for FileNodeError {
x.display(),
s.join(", ")
),
Self::InvalidPath(x) => write!(f, "Invalid path: {}", x.display()),
Self::NoNameDefined(x) => write!(f, "No name defined in {}", x.display()),
}
}
Expand Down
Loading

0 comments on commit 05fbe7a

Please sign in to comment.