1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#[path = "formats/hini.rs"] mod hini_format;
#[path = "formats/json.rs"] mod json_format;

use hini::tree::Node;
use std::cell::RefCell;
use std::rc::Rc;

/// Specifies a particular data format supported by hini-convert.
#[derive(Clone, Copy, Debug)]
pub enum DataFormat {
    // To add encoders/decoders, add the appropriate code in formats/whatever.rs, import it here,
    // and add it to impl DataFormat below.  To enable that format to be specified on the command
    // line, edit the ValueEnum impl for DataFormat in main.rs.

    Hini,
    Json,
    // TODO: more.
}

/// Function that transforms a hini tree into a string in some format.  Can't fail.
pub type Encoder = fn(root: Rc<RefCell<Node>>) -> String;

/// Function that transforms a string in some format into a hini tree.  Reports errors as strings.
pub type Decoder = fn(data: &str) -> Result<Rc<RefCell<Node>>, String>;

impl DataFormat {
    /// Return the encoder for this data format.
    pub fn encoder(&self) -> Encoder { match self {
        Self::Hini => hini_format::encode,
        Self::Json => json_format::encode,
    }}

    /// Return the decoder for this data format.
    pub fn decoder(&self) -> Decoder { match self {
        Self::Hini => hini_format::decode,
        Self::Json => json_format::decode,
    }}
}