1010//! but some fields may be missing on some platforms. That's life in embedded.
1111
1212use crate :: cli:: GlobalOptions ;
13+ use crate :: fields:: { cpu as f, to_text_key} ;
1314use crate :: io;
1415use crate :: json:: { begin_kv_output, JsonWriter } ;
1516use std:: collections:: HashSet ;
@@ -168,51 +169,51 @@ impl CpuInfo {
168169 pub fn print_text ( & self , verbose : bool ) {
169170 let mut parts = Vec :: new ( ) ;
170171
171- parts. push ( format ! ( "LOGICAL_CPUS ={}" , self . logical_cpus) ) ;
172+ parts. push ( format ! ( "{} ={}" , to_text_key ( f :: LOGICAL_CPUS ) , self . logical_cpus) ) ;
172173
173174 if let Some ( ref name) = self . model_name {
174175 // Quote model name since it often has spaces
175- parts. push ( format ! ( "MODEL_NAME =\" {}\" " , name) ) ;
176+ parts. push ( format ! ( "{} =\" {}\" " , to_text_key ( f :: MODEL_NAME ) , name) ) ;
176177 }
177178
178179 if let Some ( ref vendor) = self . vendor_id {
179- parts. push ( format ! ( "VENDOR_ID ={}" , vendor) ) ;
180+ parts. push ( format ! ( "{} ={}" , to_text_key ( f :: VENDOR_ID ) , vendor) ) ;
180181 }
181182
182183 if let Some ( sockets) = self . sockets {
183- parts. push ( format ! ( "SOCKETS ={}" , sockets) ) ;
184+ parts. push ( format ! ( "{} ={}" , to_text_key ( f :: SOCKETS ) , sockets) ) ;
184185 }
185186
186187 if let Some ( cores) = self . cores_per_socket {
187- parts. push ( format ! ( "CORES_PER_SOCKET ={}" , cores) ) ;
188+ parts. push ( format ! ( "{} ={}" , to_text_key ( f :: CORES_PER_SOCKET ) , cores) ) ;
188189 }
189190
190191 // RISC-V specific fields (always show if present, very informative)
191192 if let Some ( ref isa) = self . isa {
192- parts. push ( format ! ( "ISA ={}" , isa) ) ;
193+ parts. push ( format ! ( "{} ={}" , to_text_key ( f :: ISA ) , isa) ) ;
193194 }
194195 if let Some ( ref mmu) = self . mmu {
195- parts. push ( format ! ( "MMU ={}" , mmu) ) ;
196+ parts. push ( format ! ( "{} ={}" , to_text_key ( f :: MMU ) , mmu) ) ;
196197 }
197198
198199 if verbose {
199200 if let Some ( family) = self . cpu_family {
200- parts. push ( format ! ( "CPU_FAMILY ={}" , family) ) ;
201+ parts. push ( format ! ( "{} ={}" , to_text_key ( f :: CPU_FAMILY ) , family) ) ;
201202 }
202203 if let Some ( model) = self . model {
203- parts. push ( format ! ( "MODEL ={}" , model) ) ;
204+ parts. push ( format ! ( "{} ={}" , to_text_key ( f :: MODEL ) , model) ) ;
204205 }
205206 if let Some ( stepping) = self . stepping {
206- parts. push ( format ! ( "STEPPING ={}" , stepping) ) ;
207+ parts. push ( format ! ( "{} ={}" , to_text_key ( f :: STEPPING ) , stepping) ) ;
207208 }
208209 if let Some ( mhz) = self . cpu_mhz {
209- parts. push ( format ! ( "CPU_MHZ ={:.2}" , mhz) ) ;
210+ parts. push ( format ! ( "{} ={:.2}" , to_text_key ( f :: CPU_MHZ ) , mhz) ) ;
210211 }
211212 if let Some ( ref cache) = self . cache_size {
212- parts. push ( format ! ( "CACHE_SIZE =\" {}\" " , cache) ) ;
213+ parts. push ( format ! ( "{} =\" {}\" " , to_text_key ( f :: CACHE_SIZE ) , cache) ) ;
213214 }
214215 if let Some ( ref arch) = self . architecture {
215- parts. push ( format ! ( "ARCHITECTURE ={}" , arch) ) ;
216+ parts. push ( format ! ( "{} ={}" , to_text_key ( f :: ARCHITECTURE ) , arch) ) ;
216217 }
217218 }
218219
@@ -225,26 +226,26 @@ impl CpuInfo {
225226
226227 w. field_object ( "data" ) ;
227228
228- w. field_u64 ( "logical_cpus" , self . logical_cpus as u64 ) ;
229- w. field_str_opt ( "model_name" , self . model_name . as_deref ( ) ) ;
230- w. field_str_opt ( "vendor_id" , self . vendor_id . as_deref ( ) ) ;
231- w. field_u64_opt ( "sockets" , self . sockets . map ( |v| v as u64 ) ) ;
232- w. field_u64_opt ( "cores_per_socket" , self . cores_per_socket . map ( |v| v as u64 ) ) ;
229+ w. field_u64 ( f :: LOGICAL_CPUS , self . logical_cpus as u64 ) ;
230+ w. field_str_opt ( f :: MODEL_NAME , self . model_name . as_deref ( ) ) ;
231+ w. field_str_opt ( f :: VENDOR_ID , self . vendor_id . as_deref ( ) ) ;
232+ w. field_u64_opt ( f :: SOCKETS , self . sockets . map ( |v| v as u64 ) ) ;
233+ w. field_u64_opt ( f :: CORES_PER_SOCKET , self . cores_per_socket . map ( |v| v as u64 ) ) ;
233234 // RISC-V specific
234- w. field_str_opt ( "isa" , self . isa . as_deref ( ) ) ;
235- w. field_str_opt ( "mmu" , self . mmu . as_deref ( ) ) ;
235+ w. field_str_opt ( f :: ISA , self . isa . as_deref ( ) ) ;
236+ w. field_str_opt ( f :: MMU , self . mmu . as_deref ( ) ) ;
236237
237238 if verbose {
238- w. field_u64_opt ( "cpu_family" , self . cpu_family . map ( |v| v as u64 ) ) ;
239- w. field_u64_opt ( "model" , self . model . map ( |v| v as u64 ) ) ;
240- w. field_u64_opt ( "stepping" , self . stepping . map ( |v| v as u64 ) ) ;
239+ w. field_u64_opt ( f :: CPU_FAMILY , self . cpu_family . map ( |v| v as u64 ) ) ;
240+ w. field_u64_opt ( f :: MODEL , self . model . map ( |v| v as u64 ) ) ;
241+ w. field_u64_opt ( f :: STEPPING , self . stepping . map ( |v| v as u64 ) ) ;
241242 // For MHz we'll use string to preserve precision
242243 if let Some ( mhz) = self . cpu_mhz {
243- w. field_str ( "cpu_mhz" , & format ! ( "{:.2}" , mhz) ) ;
244+ w. field_str ( f :: CPU_MHZ , & format ! ( "{:.2}" , mhz) ) ;
244245 }
245- w. field_str_opt ( "cache_size" , self . cache_size . as_deref ( ) ) ;
246- w. field_str_opt ( "architecture" , self . architecture . as_deref ( ) ) ;
247- w. field_str_opt ( "flags" , self . flags . as_deref ( ) ) ;
246+ w. field_str_opt ( f :: CACHE_SIZE , self . cache_size . as_deref ( ) ) ;
247+ w. field_str_opt ( f :: ARCHITECTURE , self . architecture . as_deref ( ) ) ;
248+ w. field_str_opt ( f :: FLAGS , self . flags . as_deref ( ) ) ;
248249 }
249250
250251 w. end_field_object ( ) ;
@@ -332,24 +333,24 @@ pub fn collect(_verbose: bool) -> Option<CpuInfo> {
332333pub fn write_json ( w : & mut JsonWriter , info : & CpuInfo , verbose : bool ) {
333334 w. field_object ( "cpu" ) ;
334335
335- w. field_u64 ( "logical_cpus" , info. logical_cpus as u64 ) ;
336- w. field_str_opt ( "model_name" , info. model_name . as_deref ( ) ) ;
337- w. field_str_opt ( "vendor_id" , info. vendor_id . as_deref ( ) ) ;
338- w. field_u64_opt ( "sockets" , info. sockets . map ( |v| v as u64 ) ) ;
339- w. field_u64_opt ( "cores_per_socket" , info. cores_per_socket . map ( |v| v as u64 ) ) ;
336+ w. field_u64 ( f :: LOGICAL_CPUS , info. logical_cpus as u64 ) ;
337+ w. field_str_opt ( f :: MODEL_NAME , info. model_name . as_deref ( ) ) ;
338+ w. field_str_opt ( f :: VENDOR_ID , info. vendor_id . as_deref ( ) ) ;
339+ w. field_u64_opt ( f :: SOCKETS , info. sockets . map ( |v| v as u64 ) ) ;
340+ w. field_u64_opt ( f :: CORES_PER_SOCKET , info. cores_per_socket . map ( |v| v as u64 ) ) ;
340341 // RISC-V specific
341- w. field_str_opt ( "isa" , info. isa . as_deref ( ) ) ;
342- w. field_str_opt ( "mmu" , info. mmu . as_deref ( ) ) ;
342+ w. field_str_opt ( f :: ISA , info. isa . as_deref ( ) ) ;
343+ w. field_str_opt ( f :: MMU , info. mmu . as_deref ( ) ) ;
343344
344345 if verbose {
345- w. field_u64_opt ( "cpu_family" , info. cpu_family . map ( |v| v as u64 ) ) ;
346- w. field_u64_opt ( "model" , info. model . map ( |v| v as u64 ) ) ;
347- w. field_u64_opt ( "stepping" , info. stepping . map ( |v| v as u64 ) ) ;
346+ w. field_u64_opt ( f :: CPU_FAMILY , info. cpu_family . map ( |v| v as u64 ) ) ;
347+ w. field_u64_opt ( f :: MODEL , info. model . map ( |v| v as u64 ) ) ;
348+ w. field_u64_opt ( f :: STEPPING , info. stepping . map ( |v| v as u64 ) ) ;
348349 if let Some ( mhz) = info. cpu_mhz {
349- w. field_str ( "cpu_mhz" , & format ! ( "{:.2}" , mhz) ) ;
350+ w. field_str ( f :: CPU_MHZ , & format ! ( "{:.2}" , mhz) ) ;
350351 }
351- w. field_str_opt ( "cache_size" , info. cache_size . as_deref ( ) ) ;
352- w. field_str_opt ( "architecture" , info. architecture . as_deref ( ) ) ;
352+ w. field_str_opt ( f :: CACHE_SIZE , info. cache_size . as_deref ( ) ) ;
353+ w. field_str_opt ( f :: ARCHITECTURE , info. architecture . as_deref ( ) ) ;
353354 }
354355
355356 w. end_field_object ( ) ;
0 commit comments