Crate xqpath

Crate xqpath 

Source
Expand description

§XQPath

一个用于结构化数据(JSON/YAML/TOML/CSV)路径提取与更新的高性能 Rust 工具库。

§快速开始

§基本使用

use xqpath::{query, exists};
use serde_json::json;

let yaml = r#"
user:
  name: Alice
  age: 30
  scores: [85, 92, 78]
"#;

// 提取字段值
let name = query!(yaml, "user.name").unwrap();
assert_eq!(name[0], json!("Alice"));

// 检查路径是否存在
let exists = exists!(yaml, "user.email").unwrap();
assert_eq!(exists, false);

§调试功能(v1.4.1+)

#[cfg(feature = "debug")]
use xqpath::{query_debug, trace_query, DebugContext};
use serde_json::json;

#[cfg(feature = "debug")]
fn debug_example() {
    let data = r#"{"users": [{"name": "Alice"}, {"name": "Bob"}]}"#;

    // 带调试信息的查询
    let result = query_debug!(data, ".users[*].name", |debug_info: &xqpath::debug::DebugInfo| {
        println!("解析耗时: {:?}", debug_info.parse_duration);
        println!("执行路径: {}", debug_info.execution_path);
    }).unwrap();

    // 性能跟踪查询
    let (result, stats) = trace_query!(data, ".users[*].name").unwrap();
    println!("总耗时: {:?}", stats.duration);
}

§更新操作(需要 update feature)

#[cfg(feature = "update")]
use xqpath::update;
use serde_json::json;

#[cfg(feature = "update")]
fn example() {
    let yaml = r#"user: {name: Alice, age: 30}"#;
    let updated = update!(yaml, "user.age", json!(31)).unwrap();
}

§特性

  • 路径提取: 支持 .field[index]*** 等 jq 风格路径
  • 格式支持: 自动检测和解析 JSON/YAML 格式
  • 通配符: 支持字段和递归匹配
  • 类型过滤: 支持类型断言和过滤
  • 更新功能: 支持路径指定位置的更新(feature gate)
  • 调试支持: 结构化日志和性能跟踪(feature gate)
  • 轻量级: 最小依赖集,高性能

Re-exports§

pub use extractor::extract;
pub use extractor::ConfigurableExtractor;
pub use extractor::ExtractError;
pub use extractor::Extractor;
pub use extractor::ExtractorConfig;
pub use debug::DebugCapable;
pub use debug::DebugConfig;
pub use debug::DebugContext;
pub use debug::DebugInfo;
pub use debug::LogLevel;
pub use debug::TimingStats;
pub use debug::logger::Logger;
pub use debug::logger::LoggerConfig;
pub use debug::tracer::ExecutionSummary;
pub use debug::tracer::TraceEvent;
pub use debug::tracer::TraceResult;
pub use debug::tracer::Tracer;
pub use debug::reporter::DiagnosticInfo;
pub use debug::reporter::EnhancedError;
pub use debug::reporter::ErrorReporter;
pub use debug::reporter::ErrorType;
pub use debug::reporter::FixSuggestion;
pub use config::ConfigError;
pub use config::ConfigManager;
pub use config::ConfigResult;
pub use config::XQPathConfig;
pub use debugger::Breakpoint;
pub use debugger::DataInspector;
pub use debugger::DebugCommand;
pub use debugger::DebugError;
pub use debugger::DebugResult;
pub use debugger::DebugSession;
pub use debugger::QueryEvaluator;
pub use debugger::WatchPoint;
pub use debugger::XQPathDebugger;
pub use parser::ast::ComparisonOp;
pub use parser::ast::ExpressionComplexity;
pub use parser::ast::LogicalOp;
pub use parser::ast::PathExpression;
pub use parser::evaluation::evaluate_path_expression;
pub use parser::evaluation::EvaluationError;
pub use parser::evaluation::ExpressionEvaluator;
pub use parser::functions::AdvancedBuiltinFunction;
pub use parser::functions::BuiltinFunction;
pub use parser::functions::FunctionRegistry;
pub use parser::parsing::parse_path_expression;
pub use parser::parsing::ExpressionParser;
pub use parser::path::parse_path;
pub use parser::path::ParseError;
pub use parser::path::PathSegment;
pub use value::format::detect_format;
pub use value::format::FormatError;
pub use value::format::FormatRegistry;
pub use value::format::JsonFormat;
pub use value::format::ValueFormat;
pub use value::format::YamlFormat;
pub use value::json::JsonPath;
pub use value::json::JsonSupport;
pub use value::yaml::YamlFormatter;
pub use value::yaml::YamlSpecialValues;
pub use value::yaml::YamlSupport;

Modules§

config
配置管理系统
debug
调试模块 - XQPath 调试和维测能力
debugger
交互式调试器
extractor
parser
value

Macros§

count
便利宏,用于计算匹配路径的值的数量
exists
便利宏,用于检查路径是否存在
exists_all
便利宏,用于检查结构化数据中是否存在所有指定路径
exists_any
便利宏,用于检查结构化数据中是否存在任意一个指定路径
extract
便利宏,用于从多种格式的数据中提取值,并指定输出格式
get_type
便利宏,用于获取路径对应值的类型
query
便利宏,用于从结构化数据中提取字段
query_as_type
便利宏,用于从结构化数据中提取值并尝试转换为指定类型
query_debug
带调试信息的查询宏
query_length
便利宏,用于从结构化数据中查询数组长度
query_multi
便利宏,用于从结构化数据中查询多个路径
query_one
便利宏,用于从结构化数据中提取单个值
query_or_default
便利宏,用于从结构化数据中提取值,如果不存在则返回默认值
query_string
便利宏,用于从结构化数据中提取值并转换为字符串
trace_query
带性能跟踪的查询宏

Constants§

VERSION
库版本信息

Functions§

has_debug_feature
检查是否启用了调试功能
has_update_feature
检查是否启用了更新功能