2个版本
0.1.1 | 2024年6月6日 |
---|---|
0.1.0 | 2024年5月30日 |
#4 in #nss
13KB
107 行
gecos
这是一个用于生成和解析gecos的Rust库。
我们开始开发这个库是为了与libnss一起使用。例如,这个库被用于guest-users nss包中。
安装
通过cargo
简单安装
cargo add gecos
用法
有关完整参考,请查看Gecos
结构。
use std::convert::TryFrom;
use gecos::{Gecos, GecosSanitizedString};
// read gecos string from passwd etc.
let raw_gecos_string = "Some Person,Room,Work phone,Home phone,Other 1,Other 2";
let mut gecos = Gecos::from_gecos_string(raw_gecos_string).unwrap();
// access fields like
// var field option for comp
assert_eq!(gecos.full_name.as_ref().unwrap().to_string(), "Some Person");
// and you even can convert it back to a raw gecos string
assert_eq!(gecos.to_gecos_string(), raw_gecos_string);
// modifying fields work like this
gecos.full_name = Some("Another name".to_string().try_into().unwrap());
// or more explicitly
gecos.room = Some(GecosSanitizedString::new("St. 9".to_string()).unwrap());
assert_eq!(gecos.full_name.as_ref().unwrap().to_string(), "Another name");
assert_eq!(gecos.room.as_ref().unwrap().to_string(), "St. 9");
依赖项
~280–740KB
~17K SLoC