Skip to content

Commit

Permalink
Refactor and document subcommand and search (#2335)
Browse files Browse the repository at this point in the history
  • Loading branch information
neunenak authored Aug 31, 2024
1 parent b4dbc8d commit f222b02
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
8 changes: 7 additions & 1 deletion src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl Search {
})
}

/// Get working directory and justfile path for newly-initialized justfile
pub(crate) fn init(
search_config: &SearchConfig,
invocation_directory: &Path,
Expand Down Expand Up @@ -125,7 +126,9 @@ impl Search {
}
}

pub(crate) fn justfile(directory: &Path) -> SearchResult<PathBuf> {
/// Search upwards from `directory` for a file whose name matches one of
/// `JUSTFILE_NAMES`
fn justfile(directory: &Path) -> SearchResult<PathBuf> {
for directory in directory.ancestors() {
let mut candidates = BTreeSet::new();

Expand Down Expand Up @@ -175,6 +178,9 @@ impl Search {
clean.into_iter().collect()
}

/// Search upwards from `directory` for the root directory of a software
/// project, as determined by the presence of one of the version control
/// system directories given in `PROJECT_ROOT_CHILDREN`
fn project_root(directory: &Path) -> SearchResult<PathBuf> {
for directory in directory.ancestors() {
let entries = fs::read_dir(directory).map_err(|io_error| SearchError::Io {
Expand Down
47 changes: 23 additions & 24 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ impl Subcommand {

let compilation = Self::compile(config, loader, &search)?;
let justfile = &compilation.justfile;
let ast = compilation.root_ast();
let src = compilation.root_src();

match self {
Choose { overrides, chooser } => {
Expand All @@ -78,8 +76,8 @@ impl Subcommand {
Command { overrides, .. } | Evaluate { overrides, .. } => {
justfile.run(config, &search, overrides, &[])?;
}
Dump => Self::dump(config, ast, justfile)?,
Format => Self::format(config, &search, src, ast, justfile)?,
Dump => Self::dump(config, compilation)?,
Format => Self::format(config, &search, compilation)?,
Groups => Self::groups(config, justfile),
List { path } => Self::list(config, justfile, path)?,
Show { path } => Self::show(config, justfile, path)?,
Expand Down Expand Up @@ -303,14 +301,14 @@ impl Subcommand {
Ok(())
}

fn dump(config: &Config, ast: &Ast, justfile: &Justfile) -> RunResult<'static> {
fn dump(config: &Config, compilation: Compilation) -> RunResult<'static> {
match config.dump_format {
DumpFormat::Json => {
serde_json::to_writer(io::stdout(), justfile)
serde_json::to_writer(io::stdout(), &compilation.justfile)
.map_err(|serde_json_error| Error::DumpJson { serde_json_error })?;
println!();
}
DumpFormat::Just => print!("{ast}"),
DumpFormat::Just => print!("{}", compilation.root_ast()),
}
Ok(())
}
Expand All @@ -337,13 +335,11 @@ impl Subcommand {
Ok(())
}

fn format(
config: &Config,
search: &Search,
src: &str,
ast: &Ast,
justfile: &Justfile,
) -> RunResult<'static> {
fn format(config: &Config, search: &Search, compilation: Compilation) -> RunResult<'static> {
let justfile = &compilation.justfile;
let src = compilation.root_src();
let ast = compilation.root_ast();

config.require_unstable(justfile, UnstableFeature::FormatSubcommand)?;

let formatted = ast.to_string();
Expand Down Expand Up @@ -392,20 +388,23 @@ impl Subcommand {
let search = Search::init(&config.search_config, &config.invocation_directory)?;

if search.justfile.is_file() {
Err(Error::InitExists {
return Err(Error::InitExists {
justfile: search.justfile,
})
} else if let Err(io_error) = fs::write(&search.justfile, INIT_JUSTFILE) {
Err(Error::WriteJustfile {
});
}

if let Err(io_error) = fs::write(&search.justfile, INIT_JUSTFILE) {
return Err(Error::WriteJustfile {
justfile: search.justfile,
io_error,
})
} else {
if config.verbosity.loud() {
eprintln!("Wrote justfile to `{}`", search.justfile.display());
}
Ok(())
});
}

if config.verbosity.loud() {
eprintln!("Wrote justfile to `{}`", search.justfile.display());
}

Ok(())
}

fn man() -> RunResult<'static> {
Expand Down

0 comments on commit f222b02

Please sign in to comment.