config
config
管理rsup
的配置文件config.toml
,可以生成、读取、修改配置文件。
config
提供的配置读取、写入函数公用。在安装器执行时需要调用,所以发布到crates-io
,包名为rsup_config
.
在使用config
时,安装依赖名称为rsup_config
. 访问crates.io
rs
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Config {
pub name: String,
pub version: String,
pub dir: String,
// web 服务配置
pub web: WebConfig,
// 包管理配置
pub pkg: PkgConfig,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct WebConfig {
pub port: u16,
pub static_dir: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct PkgConfig {
pub npm_registry: String,
}
为了方便在各个子包中访问配置文件数据并且避免重复读取文件,config
提供了懒加载配置,确保了并发多线程下的读写安全。
rs
pub static CONFIG: Lazy<RwLock<Config>> = Lazy::new(|| {
// 这里调用初始化
let config = Config::read_config().unwrap();
RwLock::new(config)
});