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};
|
||||||
|
|
||||||
use serde::{Deserialize, Deserializer, Serialize};
|
|
||||||
|
|
||||||
/// Represents a single nodejs version info entry
|
/// Represents a single nodejs version info entry
|
||||||
/// as retrieved from nodejs.org
|
/// as retrieved from nodejs.org
|
||||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
pub struct VersionInfo {
|
pub struct VersionInfo {
|
||||||
#[serde(deserialize_with = "deserialize_prefixed_version")]
|
#[serde(deserialize_with = "deserialize_prefixed_version")]
|
||||||
pub version: semver::Version,
|
pub version: semver::Version,
|
||||||
pub date: String,
|
pub date: String,
|
||||||
pub modules: Option<String>,
|
pub modules: Option<String>,
|
||||||
|
|
||||||
#[serde(deserialize_with = "deserialize_false_as_none")]
|
pub lts: LtsInfo,
|
||||||
pub lts: Option<String>,
|
|
||||||
pub security: bool,
|
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 v8: String,
|
||||||
pub npm: Option<String>,
|
pub npm: Option<String>,
|
||||||
pub uv: Option<String>,
|
pub uv: Option<String>,
|
||||||
pub zlib: Option<String>,
|
pub zlib: Option<String>,
|
||||||
pub openssl: Option<String>,
|
pub openssl: Option<String>,
|
||||||
|
pub files: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_false_as_none<'de, D: Deserializer<'de>>(
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
deserializer: D,
|
#[serde(untagged)]
|
||||||
) -> Result<Option<String>, D::Error> {
|
pub enum LtsInfo {
|
||||||
Ok(String::deserialize(deserializer).ok())
|
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>>(
|
fn deserialize_prefixed_version<'de, D: Deserializer<'de>>(
|
||||||
deserializer: D,
|
deserializer: D,
|
||||||
) -> Result<semver::Version, D::Error> {
|
) -> Result<semver::Version, D::Error> {
|
||||||
let version = String::deserialize(deserializer)?;
|
let version = String::deserialize(deserializer)?;
|
||||||
let version = if let Some(v) = version.strip_prefix('v') {
|
let version = semver::Version::parse(version.trim_start_matches('v'))
|
||||||
Cow::Borrowed(v)
|
.map_err(serde::de::Error::custom)?;
|
||||||
} else {
|
|
||||||
Cow::Owned(version)
|
|
||||||
};
|
|
||||||
let version = semver::Version::parse(version.as_ref()).map_err(serde::de::Error::custom)?;
|
|
||||||
|
|
||||||
Ok(version)
|
Ok(version)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue