i_edit_yaml 0.1.0

A lightweight, high-performance tool for editing YAML based on field paths
Documentation

i_edit_yaml

Crates.io Documentation License: MIT OR Apache-2.0 Rust

一个轻量级、高性能的、基于字段路径的 YAML 编辑工具。

功能特性

  • 通过直观的字段路径读取和修改 YAML 文件
  • 支持嵌套结构和数组操作
  • 类型感知的数值处理
  • 可作为独立 CLI 工具或 Rust 库使用
  • 支持 YAML 和 JSON 输出格式

安装

从 crates.io 安装

cargo install i_edit_yaml

从源码安装

git clone https://github.com/ymc-github/i_edit_yaml

cd i_edit_yaml

cargo install --path .

# 从 GitHub 安装

cargo install --git https://github.com/ymc-github/i_edit_yaml


# 指定分支

cargo install --git https://github.com/ymc-github/i_edit_yaml --branch main


# 指定标签

cargo install --git https://github.com/ymc-github/i_edit_yaml --tag v0.1.0

使用方法

作为命令行工具

提取字段(get 命令)

# 基本使用

i_edit_yaml get -f config.yaml -k package.name


# 提取数组

i_edit_yaml get --array package.authors


# 提取数组元素

i_edit_yaml get --array-element package.authors --array-index 0


# 提取数组长度

i_edit_yaml get --array-length package.keywords


# 提取映射的所有键

i_edit_yaml get --mapping-keys dependencies


# 提取多个字段

i_edit_yaml get -m package.name -m package.version -m package.authors


# 输出为 JSON 格式

i_edit_yaml get -k dependencies --output json-pretty


# 输出为 YAML 格式

i_edit_yaml get -k package --output yaml-pretty

设置字段(set 命令)

# 基本使用

i_edit_yaml set -f config.yaml -k package.version -v "1.0.0" --in-place


# 设置数组元素

i_edit_yaml set -k package.authors[0] -v "New Author <author@example.com>" --in-place


# 创建不存在的字段

i_edit_yaml set -k package.description -v "A new description" --create-missing --in-place


# 指定值类型

i_edit_yaml set -k package.enabled -v "true" -t boolean --in-place


# 设置 null 值

i_edit_yaml set -k package.optional -v "null" -t null --in-place

作为库使用

添加依赖到 Cargo.toml

[dependencies]

i_edit_yaml = "0.1"

在代码中使用:

use i_edit_yaml::{get, set, ExtractConfig, SetConfig};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 提取字段
    let get_config = ExtractConfig {
        file_path: "config.yaml".to_string(),
        field_path: "database.host".to_string(),
        output_format: None,
        strip_quotes: true,
    };
    let host = get::extract_field(&get_config)?;
    println!("Database host: {}", host);

    // 设置字段
    let set_config = SetConfig {
        file_path: "config.yaml".to_string(),
        field_path: "database.port".to_string(),
        value: "5432".to_string(),
        value_type: Some("integer".to_string()),
        create_missing: false,
    };
    set::set_field_and_save(&set_config)?;
    println!("Database port updated successfully");

    Ok(())
}

许可证

MIT OR Apache-2.0