Skip to main content

twinleaf_tools/
lib.rs

1use clap::Parser;
2use std::path::PathBuf;
3use std::time::Duration;
4use tio::proto::DeviceRoute;
5use tio::util;
6use twinleaf::tio;
7pub mod tools;
8pub mod tui;
9include!("tio_cli.rs");
10
11fn parse_device_route(s: &str) -> Result<DeviceRoute, String> {
12    DeviceRoute::from_str(s).map_err(|_| format!("invalid sensor route: {s:?}"))
13}
14
15fn parse_existing_file(s: &str) -> Result<PathBuf, String> {
16    let p = PathBuf::from(s);
17    match std::fs::metadata(&p) {
18        Ok(m) if m.is_file() => Ok(p),
19        Ok(_) => Err(format!("not a regular file: {s:?}")),
20        Err(e) => Err(format!("cannot read {s:?}: {e}")),
21    }
22}
23
24#[derive(Parser, Debug, Clone)]
25pub struct TioOpts {
26    /// Sensor root address (e.g., tcp://localhost, serial:///dev/ttyUSB0)
27    #[arg(
28        short = 'r',
29        long = "root",
30        default_value_t = util::default_proxy_url().to_string(),
31        value_hint = clap::ValueHint::Url,
32        help = "Sensor root address"
33    )]
34    pub root: String,
35
36    /// Sensor path in the sensor tree (e.g., /, /0, /0/1)
37    #[arg(
38        short = 's',
39        long = "sensor",
40        default_value = "/",
41        value_parser = parse_device_route,
42        help = "Sensor path in the sensor tree"
43    )]
44    pub route: DeviceRoute,
45}