Skip to main content

FromKv

Trait FromKv 

Source
pub trait FromKv: Sized {
    // Required method
    fn from_kv(value: &str, span: Span) -> Result<Self>;
}
Expand description

Trait for types that can be parsed from a KEY=VALUE string.

This is the extension point for custom types. Implement this trait to allow your type to be used in a #[derive(Kv)] struct.

The span parameter indicates where in the input the value is located, for error reporting.

§Example

use pkgsrc_kv::{FromKv, KvError, Span};

struct MyId(u32);

impl FromKv for MyId {
    fn from_kv(value: &str, span: Span) -> Result<Self, KvError> {
        value.parse::<u32>()
            .map(MyId)
            .map_err(|e| KvError::Parse {
                message: e.to_string(),
                span,
            })
    }
}

Required Methods§

Source

fn from_kv(value: &str, span: Span) -> Result<Self>

Parse a value from a string.

§Errors

Returns an error if the value cannot be parsed into the target type.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl FromKv for PathBuf

Source§

fn from_kv(value: &str, _span: Span) -> Result<Self>

Source§

impl FromKv for String

Source§

fn from_kv(value: &str, _span: Span) -> Result<Self>

Source§

impl FromKv for bool

Source§

fn from_kv(value: &str, span: Span) -> Result<Self>

Source§

impl FromKv for i8

Source§

fn from_kv(value: &str, span: Span) -> Result<Self>

Source§

impl FromKv for i16

Source§

fn from_kv(value: &str, span: Span) -> Result<Self>

Source§

impl FromKv for i32

Source§

fn from_kv(value: &str, span: Span) -> Result<Self>

Source§

impl FromKv for i64

Source§

fn from_kv(value: &str, span: Span) -> Result<Self>

Source§

impl FromKv for isize

Source§

fn from_kv(value: &str, span: Span) -> Result<Self>

Source§

impl FromKv for u8

Source§

fn from_kv(value: &str, span: Span) -> Result<Self>

Source§

impl FromKv for u16

Source§

fn from_kv(value: &str, span: Span) -> Result<Self>

Source§

impl FromKv for u32

Source§

fn from_kv(value: &str, span: Span) -> Result<Self>

Source§

impl FromKv for u64

Source§

fn from_kv(value: &str, span: Span) -> Result<Self>

Source§

impl FromKv for usize

Source§

fn from_kv(value: &str, span: Span) -> Result<Self>

Source§

impl<T: FromKv> FromKv for Vec<T>

Source§

fn from_kv(value: &str, span: Span) -> Result<Self>

Implementors§