YamlWrapper

Struct YamlWrapper 

Source
pub struct YamlWrapper {
    pub raw_data: String,
    pub data: Value,
}
Expand description

Yaml 数据读取

use dev_tool::YamlWrapper;
 
#[test]
fn test_config_util() {
   // 读取配置文件
    let wrapper = YamlWrapper::new("docs/config.yaml").unwrap();
    // 直接将yaml字符串转换成YamlWrapper
    // let warpper = YamlWrapper::from_string("......").unwrap();
 
    // 不管是对象,还是数组,都是直接通过`.`操作。address是对象,children是数组,name是children中对象的一个属性
    let x = wrapper.get("address.children.name");
    println!("address.children.name = {:?}", x);
     
    // get方法是获取数组,而get_one获取的是第一个元素
    let x = wrapper.get_one("address.x.y").as_str().unwrap();
    println!("address.x.y = {}", x);
}

Fields§

§raw_data: String§data: Value

Implementations§

Source§

impl YamlWrapper

Source

pub fn new(file_path: &str) -> Result<Self, Box<dyn Error>>

创建一个新的配置实例

该函数会读取指定路径的配置文件,解析JSON格式的内容,并创建配置对象

§参数
  • file_path - 配置文件的路径字符串引用
§返回值

返回Result类型,成功时包含配置实例,失败时包含IO错误

§错误

当文件读取失败或JSON解析失败时会返回相应的错误

Source

pub fn from_string(conf_string: &str) -> Result<Self, Box<dyn Error>>

Source

pub fn get<'a, 'b: 'a>(&'a self, key: &'b str) -> Vec<&'a Value>

获取指定键对应的值列表

该函数通过创建一个值包装器来访问内部数据,并返回与给定键关联的所有值。

§参数
  • key - 要查找的静态字符串键
§返回值

返回一个包含所有匹配值的引用向量

Source

pub fn get_one<'a, 'b: 'a>(&'a self, key: &'b str) -> &'a Value

获取指定键对应的第一个值

§参数
  • key - 要查找的键名,必须是静态字符串
§返回值

返回键对应值的引用

§注意

如果键不存在或对应的值为空,会触发panic

Trait Implementations§

Source§

impl Debug for YamlWrapper

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,