From 49db33c73b378c673688b0c658a540f387613916 Mon Sep 17 00:00:00 2001 From: ejortega <24722023+ejortega@users.noreply.github.com> Date: Fri, 21 Jul 2023 15:10:13 -0500 Subject: [PATCH] refactor: add IntoIter for ParsedLockfile --- cli/src/commands/jobs.rs | 8 ++------ cli/src/commands/parse.rs | 33 +++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/cli/src/commands/jobs.rs b/cli/src/commands/jobs.rs index f609fedc2..a92fe6406 100644 --- a/cli/src/commands/jobs.rs +++ b/cli/src/commands/jobs.rs @@ -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}; @@ -118,9 +116,7 @@ pub async fn handle_submission(api: &mut PhylumApi, matches: &clap::ArgMatches) ); } - let parsed_packages: Vec = res.into(); - - packages.extend(parsed_packages); + packages.extend(res.into_iter()); } if let Some(base) = matches.get_one::("base") { diff --git a/cli/src/commands/parse.rs b/cli/src/commands/parse.rs index 8afb390dd..dc1195638 100644 --- a/cli/src/commands/parse.rs +++ b/cli/src/commands/parse.rs @@ -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}; @@ -17,16 +18,25 @@ pub struct ParsedLockfile { pub packages: Vec, } -impl From for Vec { - 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, +} + +impl Iterator for ParsedLockfileIterator { + type Item = PackageDescriptorAndLockfilePath; + + fn next(&mut self) -> Option { + 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() } } } @@ -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 = parsed_lockfile.into(); - pkgs.extend(parsed_packages); + pkgs.extend(parsed_lockfile.into_iter()); } serde_json::to_writer_pretty(&mut io::stdout(), &pkgs)?;