mirror of https://github.com/Trivernis/nenv
Improve the version cache to be way faster and smaller
parent
9a7c91620e
commit
309189cb88
@ -1,49 +1,52 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use serde::{Deserialize, Deserializer, Serialize};
|
||||
use serde::{Deserialize, Deserializer};
|
||||
|
||||
/// Represents a single nodejs version info entry
|
||||
/// as retrieved from nodejs.org
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
pub struct VersionInfo {
|
||||
#[serde(deserialize_with = "deserialize_prefixed_version")]
|
||||
pub version: semver::Version,
|
||||
pub date: String,
|
||||
pub modules: Option<String>,
|
||||
|
||||
#[serde(deserialize_with = "deserialize_false_as_none")]
|
||||
pub lts: Option<String>,
|
||||
pub lts: LtsInfo,
|
||||
pub security: bool,
|
||||
#[serde(flatten)]
|
||||
pub module_versions: ModuleVersions,
|
||||
pub files: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct ModuleVersions {
|
||||
pub v8: String,
|
||||
pub npm: Option<String>,
|
||||
pub uv: Option<String>,
|
||||
pub zlib: Option<String>,
|
||||
pub openssl: Option<String>,
|
||||
pub files: Vec<String>,
|
||||
}
|
||||
|
||||
fn deserialize_false_as_none<'de, D: Deserializer<'de>>(
|
||||
deserializer: D,
|
||||
) -> Result<Option<String>, D::Error> {
|
||||
Ok(String::deserialize(deserializer).ok())
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum LtsInfo {
|
||||
Version(String),
|
||||
NotLts(bool),
|
||||
}
|
||||
|
||||
impl LtsInfo {
|
||||
pub fn lts(self) -> Option<String> {
|
||||
match self {
|
||||
LtsInfo::Version(v) => Some(v),
|
||||
LtsInfo::NotLts(_) => None,
|
||||
}
|
||||
}
|
||||
pub fn lts_ref(&self) -> Option<&String> {
|
||||
match &self {
|
||||
LtsInfo::Version(v) => Some(v),
|
||||
LtsInfo::NotLts(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_prefixed_version<'de, D: Deserializer<'de>>(
|
||||
deserializer: D,
|
||||
) -> Result<semver::Version, D::Error> {
|
||||
let version = String::deserialize(deserializer)?;
|
||||
let version = if let Some(v) = version.strip_prefix('v') {
|
||||
Cow::Borrowed(v)
|
||||
} else {
|
||||
Cow::Owned(version)
|
||||
};
|
||||
let version = semver::Version::parse(version.as_ref()).map_err(serde::de::Error::custom)?;
|
||||
let version = semver::Version::parse(version.trim_start_matches('v'))
|
||||
.map_err(serde::de::Error::custom)?;
|
||||
|
||||
Ok(version)
|
||||
}
|
||||
|
Loading…
Reference in New Issue