Skip to content

Commit

Permalink
specify path to store the collection of spec files
Browse files Browse the repository at this point in the history
  • Loading branch information
toidiu committed Sep 22, 2023
1 parent 5ed1e4e commit 9c93f00
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ pub struct Extract {

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

if self.out.extension().is_some() {
// assume a path with an extension is a single file
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 spec files are stored in a folder called `specs`. This folder is
/// stored at the root of the project by default. Use this argument to
/// specify a different location.
#[structopt(long = "spec-download-path", default_value = ".")]
pub spec_download_path: 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
5 changes: 4 additions & 1 deletion src/report/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ impl Report {
let contents: HashMap<_, _> = targets
.par_iter()
.map(|target| {
let contents = target.path.load().unwrap();
let contents = target
.path
.load(Some(&self.project.spec_download_path))
.unwrap();
(target, contents)
})
.collect();
Expand Down
9 changes: 6 additions & 3 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<&String>) -> 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,13 @@ impl TargetPath {
Ok(contents)
}

pub fn local(&self) -> PathBuf {
pub fn local(&self, spec_download_path: Option<&String>) -> PathBuf {
match self {
Self::Url(url) => {
let mut path = std::env::current_dir().unwrap();
if let Some(path_to_spec) = spec_download_path {
path.push(path_to_spec);
}
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

0 comments on commit 9c93f00

Please sign in to comment.