Skip to content

Commit

Permalink
Fix NodeVersion deserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
j178 committed Dec 19, 2024
1 parent 15747d0 commit de7fb18
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ tempfile = "3.13.0"
textwrap = "0.16.1"
thiserror = "1.0.64"
tokio = { version = "1.40.0", features = ["fs", "process", "rt", "sync", "macros"] }
tokio-util = "0.7.13"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
unicode-width = "0.2.0"
url = { version = "2.5.2", features = ["serde"] }
which = "6.0.3"
tokio-util = "0.7.13"

[target.'cfg(unix)'.dependencies]
libc = "0.2.164"
Expand Down
1 change: 1 addition & 0 deletions src/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ impl Hook {
/// Get the environment directory that the hook will be installed to.
pub fn environment_dir(&self) -> Option<PathBuf> {
let env_dir = self.language.environment_dir()?;
// TODO: language_version might not be a valid directory name.
Some(
self.path()
.join(format!("{}-{}", env_dir, &self.language_version)),
Expand Down
24 changes: 23 additions & 1 deletion src/languages/node/installer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Lts {
}
}

#[derive(Deserialize, Debug)]
#[derive(Debug)]
pub struct NodeVersion {
pub version: semver::Version,
pub lts: Lts,
Expand All @@ -50,6 +50,7 @@ pub struct NodeVersion {
impl FromStr for NodeVersion {
type Err = semver::Error;

/// Parse from `<version>[-<code_name>]` format.
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (ver, code_name) = if s.contains('-') {
s.split_once('-').unwrap()
Expand All @@ -66,6 +67,27 @@ impl FromStr for NodeVersion {
}
}

impl<'de> Deserialize<'de> for NodeVersion {
fn deserialize<D>(deserializer: D) -> Result<NodeVersion, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
struct _Version {
version: String,
lts: Lts,
}

let raw = _Version::deserialize(deserializer)?;
let version_str = raw.version.trim_start_matches('v');
let version = semver::Version::parse(version_str).map_err(serde::de::Error::custom)?;
Ok(NodeVersion {
version,
lts: raw.lts,
})
}
}

impl Display for NodeVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.version)?;
Expand Down
33 changes: 33 additions & 0 deletions tests/files/node-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
repos:
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.1.0
hooks:
- id: prettier
types_or: [yaml, json5]
language_version: default
- id: prettier
types_or: [yaml, json5]
language_version: system
- id: prettier
types_or: [yaml, json5]
language_version: system
- id: prettier
types_or: [yaml, json5]
# code name
language_version: jod

Check warning on line 17 in tests/files/node-hooks.yaml

View workflow job for this annotation

GitHub Actions / typos

"jod" should be "job".
- id: prettier
types_or: [yaml, json5]
# major version
language_version: '23'
- id: prettier
types_or: [yaml, json5]
# major and minor version
language_version: '23.4'
- id: prettier
types_or: [yaml, json5]
# major and minor version and patch
language_version: 23.4.0
- id: prettier
types_or: [yaml, json5]
# version requirement
language_version: ^23.4.0

0 comments on commit de7fb18

Please sign in to comment.