#![allow(deprecated)]
use crate::{
graph::DependencyGraph,
manifest::{overrides::OverrideKey, target::TargetKind},
names::PackageName,
source::specifiers::DependencySpecifiers,
};
use relative_path::RelativePathBuf;
use semver::Version;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
pub const CURRENT_FORMAT: usize = 2;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Lockfile {
pub name: PackageName,
pub version: Version,
pub target: TargetKind,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub overrides: BTreeMap<OverrideKey, DependencySpecifiers>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub workspace: BTreeMap<PackageName, BTreeMap<TargetKind, RelativePathBuf>>,
#[serde(default, skip_serializing_if = "DependencyGraph::is_empty")]
pub graph: DependencyGraph,
}
pub fn parse_lockfile(lockfile: &str) -> Result<Lockfile, errors::ParseLockfileError> {
#[derive(Serialize, Deserialize, Debug)]
pub struct LockfileFormat {
#[serde(default)]
pub format: usize,
}
let format: LockfileFormat = toml::de::from_str(lockfile)?;
let format = format.format;
match format {
CURRENT_FORMAT => toml::de::from_str(lockfile).map_err(Into::into),
format if format < CURRENT_FORMAT => Err(errors::ParseLockfileError::TooOld(format)),
format => Err(errors::ParseLockfileError::TooNew(format)),
}
}
pub mod errors {
use thiserror::Error;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ParseLockfileError {
#[error("lockfile format {} is too new. newest supported format: {}", .0, super::CURRENT_FORMAT)]
TooNew(usize),
#[error("lockfile format {} is too old. manual deletion is required. current format: {}", .0, super::CURRENT_FORMAT)]
TooOld(usize),
#[error("deserializing the lockfile failed")]
De(#[from] toml::de::Error),
}
}