Refactor: Extract CratesToml::append{_to_path}

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-21 19:50:23 +10:00
parent ef72f851f7
commit 31d9716d28
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
2 changed files with 21 additions and 6 deletions

View file

@ -42,6 +42,25 @@ impl CratesToml {
fs::write(path, &toml::to_vec(&self)?)?;
Ok(())
}
pub fn append_to_path(
path: impl AsRef<Path>,
cvs: CrateVersionSource,
bins: BTreeSet<String>,
) -> Result<(), CratesTomlParseError> {
let mut c1 = Self::load_from_path(path.as_ref()).unwrap_or_default();
c1.insert(cvs, bins);
c1.write_to_path(path.as_ref())?;
Ok(())
}
pub fn append(
cvs: CrateVersionSource,
bins: BTreeSet<String>,
) -> Result<(), CratesTomlParseError> {
Self::append_to_path(Self::default_path()?, cvs, bins)
}
}
impl FromStr for CratesToml {