Parse rule files with Rust

This commit is contained in:
topjohnwu
2023-05-24 19:11:56 -07:00
parent 5a94ef9106
commit 18d0cedbe2
16 changed files with 176 additions and 93 deletions
+4 -4
View File
@@ -7,9 +7,9 @@ edition = "2021"
path = "lib.rs"
[build-dependencies]
cxx-gen = { path = "../external/cxx-rs/gen/lib" }
cxx-gen = { workspace = true }
[dependencies]
cxx = { path = "../external/cxx-rs" }
libc = "0.2"
cfg-if = "1.0"
cxx = { workspace = true }
libc = { workspace = true }
cfg-if = { workspace = true }
+38 -6
View File
@@ -1,14 +1,12 @@
use std::cmp::min;
use std::ffi::CStr;
use std::fs::File;
use std::io;
use std::io::{BufRead, Write};
use std::os::unix::io::{AsRawFd, FromRawFd, OwnedFd, RawFd};
use std::path::Path;
use libc::{c_char, c_uint, mode_t, EEXIST, ENOENT, O_CLOEXEC, O_PATH};
use crate::{bfmt_cstr, errno, xopen};
use crate::{bfmt_cstr, errno, error, xopen};
pub mod unsafe_impl {
use std::ffi::CStr;
@@ -137,9 +135,43 @@ pub extern "C" fn mkdirs(path: *const c_char, mode: mode_t) -> i32 {
}
}
pub fn read_lines<P: AsRef<Path>>(path: P) -> io::Result<io::Lines<io::BufReader<File>>> {
let file = File::open(path)?;
Ok(io::BufReader::new(file).lines())
pub trait BufReadExt {
fn foreach_lines<F: FnMut(&mut String) -> bool>(&mut self, f: F);
fn foreach_props<F: FnMut(&str, &str) -> bool>(&mut self, f: F);
}
impl<T: BufRead> BufReadExt for T {
fn foreach_lines<F: FnMut(&mut String) -> bool>(&mut self, mut f: F) {
let mut buf = String::new();
loop {
match self.read_line(&mut buf) {
Ok(0) => break,
Ok(_) => {
if !f(&mut buf) {
break;
}
}
Err(e) => {
error!("{}", e);
break;
}
};
buf.clear();
}
}
fn foreach_props<F: FnMut(&str, &str) -> bool>(&mut self, mut f: F) {
self.foreach_lines(|line| {
let line = line.trim();
if line.starts_with('#') {
return true;
}
if let Some((key, value)) = line.split_once('=') {
return f(key, value);
}
return true;
});
}
}
pub trait WriteExt {