`Decode` setting

I want to deserialize the configuration structure as follows:

#[deriving(Clone, PartialEq, Decodable, Show)]
pub struct Config {
    network1: Network,
    network2: Network
}

#[deriving(Clone, PartialEq, Decodable, Show)]
pub struct Network {
    interface: Option<String>,
    prefer: Option<AddressPreference>,
    address: Option<String>,
    port: u16
}

#[deriving(Clone, PartialEq, Show)]
pub enum AddressPreference {
    IPv4, IPv6, Any
}

He forms a tree structand lists. But the decoder that I want to use does not support enumerations, so I thought that I could represent the enumeration as a string, implementing Decodablefor an enumeration that matches the strings. But I do not know how to do it properly.

The main problem is that I do not know how to write an implementation Decodablethat correctly handles errors. If i write

impl<E, D: Decoder<E>> Decodable<D, E> for AddressPreference { ... }

the only way I can indicate a parsing error is fail!()- I do not know the actual type of error, so I can not instantiate it. This is unacceptable.

Buf if i write

impl Decodable<toml::Decoder, toml::Error> for AddressPreference { ... }

Decodable ( ), , :

src/sfc/config.rs:11:30: 11:39 error: expected serialize::serialize::Decodable<__D,__E>, but found serialize::serialize::Decodable<rust-toml::Decoder,rust-toml::Error> (expected type parameter but found struct rust-toml::Decoder)
src/sfc/config.rs:11 #[deriving(Clone, PartialEq, Decodable, Show)]

? , , , , . , ?

+4

Source: https://habr.com/ru/post/1545189/


All Articles