Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Nom 3.2 #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ travis-ci = { repository = "danburkert/procinfo-rs" }

[dependencies]
libc = "0.2"
nom = { version = "2", features = ["verbose-errors"] }
nom = { version = "3", features = ["verbose-errors"] }
byteorder = "1.0"

[build-dependencies]
Expand Down
33 changes: 20 additions & 13 deletions src/loadavg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,26 @@ pub struct LoadAvg {
}

/// Parses the loadavg file format.
named!(parse_loadavg<LoadAvg>,
chain!(load_avg_1_min: parse_f32 ~ space ~
load_avg_5_min: parse_f32 ~ space ~
load_avg_10_min: parse_f32 ~ space ~
tasks_runnable: parse_u32 ~ tag!("/") ~
tasks_total: parse_u32 ~ space ~
last_created_pid: parse_i32 ~ line_ending,
|| { LoadAvg { load_avg_1_min: load_avg_1_min,
load_avg_5_min: load_avg_5_min,
load_avg_10_min: load_avg_10_min,
tasks_runnable: tasks_runnable,
tasks_total: tasks_total,
last_created_pid: last_created_pid } }));
named!(parse_loadavg<LoadAvg>, do_parse!(
load_avg_1_min: parse_f32 >>
space >>
load_avg_5_min: parse_f32 >>
space >>
load_avg_10_min: parse_f32 >>
space >>
tasks_runnable: parse_u32 >>
tag!("/") >>
tasks_total: parse_u32 >>
space >>
last_created_pid: parse_i32 >>
opt!(line_ending) >>
(LoadAvg {
load_avg_1_min: load_avg_1_min,
load_avg_5_min: load_avg_5_min,
load_avg_10_min: load_avg_10_min,
tasks_runnable: tasks_runnable,
tasks_total: tasks_total,
last_created_pid: last_created_pid } )));

