Skip to content

Commit

Permalink
feat: ✨ implement serde for KernelVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
dergecko committed Oct 28, 2024
1 parent eb6ee50 commit c994b9d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
10 changes: 8 additions & 2 deletions procfs-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ rust-version.workspace = true

[features]
default = ["chrono"]
serde1 = ["serde", "bitflags/serde"]
serde1 = ["serde", "bitflags/serde", "serde_with"]

[dependencies]
backtrace = { version = "0.3", optional = true }
bitflags = { version = "2" }
chrono = { version = "0.4.20", optional = true, features = ["clock"], default-features = false }
chrono = { version = "0.4.20", optional = true, features = [
"clock",
], default-features = false }
hex = "0.4"
serde = { version = "1.0", features = ["derive"], optional = true }
serde_with = { version = "3.11", optional = true }

[dev-dependencies]
serde_json = { version = "1.0" }

[package.metadata.docs.rs]
all-features = true
1 change: 0 additions & 1 deletion procfs-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ pub struct ExplicitSystemInfo {
pub ticks_per_second: u64,
pub page_size: u64,
pub is_little_endian: bool,
#[cfg_attr(feature = "serde1", serde(skip))]
pub kernel_version: KernelVersion,
}

Expand Down
41 changes: 38 additions & 3 deletions procfs-core/src/sys/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
//! The files in this directory can be used to tune and monitor miscellaneous
//! and general things in the operation of the Linux kernel.

use std::cmp;
use std::collections::HashSet;
use std::str::FromStr;
use std::{cmp, fmt::Display};

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

use bitflags::bitflags;

use crate::{ProcError, ProcResult};

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

impl Display for Version {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
}
}

/// Represents a kernel type
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Type {
Expand Down Expand Up @@ -429,4 +435,33 @@ mod tests {
let a = SemaphoreLimits::from_str("1 string 500 3200");
assert!(a.is_err() && a.err().unwrap() == "Failed to parse SEMMNS");
}

#[cfg(feature = "serde1")]
mod serde_kernel_version {
#[test]
fn should_serialize_kernel_version() {
let version = Version {
major: 42,
minor: 0,
patch: 1,
};
let version = serde_json::to_string(&version).unwrap();

// NOTE: The double quote is necessary because of the JSON format.
assert_eq!(r#""42.0.1""#, &version);
}

#[test]
fn should_deserialize_kernel_version() {
let expected = Version {
major: 21,
minor: 0,
patch: 2,
};
// NOTE: The double quote is necessary because of the JSON format.
let version: Version = serde_json::from_str(r#""21.0.2""#).unwrap();

assert_eq!(version, expected);
}
}
}

0 comments on commit c994b9d

Please sign in to comment.