use crate::{
error::{Error, ErrorKind},
version::VersionReq,
vulnerability::Vulnerability,
};
use std::path::Path;
pub struct Fixer {
manifest: cargo_edit::LocalManifest,
}
impl Fixer {
pub fn new(cargo_toml: impl AsRef<Path>) -> Result<Self, Error> {
let manifest = cargo_edit::LocalManifest::try_new(cargo_toml.as_ref())?;
Ok(Self { manifest })
}
pub fn fix(
&mut self,
vulnerability: &Vulnerability,
dry_run: bool,
) -> Result<VersionReq, Error> {
let version_req = match vulnerability.versions.patched.get(0) {
Some(req) => req,
None => fail!(ErrorKind::Version, "no fixed version available"),
};
let dependency = cargo_edit::Dependency::new(vulnerability.package.name.as_str())
.set_version(&version_req.to_string());
self.manifest.upgrade(&dependency, dry_run, false)?;
Ok(version_req.clone())
}
}