Skip to content

Commit

Permalink
chore: move nargo-specific functions from fm to nargo (#1003)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored Mar 18, 2023
1 parent 8a97336 commit 8dc6352
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 45 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

2 changes: 0 additions & 2 deletions crates/fm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ mod file_reader;

pub use file_map::{File, FileId, FileMap};

pub mod util;
use std::{
collections::HashMap,
path::{Path, PathBuf},
};
pub use util::*;

pub const FILE_EXTENSION: &str = "nr";

Expand Down
35 changes: 0 additions & 35 deletions crates/fm/src/util.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/nargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ iter-extended.workspace = true
noirc_driver.workspace = true
noirc_frontend.workspace = true
noirc_abi.workspace = true
fm.workspace = true
acvm.workspace = true
cfg-if.workspace = true
toml.workspace = true
Expand Down
41 changes: 36 additions & 5 deletions crates/nargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
use color_eyre as _;

use noirc_frontend::graph::CrateType;
use std::path::{Path, PathBuf};
use std::{
fs::ReadDir,
path::{Path, PathBuf},
};

use crate::errors::CliError;
// Nargo is the package manager for Noir
Expand All @@ -32,7 +35,7 @@ mod toml;
/// However, it should only do this after checking the current path
/// This allows the use of workspace settings in the future.
fn find_package_config(current_path: &Path) -> Result<PathBuf, CliError> {
match fm::find_file(current_path, "Nargo", "toml") {
match find_file(current_path, "Nargo", "toml") {
Some(p) => Ok(p),
None => Err(CliError::Generic(format!(
"cannot find a Nargo.toml in {}",
Expand All @@ -44,7 +47,7 @@ fn find_package_config(current_path: &Path) -> Result<PathBuf, CliError> {
fn lib_or_bin(current_path: &Path) -> Result<(PathBuf, CrateType), CliError> {
// A library has a lib.nr and a binary has a main.nr
// You cannot have both.
let src_path = match fm::find_dir(current_path, "src") {
let src_path = match find_dir(current_path, "src") {
Some(path) => path,
None => {
return Err(CliError::Generic(format!(
Expand All @@ -53,8 +56,8 @@ fn lib_or_bin(current_path: &Path) -> Result<(PathBuf, CrateType), CliError> {
)))
}
};
let lib_nr_path = fm::find_file(&src_path, "lib", "nr");
let bin_nr_path = fm::find_file(&src_path, "main", "nr");
let lib_nr_path = find_file(&src_path, "lib", "nr");
let bin_nr_path = find_file(&src_path, "main", "nr");
match (lib_nr_path, bin_nr_path) {
(Some(_), Some(_)) => Err(CliError::Generic(
"package cannot contain both a `lib.nr` and a `main.nr`".to_owned(),
Expand All @@ -66,3 +69,31 @@ fn lib_or_bin(current_path: &Path) -> Result<(PathBuf, CrateType), CliError> {
)),
}
}

// Looks for file named `file_name` in path
fn find_file<P: AsRef<Path>>(path: P, file_name: &str, extension: &str) -> Option<PathBuf> {
let entries = list_files_and_folders_in(path)?;
let file_name = format!("{file_name}.{extension}");

find_artifact(entries, &file_name)
}

// Looks for directory named `dir_name` in path
fn find_dir<P: AsRef<Path>>(path: P, dir_name: &str) -> Option<PathBuf> {
let entries = list_files_and_folders_in(path)?;
find_artifact(entries, dir_name)
}

// There is no distinction between files and folders
fn find_artifact(entries: ReadDir, artifact_name: &str) -> Option<PathBuf> {
let entry = entries
.into_iter()
.flatten()
.find(|entry| entry.file_name().to_str() == Some(artifact_name))?;

Some(entry.path())
}

fn list_files_and_folders_in<P: AsRef<Path>>(path: P) -> Option<ReadDir> {
std::fs::read_dir(path).ok()
}
2 changes: 1 addition & 1 deletion crates/nargo/src/workspace.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// We will say that a cargo unit must contain either a binary or a library
// Then we use workspace to allow more than one. In the future, do not allow there to be
// Then we use workspace to allow more than one. In the future, do not allow there to be
// both a binary and a library.
// - library will be default

0 comments on commit 8dc6352

Please sign in to comment.