From 9e1b825a7db3610b1a12524b1a5b4d3a37a6a237 Mon Sep 17 00:00:00 2001 From: alxndrv <> Date: Wed, 12 Feb 2025 13:17:28 +0200 Subject: [PATCH] `lscpu`: Add support for nested fields in output --- src/uu/lscpu/src/lscpu.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/uu/lscpu/src/lscpu.rs b/src/uu/lscpu/src/lscpu.rs index 5dd313b..0bde18f 100644 --- a/src/uu/lscpu/src/lscpu.rs +++ b/src/uu/lscpu/src/lscpu.rs @@ -27,6 +27,8 @@ struct CpuInfos { struct CpuInfo { field: String, data: String, + #[serde(skip_serializing_if = "Vec::is_empty")] + children: Vec, } impl CpuInfos { @@ -36,10 +38,11 @@ impl CpuInfos { } } - fn push(&mut self, field: &str, data: &str) { + fn push(&mut self, field: &str, data: &str, children: Option>) { let cpu_info = CpuInfo { field: String::from_str(field).unwrap(), data: String::from_str(data).unwrap(), + children: children.unwrap_or_default(), }; self.lscpu.push(cpu_info); } @@ -59,8 +62,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let json = matches.get_flag(options::JSON); let mut cpu_infos = CpuInfos::new(); - cpu_infos.push("Architecture", &get_architecture()); - cpu_infos.push("CPU(s)", &format!("{}", system.cpus().len())); + cpu_infos.push("Architecture", &get_architecture(), None); + cpu_infos.push("CPU(s)", &format!("{}", system.cpus().len()), None); // Add more CPU information here... if let Ok(contents) = fs::read_to_string("/proc/cpuinfo") { @@ -70,7 +73,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { .unwrap(); // Assuming all CPUs have the same model name if let Some(cap) = re.captures_iter(&contents).next() { - cpu_infos.push("Model name", &cap[1]); + cpu_infos.push("Model name", &cap[1], None); }; }