Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

specify path to the spec files #118

Merged
merged 3 commits into from
Sep 25, 2023
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
12 changes: 10 additions & 2 deletions src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,23 @@ pub struct Extract {
#[structopt(short, long, default_value = ".")]
out: PathBuf,

/// Path to store the collection of spec files
///
/// The collection of spec files are stored in a folder called `specs`. The
/// `specs` folder is stored in the current directory by default. Use this
/// argument to override the default location.
#[structopt(long = "spec-path")]
pub spec_path: Option<String>,

target: TargetPath,
}

impl Extract {
pub fn exec(&self) -> Result<(), Error> {
let contents = self.target.load()?;
let contents = self.target.load(self.spec_path.as_deref())?;
let spec = self.format.parse(&contents)?;
let sections = extract_sections(&spec);
let local_path = self.target.local();
let local_path = self.target.local(self.spec_path.as_deref());

if self.out.extension().is_some() {
// assume a path with an extension is a single file
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fn main() {
}
}

#[allow(clippy::large_enum_variant)]
#[derive(Debug, StructOpt)]
enum Arguments {
Extract(extract::Extract),
Expand Down
8 changes: 8 additions & 0 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ pub struct Project {
/// Glob patterns for spec files
#[structopt(long = "spec-pattern")]
spec_patterns: Vec<String>,

/// Path to store the collection of spec files
///
/// The collection of spec files are stored in a folder called `specs`. The
/// `specs` folder is stored in the current directory by default. Use this
/// argument to override the default location.
#[structopt(long = "spec-path")]
pub spec_path: Option<String>,
}

impl Project {
Expand Down
7 changes: 2 additions & 5 deletions src/report/lcov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ pub fn report(report: &ReportResult, dir: &Path) -> Result<(), Error> {
}

#[allow(clippy::cognitive_complexity)]
pub fn report_source<Output: Write>(
report: &TargetReport,
output: &mut Output,
) -> Result<(), Error> {
fn report_source<Output: Write>(report: &TargetReport, output: &mut Output) -> Result<(), Error> {
macro_rules! put {
($($arg:expr),* $(,)?) => {
writeln!(output $(, $arg)*)?;
Expand All @@ -73,7 +70,7 @@ pub fn report_source<Output: Write>(

put!("TN:Compliance");
let relative =
pathdiff::diff_paths(report.target.path.local(), std::env::current_dir()?).unwrap();
pathdiff::diff_paths(report.target.path.local(None), std::env::current_dir()?).unwrap();
put!("SF:{}", relative.display());

// record all sections
Expand Down
2 changes: 1 addition & 1 deletion src/report/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Report {
let contents: HashMap<_, _> = targets
.par_iter()
.map(|target| {
let contents = target.path.load().unwrap();
let contents = target.path.load(self.project.spec_path.as_deref()).unwrap();
(target, contents)
})
.collect();
Expand Down
12 changes: 8 additions & 4 deletions src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ impl TargetPath {
Ok(Self::Path(path))
}

pub fn load(&self) -> Result<String, Error> {
pub fn load(&self, spec_download_path: Option<&str>) -> Result<String, Error> {
let mut contents = match self {
Self::Url(url) => {
let path = self.local();
let path = self.local(spec_download_path);
if !path.exists() {
std::fs::create_dir_all(path.parent().unwrap())?;

Expand Down Expand Up @@ -103,10 +103,14 @@ impl TargetPath {
Ok(contents)
}

pub fn local(&self) -> PathBuf {
pub fn local(&self, spec_download_path: Option<&str>) -> PathBuf {
match self {
Self::Url(url) => {
let mut path = std::env::current_dir().unwrap();
let mut path = if let Some(path_to_spec) = spec_download_path {
PathBuf::from_str(path_to_spec).unwrap()
} else {
std::env::current_dir().unwrap()
};
path.push("specs");
path.push(url.host_str().expect("url should have host"));
path.extend(url.path_segments().expect("url should have path"));
Expand Down
Loading