9 个版本

0.0.10 2023年10月21日
0.0.9 2023年10月20日
0.0.4 2021年4月29日
0.0.3 2021年1月31日

#512解析器实现

28 每月下载次数

MIT 许可证

25KB
489

nparse 0.0.10

Build Status

用于各种 Rust 字符串的解析器。

解析器包括

  • 缩进良好的字符串(例如:dmidecode 输出)
  • 键值对字符串(例如:lscpu 输出)
  • 多行键值对字符串(例如:Windows 的 systeminfo 输出)
  • 点状树字符串(例如:sysctl 输出)

要求

  • Rust 1.50+

用法

nparse = "0.0.10"

示例用法

  • Indent 字符串转换为 json
use std::{fs::File, io::Read};
use nparse::*;

fn main () {
    let path = "data/dmidecode.txt";
    let mut out = String::new();
    {
        let mut f = File::open(path).unwrap();
        f.read_to_string(&mut out).unwrap();
    }
    let result = out.indent_to_json();
    println!("{:?}", result);
}
  • K:V 字符串转换为 json
use std::{fs::File, io::Read};
use nparse::*;

fn main () {
    let path = "data/lscpu.txt";
    let mut out = String::new();
    {
        let mut f = File::open(path).unwrap();
        f.read_to_string(&mut out).unwrap();
    }
    let result = out.kv_str_to_json();
    println!("{:?}", result);
}
  • K=V 字符串转换为 json
use std::{fs::File, io::Read};
use nparse::*;

fn main () {
    let path = "data/os-release.txt";
    let mut out = String::new();
    {
        let mut f = File::open(path).unwrap();
        f.read_to_string(&mut out).unwrap();
    }
    let result = out.kev_str_to_json();
    println!("{:?}", result);
}
  • dotted 字符串转换为 json(例如:parent.sub.sub: val
use std::{fs::File, io::Read};
use nparse::*;

fn main () {
    let path = "data/sysctl.txt";
    let mut out = String::new();
    {
        let mut f = File::open(path).unwrap();
        f.read_to_string(&mut out).unwrap();
    }
    let result = out.dotted_tree_to_json();
    println!("{:?}", result);
}

测试,构建

  • 测试
cargo t 
  • 构建发布版本
cargo b --release

示例

  • 将 dmidecode 输出解析为 json
cargo run --example dmidecode
  • 将 sysctl 输出解析为 json
cargo run --example sysctl
  • 将 lscpu 解析为 json
cargo run --example lscpu
  • 将 windows systeminfo 解析为 json
cargo run --example systeminfo

依赖项

~2–2.7MB
~51K SLoC