Skip to content

Commit 42f3d02

Browse files
committed
config: add function to find pin config by name
Signed-off-by: Paul Osborne <[email protected]>
1 parent 127683f commit 42f3d02

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/config.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,17 @@ impl GpioConfig {
182182
GpioConfig::from_str(&contents[..])
183183
}
184184

185+
/// Get the pin with the provided name if present in this configuration
186+
pub fn get_pin(&self, name: &str) -> Option<&PinConfig> {
187+
self.pins.iter().find(|p| p.names.iter().any(|n| n == name))
188+
}
189+
190+
/// Get a reference to all the pins in this config
185191
pub fn get_pins(&self) -> &[PinConfig] {
186192
&self.pins[..]
187193
}
188194

195+
/// Get the symlink root specified in the config (or the default)
189196
pub fn get_symlink_root(&self) -> &str {
190197
match self.symlink_root {
191198
Some(ref root) => &root,
@@ -302,6 +309,19 @@ names = ["wildcard"]
302309
assert_eq!(status_led.export, true);
303310
}
304311

312+
#[test]
313+
fn test_get_pin_present() {
314+
let config = GpioConfig::from_str(BASIC_CFG).unwrap();
315+
let status_led = config.get_pin("status_led").unwrap();
316+
assert_eq!(status_led.num, 37);
317+
}
318+
319+
#[test]
320+
fn test_get_pin_not_present() {
321+
let config = GpioConfig::from_str(BASIC_CFG).unwrap();
322+
assert_eq!(config.get_pin("missing"), None);
323+
}
324+
305325
#[test]
306326
fn test_parser_compact() {
307327
let config = GpioConfig::from_str(COMPACT_CFG).unwrap();

0 commit comments

Comments
 (0)