Skip to content

Commit c994b9d

Browse files
committed
feat: ✨ implement serde for KernelVersion
1 parent eb6ee50 commit c994b9d

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

procfs-core/Cargo.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@ rust-version.workspace = true
1414

1515
[features]
1616
default = ["chrono"]
17-
serde1 = ["serde", "bitflags/serde"]
17+
serde1 = ["serde", "bitflags/serde", "serde_with"]
1818

1919
[dependencies]
2020
backtrace = { version = "0.3", optional = true }
2121
bitflags = { version = "2" }
22-
chrono = { version = "0.4.20", optional = true, features = ["clock"], default-features = false }
22+
chrono = { version = "0.4.20", optional = true, features = [
23+
"clock",
24+
], default-features = false }
2325
hex = "0.4"
2426
serde = { version = "1.0", features = ["derive"], optional = true }
27+
serde_with = { version = "3.11", optional = true }
28+
29+
[dev-dependencies]
30+
serde_json = { version = "1.0" }
2531

2632
[package.metadata.docs.rs]
2733
all-features = true

procfs-core/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@ pub struct ExplicitSystemInfo {
278278
pub ticks_per_second: u64,
279279
pub page_size: u64,
280280
pub is_little_endian: bool,
281-
#[cfg_attr(feature = "serde1", serde(skip))]
282281
pub kernel_version: KernelVersion,
283282
}
284283

procfs-core/src/sys/kernel/mod.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
//! The files in this directory can be used to tune and monitor miscellaneous
44
//! and general things in the operation of the Linux kernel.
55
6-
use std::cmp;
76
use std::collections::HashSet;
87
use std::str::FromStr;
8+
use std::{cmp, fmt::Display};
99

1010
#[cfg(feature = "serde1")]
11-
use serde::{Deserialize, Serialize};
11+
use serde_with::{DeserializeFromStr, SerializeDisplay};
1212

1313
use bitflags::bitflags;
1414

1515
use crate::{ProcError, ProcResult};
1616

1717
/// Represents a kernel version, in major.minor.release version.
1818
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Default)]
19-
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
19+
#[cfg_attr(feature = "serde1", derive(SerializeDisplay, DeserializeFromStr))]
2020
pub struct Version {
2121
pub major: u8,
2222
pub minor: u8,
@@ -103,6 +103,12 @@ impl cmp::PartialOrd for Version {
103103
}
104104
}
105105

106+
impl Display for Version {
107+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
108+
write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
109+
}
110+
}
111+
106112
/// Represents a kernel type
107113
#[derive(Debug, Clone, Eq, PartialEq)]
108114
pub struct Type {
@@ -429,4 +435,33 @@ mod tests {
429435
let a = SemaphoreLimits::from_str("1 string 500 3200");
430436
assert!(a.is_err() && a.err().unwrap() == "Failed to parse SEMMNS");
431437
}
438+
439+
#[cfg(feature = "serde1")]
440+
mod serde_kernel_version {
441+
#[test]
442+
fn should_serialize_kernel_version() {
443+
let version = Version {
444+
major: 42,
445+
minor: 0,
446+
patch: 1,
447+
};
448+
let version = serde_json::to_string(&version).unwrap();
449+
450+
// NOTE: The double quote is necessary because of the JSON format.
451+
assert_eq!(r#""42.0.1""#, &version);
452+
}
453+
454+
#[test]
455+
fn should_deserialize_kernel_version() {
456+
let expected = Version {
457+
major: 21,
458+
minor: 0,
459+
patch: 2,
460+
};
461+
// NOTE: The double quote is necessary because of the JSON format.
462+
let version: Version = serde_json::from_str(r#""21.0.2""#).unwrap();
463+
464+
assert_eq!(version, expected);
465+
}
466+
}
432467
}

0 commit comments

Comments
 (0)