Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_sys/src/parse.rs

1.3 KiB, 1 run

created by r1870400018:10590, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! Shared helpers for reading and parsing `/proc` pseudo-files.
2
3use oxedyne_fe2o3_core::prelude::*;
4
5use std::{
6 fs,
7 path::Path,
8 str::FromStr,
9};
10
11/// Read the entire contents of `path` into a `String`. Wraps the
12/// standard-library read with the project error macros so callers
13/// can `res!()` it.
14pub fn read_to_string<P: AsRef<Path>>(path: P) -> Outcome<String> {
15 let p = path.as_ref();
16 match fs::read_to_string(p) {
17 Ok(s) => Ok(s),
18 Err(e) => Err(err!(
19 "Failed to read {:?}: {}",
20 p, e;
21 IO, File, Read)),
22 }
23}
24
25/// Parse a whitespace-separated numeric token into `T`. Returns
26/// an error whose message names the token so broken `/proc`
27/// output is visible in the log.
28pub fn parse_num<T: FromStr>(token: &str, field: &str) -> Outcome<T> {
29 match token.parse::<T>() {
30 Ok(v) => Ok(v),
31 Err(_) => Err(err!(
32 "Cannot parse {} token {:?} as number.",
33 field, token;
34 Input, Invalid, Decode)),
35 }
36}
37
38/// Split a line on whitespace and return a vector of non-empty
39/// tokens. `/proc` files use varying numbers of spaces as
40/// separators so splitwise iteration is simplest.
41pub fn tokens(line: &str) -> Vec<&str> {
42 line.split_whitespace().collect()
43}