Skip to content

Commit

Permalink
refactor: add IntoIter for ParsedLockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
ejortega committed Jul 21, 2023
1 parent f801b62 commit 49db33c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
8 changes: 2 additions & 6 deletions cli/src/commands/jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use console::style;
use log::debug;
use phylum_project::LockfileConfig;
use phylum_types::types::common::{JobId, ProjectId};
use phylum_types::types::package::{
PackageDescriptor, PackageDescriptorAndLockfilePath, PackageType,
};
use phylum_types::types::package::{PackageDescriptor, PackageType};
use reqwest::StatusCode;
#[cfg(feature = "vulnreach")]
use vulnreach_types::{Job, JobPackage};
Expand Down Expand Up @@ -118,9 +116,7 @@ pub async fn handle_submission(api: &mut PhylumApi, matches: &clap::ArgMatches)
);
}

let parsed_packages: Vec<PackageDescriptorAndLockfilePath> = res.into();

packages.extend(parsed_packages);
packages.extend(res.into_iter());
}

if let Some(base) = matches.get_one::<String>("base") {
Expand Down
33 changes: 21 additions & 12 deletions cli/src/commands/parse.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! `phylum parse` command for lockfile parsing

use std::path::{Path, PathBuf};
use std::vec::IntoIter;
use std::{fs, io};

use anyhow::{anyhow, Context, Result};
Expand All @@ -17,16 +18,25 @@ pub struct ParsedLockfile {
pub packages: Vec<PackageDescriptor>,
}

impl From<ParsedLockfile> for Vec<PackageDescriptorAndLockfilePath> {
fn from(value: ParsedLockfile) -> Self {
let mut packages = Vec::with_capacity(value.packages.len());
for pkg_descriptor in value.packages {
packages.push(PackageDescriptorAndLockfilePath {
package_descriptor: pkg_descriptor,
lockfile_path: Some(value.path.to_string_lossy().into_owned()),
})
}
packages
pub struct ParsedLockfileIterator {
path: PathBuf,
packages: IntoIter<PackageDescriptor>,
}

impl Iterator for ParsedLockfileIterator {
type Item = PackageDescriptorAndLockfilePath;

fn next(&mut self) -> Option<Self::Item> {
self.packages.next().map(|package_descriptor| PackageDescriptorAndLockfilePath {
package_descriptor,
lockfile_path: Some(self.path.to_string_lossy().into_owned()),
})
}
}

impl ParsedLockfile {
pub fn into_iter(self) -> ParsedLockfileIterator {
ParsedLockfileIterator { path: self.path, packages: self.packages.into_iter() }
}
}

Expand All @@ -48,8 +58,7 @@ pub fn handle_parse(matches: &clap::ArgMatches) -> CommandResult {

for lockfile in lockfiles {
let parsed_lockfile = parse_lockfile(lockfile.path, Some(&lockfile.lockfile_type))?;
let parsed_packages: Vec<PackageDescriptorAndLockfilePath> = parsed_lockfile.into();
pkgs.extend(parsed_packages);
pkgs.extend(parsed_lockfile.into_iter());
}

serde_json::to_writer_pretty(&mut io::stdout(), &pkgs)?;
Expand Down

0 comments on commit 49db33c

Please sign in to comment.