/// Returns the system load average.
pub fn loadavg() -> Result<LoadAvg> {
Expand Down
2 changes: 1 addition & 1 deletion src/net/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ named!(interface_stats<DeviceStatus>,

named!(interface_list< Vec<DeviceStatus> >,
do_parse!(
interfaces: separated_list!(line_ending, interface_stats) >>
interfaces: separated_list_complete!(line_ending, interface_stats) >>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what the significance of this change is, but it was failing to unwrap Incomplete results until I changed this.

line_ending >>
(interfaces)));

Expand Down
13 changes: 9 additions & 4 deletions src/parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ named!(pub parse_f32<f32>,
map_res!(map_res!(fdigit, str::from_utf8), FromStr::from_str));

/// Parses a sequence of whitespace seperated u32s.
named!(pub parse_u32s<Vec<u32> >, separated_list!(space, complete!(parse_u32)));
named!(pub parse_u32s<Vec<u32> >, separated_list_complete!(space, parse_u32));

/// Parses a sequence of whitespace seperated i32s.
named!(pub parse_i32s<Vec<i32> >, separated_list!(space, parse_i32));
named!(pub parse_i32s<Vec<i32> >, separated_list_complete!(space, parse_i32));

/// Parses a bit into a boolean
named!(pub parse_bit<bool>, alt!(
Expand All @@ -152,7 +152,12 @@ named!(pub parse_bit<bool>, alt!(

/// Parses a usize followed by a kB unit tag.
named!(pub parse_kb<usize>,
chain!(space ~ bytes: parse_usize ~ space ~ tag!("kB"), || { bytes }));
do_parse!(
space >>
bytes: parse_usize >>
space >>
opt!(tag!("kB")) >>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may have misinterpreted the logic of the chain, this may not need to be wrapped in opt!?

(bytes)));

/// Parses a u32 in base-16 format.
named!(pub parse_u32_hex<u32>,
Expand Down Expand Up @@ -181,7 +186,7 @@ fn reverse(n: u8) -> u8 {
///
/// See cpuset(7) for the format being parsed.
named!(pub parse_u32_mask_list<Box<[u8]> >,
map!(separated_nonempty_list!(tag!(","), parse_u32_hex), |mut ints: Vec<u32>| {
map!(separated_nonempty_list_complete!(tag!(","), parse_u32_hex), |mut ints: Vec<u32>| {
let mut bytes: Vec<u8> = Vec::with_capacity(ints.len() * 4);
let mut buf: [u8; 4] = [0; 4];
ints.reverse();
Expand Down
33 changes: 21 additions & 12 deletions src/pid/statm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,27 @@ pub struct Statm {

/// Parses the statm file format.
named!(parse_statm<Statm>,
chain!(size: parse_usize ~ space ~
resident: parse_usize ~ space ~
share: parse_usize ~ space ~
text: parse_usize ~ space ~
digit ~ space ~ // lib - unused since linux 2.6
data: parse_usize ~ space ~
digit ~ line_ending, // dt - unused since linux 2.6
|| { Statm { size: size,
resident: resident,
share: share,
text: text,
data: data } }));
do_parse!(
size: parse_usize >>
space >>
resident: parse_usize >>
space >>
share: parse_usize >>
space >>
text: parse_usize >>
space >>
digit >>
space >> // lib - unused since linux 2.6
data: parse_usize >>
space >>
digit >>
opt!(line_ending) >> // dt - unused since linux 2.6
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto, do you think I can drop the opt!?

(Statm {
size: size,
resident: resident,
share: share,
text: text,
data: data } )));

/// Parses the provided statm file.
fn statm_file(file: &mut File) -> Result<Statm> {
Expand Down
41 changes: 33 additions & 8 deletions src/pid/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,29 @@ named!(parse_tid<pid_t>, delimited!(tag!("Pid:\t"), parse_i32,
named!(parse_ppid<pid_t>, delimited!(tag!("PPid:\t"), parse_i32, line_ending));
named!(parse_tracer_pid<pid_t>, delimited!(tag!("TracerPid:\t"), parse_i32, line_ending));

named!(parse_uid<(uid_t, uid_t, uid_t, uid_t)>, chain!(tag!("Uid:\t") ~ real: parse_u32 ~ space ~ effective: parse_u32
~ space ~ saved: parse_u32 ~ space ~ fs: parse_u32 ~ line_ending,
|| { (real, effective, saved, fs) }));
named!(parse_gid<(gid_t, gid_t, gid_t, gid_t)>, chain!(tag!("Gid:\t") ~ real: parse_u32 ~ space ~ effective: parse_u32
~ space ~ saved: parse_u32 ~ space ~ fs: parse_u32 ~ line_ending,
|| { (real, effective, saved, fs) }));
named!(parse_uid<(uid_t, uid_t, uid_t, uid_t)>, do_parse!(
tag!("Uid:\t") >>
real: parse_u32 >>
space >>
effective: parse_u32 >>
space >>
saved: parse_u32 >>
space >>
fs: parse_u32 >>
opt!(line_ending) >>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

((real, effective, saved, fs))));

named!(parse_gid<(gid_t, gid_t, gid_t, gid_t)>, do_parse!(
tag!("Gid:\t") >>
real: parse_u32 >>
space >>
effective: parse_u32 >>
space >>
saved: parse_u32 >>
space >>
fs: parse_u32 >>
opt!(line_ending) >>
((real, effective, saved, fs))));

named!(parse_fd_allocated<u32>, delimited!(tag!("FDSize:\t"), parse_u32, line_ending));
named!(parse_groups<Vec<gid_t> >, delimited!(tag!("Groups:\t"), parse_u32s, multispace));
Expand Down Expand Up @@ -248,8 +265,16 @@ named!(parse_seccomp<SeccompMode>, delimited!(tag!("Seccomp:\t"), parse
named!(parse_cpus_allowed<Box<[u8]> >, delimited!(tag!("Cpus_allowed:\t"), parse_u32_mask_list, line_ending));
named!(parse_mems_allowed<Box<[u8]> >, delimited!(tag!("Mems_allowed:\t"), parse_u32_mask_list, line_ending));

named!(parse_cpus_allowed_list<()>, chain!(tag!("Cpus_allowed_list:\t") ~ not_line_ending ~ line_ending, || { () }));
named!(parse_mems_allowed_list<()>, chain!(tag!("Mems_allowed_list:\t") ~ not_line_ending ~ line_ending, || { () }));
named!(parse_cpus_allowed_list<()>, do_parse!(
tag!("Cpus_allowed_list:\t") >>
not_line_ending >>
opt!(line_ending) >>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

(())));
named!(parse_mems_allowed_list<()>, do_parse!(
tag!("Mems_allowed_list:\t") >>
not_line_ending >>
opt!(line_ending) >>
(())));

named!(parse_voluntary_ctxt_switches<u64>, delimited!(tag!("voluntary_ctxt_switches:\t"), parse_u64, line_ending));
named!(parse_nonvoluntary_ctxt_switches<u64>, delimited!(tag!("nonvoluntary_ctxt_switches:\t"), parse_u64, line_ending));
Expand Down