Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl Compiler {

return Err(
SearchError::FilesystemIo {
io_error,
source: io_error,
path: directory,
}
.into(),
Expand All @@ -214,10 +214,7 @@ impl Compiler {
};

for entry in entries {
let entry = entry.map_err(|io_error| SearchError::FilesystemIo {
io_error,
path: directory.clone(),
})?;
let entry = entry.context(search_error::FilesystemIo { path: &directory })?;

if let Some(name) = entry.file_name().to_str() {
for (candidate, case_sensitive) in &candidates {
Expand Down
3 changes: 1 addition & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ impl Config {
Ok(Subcommand::Man)
} else if let Some(request) = arguments.subcommand.request.as_deref() {
Ok(Subcommand::Request {
request: serde_json::from_str(request)
.map_err(|source| ConfigError::RequestParse { source })?,
request: serde_json::from_str(request).context(config_error::RequestParse)?,
})
} else if arguments.subcommand.show.is_some() {
Ok(Subcommand::Show {
Expand Down
10 changes: 3 additions & 7 deletions src/datetime_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ where
{
let items = StrftimeItems::new(format)
.parse()
.map_err(|source| DatetimeFormatError::Parse {
format: format.into(),
source,
})?;
.context(datetime_format_error::Parse { format })?;

let mut result = String::new();

datetime
.format_with_items(items.iter())
.write_to(&mut result)
.map_err(|fmt::Error| DatetimeFormatError::Format {
format: format.into(),
})?;
.ok()
.context(datetime_format_error::Format { format })?;

Ok(result)
}
22 changes: 5 additions & 17 deletions src/datetime_format_error.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
use super::*;

#[derive(Debug)]
#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)), context(suffix(false)))]
pub(crate) enum DatetimeFormatError {
Format {
format: String,
},
#[snafu(display("failed to format time with format string `{format}`"))]
Format { format: String },
#[snafu(display("failed to parse time format string `{format}`: {source}"))]
Parse {
format: String,
source: chrono::ParseError,
},
}

impl Display for DatetimeFormatError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Format { format } => {
write!(f, "failed to format time with format string `{format}`")
}
Self::Parse { source, format } => {
write!(f, "failed to parse time format string `{format}`: {source}")
}
}
}
}
54 changes: 15 additions & 39 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ impl Search {
})
}
SearchConfig::FromStandardInput { working_directory } => {
let source =
io::read_to_string(io::stdin()).map_err(|io_error| SearchError::StdinIo { io_error })?;
let source = io::read_to_string(io::stdin()).context(search_error::StdinIo)?;

let (justfile, tempdir) = Self::tempdir_justfile(config, &source)?;

Expand Down Expand Up @@ -86,9 +85,7 @@ impl Search {

justfile
.parent()
.ok_or_else(|| SearchError::JustfileHadNoParent {
path: justfile.clone(),
})?;
.context(search_error::JustfileHadNoParent { path: &justfile })?;

Self::with_justfile(config, justfile, Self::clean(config, working_directory))
}
Expand All @@ -105,10 +102,7 @@ impl Search {
.is_some_and(|extension| extension.eq_ignore_ascii_case("md"))
{
let markdown =
fs::read_to_string(&justfile).map_err(|io_error| SearchError::FilesystemIo {
io_error,
path: justfile.clone(),
})?;
fs::read_to_string(&justfile).context(search_error::FilesystemIo { path: &justfile })?;

let source = tangle(&markdown);

Expand Down Expand Up @@ -138,14 +132,11 @@ impl Search {
} else {
builder.tempdir()
}
.map_err(|io_error| SearchError::TempdirIo { io_error })?;
.context(search_error::TempdirIo)?;

let justfile = dir::temporary_directory(&tempdir)?.join("justfile");

fs::write(&justfile, source).map_err(|io_error| SearchError::FilesystemIo {
io_error,
path: justfile.clone(),
})?;
fs::write(&justfile, source).context(search_error::FilesystemIo { path: &justfile })?;

Ok((justfile, tempdir))
}
Expand All @@ -154,10 +145,7 @@ impl Search {
for (directory, filename) in Self::global_justfile_paths()? {
if let Ok(read_dir) = fs::read_dir(&directory) {
for entry in read_dir {
let entry = entry.map_err(|io_error| SearchError::FilesystemIo {
io_error,
path: directory.clone(),
})?;
let entry = entry.context(search_error::FilesystemIo { path: &directory })?;

let Ok(path) = entry.path().into_utf8() else {
continue;
Expand All @@ -179,8 +167,8 @@ impl Search {
.justfile
.parent()
.and_then(|path| path.parent())
.ok_or_else(|| SearchError::JustfileHadNoParent {
path: self.justfile.clone(),
.context(search_error::JustfileHadNoParent {
path: &self.justfile,
})?;
Self::find_in_directory(config, parent)
}
Expand Down Expand Up @@ -250,16 +238,11 @@ impl Search {
for directory in directory.ancestors() {
let mut candidates = BTreeSet::new();

let entries = fs::read_dir(directory).map_err(|io_error| SearchError::FilesystemIo {
io_error,
path: directory.to_owned(),
})?;
let entries =
fs::read_dir(directory).context(search_error::FilesystemIo { path: directory })?;

for entry in entries {
let entry = entry.map_err(|io_error| SearchError::FilesystemIo {
io_error,
path: directory.to_owned(),
})?;
let entry = entry.context(search_error::FilesystemIo { path: directory })?;

let Ok(path) = entry.path().into_utf8() else {
continue;
Expand Down Expand Up @@ -303,16 +286,11 @@ impl Search {
/// system directories given in `PROJECT_ROOT_CHILDREN`
fn project_root(config: &Config, directory: &Utf8Path) -> SearchResult<Utf8PathBuf> {
for directory in directory.ancestors() {
let entries = fs::read_dir(directory).map_err(|io_error| SearchError::FilesystemIo {
io_error,
path: directory.to_owned(),
})?;
let entries =
fs::read_dir(directory).context(search_error::FilesystemIo { path: directory })?;

for entry in entries {
let entry = entry.map_err(|io_error| SearchError::FilesystemIo {
io_error,
path: directory.to_owned(),
})?;
let entry = entry.context(search_error::FilesystemIo { path: directory })?;
for project_root_child in PROJECT_ROOT_CHILDREN.iter().copied() {
if entry.file_name() == project_root_child {
return Ok(directory.to_owned());
Expand All @@ -334,9 +312,7 @@ impl Search {
Ok(
justfile
.parent()
.ok_or_else(|| SearchError::JustfileHadNoParent {
path: justfile.to_path_buf(),
})?
.context(search_error::JustfileHadNoParent { path: justfile })?
.to_owned(),
)
}
Expand Down
12 changes: 6 additions & 6 deletions src/search_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use super::*;
#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)), context(suffix(false)))]
pub(crate) enum SearchError {
#[snafu(display("I/O error at `{path}`: {io_error}"))]
#[snafu(display("I/O error at `{path}`: {source}"))]
FilesystemIo {
io_error: io::Error,
source: io::Error,
path: Utf8PathBuf,
},
#[snafu(display("cannot initialize global justfile"))]
Expand All @@ -30,10 +30,10 @@ pub(crate) enum SearchError {
NotFound,
#[snafu(transparent)]
Path { source: PathError },
#[snafu(display("error reading from standard input: {io_error}"))]
StdinIo { io_error: io::Error },
#[snafu(display("I/O error creating temporary directory: {io_error}"))]
TempdirIo { io_error: io::Error },
#[snafu(display("error reading from standard input: {source}"))]
StdinIo { source: io::Error },
#[snafu(display("I/O error creating temporary directory: {source}"))]
TempdirIo { source: io::Error },
}

#[cfg(test)]
Expand Down
Loading