Skip to content

Commit 00de1f3

Browse files
committed
config: basic structure for config parser
Took a little to figure out that the proper way to parse data using toml-rs is using rustc-serialize to "automatically" marshal to structures from the input data structures. There will need to be a second pass to normalize all of the data. Signed-off-by: Paul Osborne <[email protected]>
1 parent dca878e commit 00de1f3

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ version = "0.1.0"
44
authors = ["Paul Osborne <[email protected]>"]
55

66
[dependencies]
7-
sysfs_gpio = "0.4"
87
clap = "2.2"
8+
rustc-serialize = "0.3"
9+
sysfs_gpio = "0.4"
910
toml = "0.1"
1011

1112
[[bin]]

src/config.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,110 @@
11
// Copyright (C) 2016, Paul Osborne <[email protected]>
2+
3+
use toml;
4+
use rustc_serialize::{Decodable};
5+
use std::io;
6+
use std::path::Path;
7+
use std::collections::HashMap;
8+
use sysfs_gpio::Direction;
9+
10+
#[derive(RustcDecodable, Debug)]
11+
pub struct PinConfig {
12+
num: u64,
13+
direction: Option<String>,
14+
aliases: Option<Vec<String>>,
15+
export: Option<bool>,
16+
active_low: Option<bool>,
17+
}
18+
19+
#[derive(RustcDecodable, Debug)]
20+
pub struct GpioConfig {
21+
pins: HashMap<String, PinConfig>,
22+
}
23+
24+
#[derive(Debug)]
25+
pub enum Error {
26+
IoError(io::Error),
27+
ParseError,
28+
NoConfigFound,
29+
}
30+
31+
fn to_direction(dirstr: &str) -> Option<Direction> {
32+
match dirstr {
33+
"in" => Some(Direction::In),
34+
"out" => Some(Direction::Out),
35+
"high" => Some(Direction::High),
36+
"low" => Some(Direction::Low),
37+
_ => None
38+
}
39+
}
40+
41+
impl GpioConfig {
42+
43+
/// Load a GPIO Config from the system
44+
///
45+
/// This function will load the GPIO configuration from standard system
46+
/// locations as well as from the additional configs passed in via the
47+
/// `configs` parameter. Each parameter is expected to be a path to a
48+
/// config file in disk.
49+
///
50+
/// Under the covers, this function will attempt to discover configuration
51+
/// files in the following standard locations in order:
52+
///
53+
/// - `/etc/gpio.toml`
54+
/// - `/etc/gpio.d/*.toml`
55+
/// - `configs` (parameter)
56+
///
57+
/// Each config file found in these locations will be loaded and then they
58+
/// will be pulled together to form a unified configuration via the
59+
/// `combine` method.
60+
pub fn load(configs: Vec<String>) -> Result<GpioConfig, Error> {
61+
Err(Error::NoConfigFound)
62+
}
63+
64+
/// Load a GPIO configuration for the provided toml string
65+
pub fn from_str(config: &str) -> Result<GpioConfig, Error> {
66+
let root = toml::Parser::new(config).parse().unwrap();
67+
let mut d = toml::Decoder::new(toml::Value::Table(root));
68+
Ok(Decodable::decode(&mut d).unwrap())
69+
}
70+
71+
/// Load a GPIO config from the specified path
72+
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<GpioConfig, Error> {
73+
Err(Error::NoConfigFound)
74+
}
75+
}
76+
77+
#[cfg(test)]
78+
mod test {
79+
use super::*;
80+
81+
#[test]
82+
fn test_parse_basic() {
83+
let configstr = r#"
84+
# Basic Form
85+
[pins.reset_button]
86+
num = 73 # required
87+
direction = "in" # default: in
88+
active_low = true # default: false
89+
export = true # default: true
90+
91+
[pins.status_led]
92+
num = 37
93+
aliases = ["A27", "green_led"]
94+
direction = "out"
95+
96+
# Compact Form
97+
[pins]
98+
error_led = { num = 11, direction = "in", export = false}
99+
"#;
100+
let config = GpioConfig::from_str(configstr).unwrap();
101+
let status_led = config.pins.get("status_led").unwrap();
102+
assert_eq!(status_led.num, 37);
103+
assert_eq!(status_led.aliases,
104+
Some(vec!(String::from("A27"),
105+
String::from("green_led"))));
106+
assert_eq!(status_led.direction, Some(String::from("out")));
107+
assert_eq!(status_led.active_low, None);
108+
assert_eq!(status_led.export, None);
109+
}
110+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2016, Paul Osborne <[email protected]>
22

33
extern crate sysfs_gpio;
4+
extern crate rustc_serialize;
5+
extern crate toml;
46

57
pub mod options;
68
pub mod config;

0 commit comments

Comments
 (0)