Skip to content

Commit

Permalink
Add fs module
Browse files Browse the repository at this point in the history
  • Loading branch information
9999years committed Oct 16, 2024
1 parent 0b22e6f commit 1228f08
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 15 deletions.
79 changes: 79 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
[[disallowed-methods]]
path = "std::process::Command::output"
reason = "Use command_error::CommandExt::output_checked[_with][_utf8]"

[[disallowed-methods]]
path = "std::process::Command::status"
reason = "Use command_error::CommandExt::status_checked[_with]"

[[disallowed-methods]]
path = "std::process::Command::spawn"
reason = "Use command_error::CommandExt::spawn_checked"

[[disallowed-methods]]
path = "std::process::Child::try_wait"
reason = "Use command_error::ChildExt::try_wait_checked[_with]"

[[disallowed-methods]]
path = "std::process::Child::wait"
reason = "Use command_error::ChildExt::wait_checked[_with]"

[[disallowed-methods]]
path = "std::process::Child::wait_with_output"
reason = "Use command_error::ChildExt::output_checked[_with][_utf8]"

[[disallowed-methods]]
path = "fs_err::rename"
reason = "Use git_prole::fs::rename"

[[disallowed-methods]]
path = "std::fs::rename"
reason = "Use git_prole::fs::rename"

[[disallowed-methods]]
path = "fs_err::create_dir_all"
reason = "Use git_prole::fs::create_dir_all"

[[disallowed-methods]]
path = "std::fs::create_dir_all"
reason = "Use git_prole::fs::create_dir_all"

[[disallowed-methods]]
path = "fs_err::create_dir"
reason = "Use git_prole::fs::create_dir"

[[disallowed-methods]]
path = "std::fs::create_dir"
reason = "Use git_prole::fs::create_dir"

[[disallowed-methods]]
path = "fs_err::remove_dir"
reason = "Use git_prole::fs::remove_dir"

[[disallowed-methods]]
path = "std::fs::remove_dir"
reason = "Use git_prole::fs::remove_dir"

[[disallowed-methods]]
path = "fs_err::read_to_string"
reason = "Use git_prole::fs::read_to_string"

[[disallowed-methods]]
path = "std::fs::read_to_string"
reason = "Use git_prole::fs::read_to_string"

[[disallowed-methods]]
path = "fs_err::copy"
reason = "Use git_prole::fs::copy"

[[disallowed-methods]]
path = "std::fs::copy"
reason = "Use git_prole::fs::copy"

[[disallowed-methods]]
path = "fs_err::write"
reason = "Use git_prole::fs::copy"

[[disallowed-methods]]
path = "std::fs::write"
reason = "Use git_prole::fs::copy"
6 changes: 3 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use calm_io::stdout;
use clap::CommandFactory;
use fs_err as fs;
use miette::miette;
use miette::IntoDiagnostic;

Expand All @@ -12,6 +11,7 @@ use crate::cli::ConfigGenerateArgs;
use crate::config::Config;
use crate::convert::ConvertPlan;
use crate::convert::ConvertPlanOpts;
use crate::fs;
use crate::git::Git;

pub struct App {
Expand Down Expand Up @@ -87,10 +87,10 @@ impl App {
);

if let Some(parent) = path.parent() {
fs::create_dir_all(parent).into_diagnostic()?;
fs::create_dir_all(parent)?;
}

fs::write(path, Config::DEFAULT).into_diagnostic()?;
fs::write(path, Config::DEFAULT)?;

Ok(())
}
Expand Down
6 changes: 2 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use camino::Utf8PathBuf;
use clap::Parser;
use fs_err as fs;
use miette::Context;
use miette::IntoDiagnostic;
use serde::Deserialize;
use xdg::BaseDirectories;

use crate::cli::Cli;
use crate::fs;
use crate::install_tracing::install_tracing;

