mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-20 20:48:43 +00:00
Merge pull request #222 from NobodyXu/refactor
Refactor: Make appending to metafiles easier and fix their err handling
This commit is contained in:
commit
adbc587f3b
4 changed files with 78 additions and 37 deletions
|
@ -108,29 +108,21 @@ async fn install_from_package(
|
||||||
|
|
||||||
let bins: BTreeSet<String> = bin_files.into_iter().map(|bin| bin.base_name).collect();
|
let bins: BTreeSet<String> = bin_files.into_iter().map(|bin| bin.base_name).collect();
|
||||||
|
|
||||||
{
|
debug!("Writing .crates.toml");
|
||||||
debug!("Writing .crates.toml");
|
metafiles::v1::CratesToml::append(&cvs, bins.clone())?;
|
||||||
let mut c1 = metafiles::v1::CratesToml::load().unwrap_or_default();
|
|
||||||
c1.insert(cvs.clone(), bins.clone());
|
|
||||||
c1.write()?;
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
debug!("Writing .crates2.json");
|
||||||
debug!("Writing .crates2.json");
|
metafiles::v2::Crates2Json::append(
|
||||||
let mut c2 = metafiles::v2::Crates2Json::load().unwrap_or_default();
|
&cvs,
|
||||||
c2.insert(
|
metafiles::v2::CrateInfo {
|
||||||
cvs,
|
version_req: Some(version),
|
||||||
metafiles::v2::CrateInfo {
|
bins,
|
||||||
version_req: Some(version),
|
profile: "release".into(),
|
||||||
bins,
|
target: fetcher.target().to_string(),
|
||||||
profile: "release".into(),
|
rustc: format!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")),
|
||||||
target: fetcher.target().to_string(),
|
..Default::default()
|
||||||
rustc: format!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")),
|
},
|
||||||
..Default::default()
|
)?;
|
||||||
},
|
|
||||||
);
|
|
||||||
c2.write()?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{fmt, str::FromStr};
|
use std::{borrow::Cow, fmt, str::FromStr};
|
||||||
|
|
||||||
use miette::Diagnostic;
|
use miette::Diagnostic;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
|
@ -116,6 +116,7 @@ impl<'de> Deserialize<'de> for CrateVersionSource {
|
||||||
where
|
where
|
||||||
D: Deserializer<'de>,
|
D: Deserializer<'de>,
|
||||||
{
|
{
|
||||||
Self::from_str(&String::deserialize(deserializer)?).map_err(serde::de::Error::custom)
|
let s = Cow::<'_, str>::deserialize(deserializer)?;
|
||||||
|
Self::from_str(&s).map_err(serde::de::Error::custom)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{
|
use std::{
|
||||||
collections::{BTreeMap, BTreeSet},
|
collections::{BTreeMap, BTreeSet},
|
||||||
fs,
|
fs, io,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
@ -14,7 +14,7 @@ use crate::cargo_home;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
||||||
pub struct CratesToml {
|
pub struct CratesToml {
|
||||||
v1: BTreeMap<CrateVersionSource, BTreeSet<String>>,
|
v1: BTreeMap<String, BTreeSet<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CratesToml {
|
impl CratesToml {
|
||||||
|
@ -31,8 +31,8 @@ impl CratesToml {
|
||||||
Self::from_str(&file)
|
Self::from_str(&file)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert(&mut self, cvs: CrateVersionSource, bins: BTreeSet<String>) {
|
pub fn insert(&mut self, cvs: &CrateVersionSource, bins: BTreeSet<String>) {
|
||||||
self.v1.insert(cvs, bins);
|
self.v1.insert(cvs.to_string(), bins);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write(&self) -> Result<(), CratesTomlParseError> {
|
pub fn write(&self) -> Result<(), CratesTomlParseError> {
|
||||||
|
@ -43,6 +43,31 @@ impl CratesToml {
|
||||||
fs::write(path, &toml::to_vec(&self)?)?;
|
fs::write(path, &toml::to_vec(&self)?)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn append_to_path(
|
||||||
|
path: impl AsRef<Path>,
|
||||||
|
cvs: &CrateVersionSource,
|
||||||
|
bins: BTreeSet<String>,
|
||||||
|
) -> Result<(), CratesTomlParseError> {
|
||||||
|
let mut c1 = match Self::load_from_path(path.as_ref()) {
|
||||||
|
Ok(c1) => c1,
|
||||||
|
Err(CratesTomlParseError::Io(io_err)) if io_err.kind() == io::ErrorKind::NotFound => {
|
||||||
|
Self::default()
|
||||||
|
}
|
||||||
|
Err(err) => return Err(err),
|
||||||
|
};
|
||||||
|
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 {
|
impl FromStr for CratesToml {
|
||||||
|
@ -55,7 +80,7 @@ impl FromStr for CratesToml {
|
||||||
#[derive(Debug, Diagnostic, Error)]
|
#[derive(Debug, Diagnostic, Error)]
|
||||||
pub enum CratesTomlParseError {
|
pub enum CratesTomlParseError {
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Io(#[from] std::io::Error),
|
Io(#[from] io::Error),
|
||||||
|
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
TomlParse(#[from] toml::de::Error),
|
TomlParse(#[from] toml::de::Error),
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{
|
use std::{
|
||||||
collections::{BTreeMap, BTreeSet},
|
collections::{BTreeMap, BTreeSet},
|
||||||
fs,
|
fs, io,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ use crate::cargo_home;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
||||||
pub struct Crates2Json {
|
pub struct Crates2Json {
|
||||||
pub installs: BTreeMap<CrateVersionSource, CrateInfo>,
|
pub installs: BTreeMap<String, CrateInfo>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
||||||
|
@ -48,12 +48,12 @@ impl Crates2Json {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_from_path(path: impl AsRef<Path>) -> Result<Self, Crates2JsonParseError> {
|
pub fn load_from_path(path: impl AsRef<Path>) -> Result<Self, Crates2JsonParseError> {
|
||||||
let file = fs::read_to_string(path)?;
|
let file = fs::File::open(path.as_ref())?;
|
||||||
Ok(serde_json::from_str(&file)?)
|
Ok(serde_json::from_reader(file)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert(&mut self, cvs: CrateVersionSource, info: CrateInfo) {
|
pub fn insert(&mut self, cvs: &CrateVersionSource, info: CrateInfo) {
|
||||||
self.installs.insert(cvs, info);
|
self.installs.insert(cvs.to_string(), info);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write(&self) -> Result<(), Crates2JsonParseError> {
|
pub fn write(&self) -> Result<(), Crates2JsonParseError> {
|
||||||
|
@ -61,15 +61,38 @@ impl Crates2Json {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_to_path(&self, path: impl AsRef<Path>) -> Result<(), Crates2JsonParseError> {
|
pub fn write_to_path(&self, path: impl AsRef<Path>) -> Result<(), Crates2JsonParseError> {
|
||||||
fs::write(path, &serde_json::to_vec(&self)?)?;
|
let file = fs::File::create(path.as_ref())?;
|
||||||
|
serde_json::to_writer(file, &self)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn append_to_path(
|
||||||
|
path: impl AsRef<Path>,
|
||||||
|
cvs: &CrateVersionSource,
|
||||||
|
info: CrateInfo,
|
||||||
|
) -> Result<(), Crates2JsonParseError> {
|
||||||
|
let mut c2 = match Self::load_from_path(path.as_ref()) {
|
||||||
|
Ok(c2) => c2,
|
||||||
|
Err(Crates2JsonParseError::Io(io_err)) if io_err.kind() == io::ErrorKind::NotFound => {
|
||||||
|
Self::default()
|
||||||
|
}
|
||||||
|
Err(err) => return Err(err),
|
||||||
|
};
|
||||||
|
c2.insert(cvs, info);
|
||||||
|
c2.write_to_path(path.as_ref())?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn append(cvs: &CrateVersionSource, info: CrateInfo) -> Result<(), Crates2JsonParseError> {
|
||||||
|
Self::append_to_path(Self::default_path()?, cvs, info)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Diagnostic, Error)]
|
#[derive(Debug, Diagnostic, Error)]
|
||||||
pub enum Crates2JsonParseError {
|
pub enum Crates2JsonParseError {
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Io(#[from] std::io::Error),
|
Io(#[from] io::Error),
|
||||||
|
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Json(#[from] serde_json::Error),
|
Json(#[from] serde_json::Error),
|
||||||
|
|
Loading…
Add table
Reference in a new issue