/// Configuration, both from the command-line and a user configuration file.
Expand Down Expand Up @@ -45,9 +45,7 @@ impl Config {
ConfigFile::default()
} else {
toml::from_str(
&fs::read_to_string(&path)
.into_diagnostic()
.wrap_err("Failed to read configuration file")?,
&fs::read_to_string(&path).wrap_err("Failed to read configuration file")?,
)
.into_diagnostic()
.wrap_err("Failed to deserialize configuration file")?
Expand Down
1 change: 1 addition & 0 deletions src/copy_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ macro_rules! make_err {
/// * Filesystem boundaries may be crossed.
/// * Symbolic links will be copied, not followed.
#[instrument(level = "trace", skip_all)]
#[expect(clippy::disallowed_methods)]
pub fn copy_dir<Q: AsRef<Path>, P: AsRef<Path>>(from: P, to: Q) -> Result<Vec<Error>> {
if !from.as_ref().exists() {
return Err(make_err!("source path does not exist", ErrorKind::NotFound));
Expand Down
74 changes: 74 additions & 0 deletions src/fs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//! Like [`fs_err`], but the functions are instrumented with [`tracing::instrument`] and return
//! [`miette::Result`] instead of [`std::io::Result`].
use std::fmt::Debug;
use std::path::Path;

use miette::IntoDiagnostic;
use tracing::instrument;

#[instrument(level = "trace")]
pub fn rename<P, Q>(from: P, to: Q) -> miette::Result<()>
where
P: AsRef<Path> + Debug,
Q: AsRef<Path> + Debug,
{
#[expect(clippy::disallowed_methods)]
fs_err::rename(from, to).into_diagnostic()
}

#[instrument(level = "trace")]
pub fn create_dir<P>(path: P) -> miette::Result<()>
where
P: AsRef<Path> + Debug,
{
#[expect(clippy::disallowed_methods)]
fs_err::create_dir(path).into_diagnostic()
}

#[instrument(level = "trace")]
pub fn create_dir_all<P>(path: P) -> miette::Result<()>
where
P: AsRef<Path> + Debug,
{
#[expect(clippy::disallowed_methods)]
fs_err::create_dir_all(path).into_diagnostic()
}

#[instrument(level = "trace")]
pub fn remove_dir<P>(path: P) -> miette::Result<()>
where
P: AsRef<Path> + Debug,
{
#[expect(clippy::disallowed_methods)]
fs_err::remove_dir(path).into_diagnostic()
}

#[instrument(level = "trace")]
pub fn read_to_string<P>(path: P) -> miette::Result<String>
where
P: AsRef<Path> + Debug,
{
#[expect(clippy::disallowed_methods)]
fs_err::read_to_string(path).into_diagnostic()
}

#[instrument(level = "trace")]
pub fn copy<P, Q>(from: P, to: Q) -> miette::Result<u64>
where
P: AsRef<Path> + Debug,
Q: AsRef<Path> + Debug,
{
#[expect(clippy::disallowed_methods)]
fs_err::copy(from, to).into_diagnostic()
}

#[instrument(level = "trace")]
pub fn write<P, C>(path: P, contents: C) -> miette::Result<()>
where
P: AsRef<Path> + Debug,
C: AsRef<[u8]> + Debug,
{
#[expect(clippy::disallowed_methods)]
fs_err::write(path, contents).into_diagnostic()
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod convert;
mod copy_dir;
mod current_dir;
mod format_bulleted_list;
pub mod fs;
mod gh;
mod git;
mod install_tracing;
Expand Down
11 changes: 4 additions & 7 deletions test-harness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::process::Command;
use camino::Utf8PathBuf;
use clonable_command::Command as ClonableCommand;
use command_error::CommandExt;
use fs_err as fs;
use git_prole::fs;
use git_prole::Git;
use git_prole::Utf8TempDir;
use itertools::Itertools;
Expand Down Expand Up @@ -40,8 +40,7 @@ impl GitProle {
[init]\n\
defaultBranch = main\n\
",
)
.into_diagnostic()?;
)?;

let git_prole = test_bin::get_test_bin("git-prole").get_program().to_owned();

Expand Down Expand Up @@ -106,8 +105,7 @@ impl GitProle {
"set -ex\n\
{script}"
),
)
.into_diagnostic()?;
)?;
self.any_command("bash")
.arg("--norc")
.arg(tempfile.as_ref())
Expand Down Expand Up @@ -164,9 +162,8 @@ impl GitProle {
}

pub fn write_config(&self, contents: &str) -> miette::Result<()> {
fs::create_dir_all(self.path(".config/git-prole")).into_diagnostic()?;
fs::create_dir_all(self.path(".config/git-prole"))?;
fs::write(self.path(".config/git-prole/config.toml"), contents)
.into_diagnostic()
.wrap_err("Failed to write `git-prole` configuration")?;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion test-harness/src/repo_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::str::FromStr;

use camino::Utf8PathBuf;
use expect_test::Expect;
use fs_err as fs;
use git_prole::format_bulleted_list_multiline;
use git_prole::fs;
use git_prole::BranchRef;
use git_prole::Git;
use git_prole::LocalBranchRef;
Expand Down

0 comments on commit 1228f08

Please sign in to comment.