Skip to content

Commit

Permalink
refacto(commons): move parse_vars function in commons crate (#173)
Browse files Browse the repository at this point in the history
This will allow reusing this logic in all Zenoh-Flow executable.

Signed-off-by: Julien Loudet <julien.loudet@zettascale.tech>
  • Loading branch information
J-Loudet authored Feb 7, 2024
1 parent c8f8f9a commit e5968b4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion zenoh-flow-commons/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod utils;
pub use utils::try_load_from_file;

mod vars;
pub use vars::Vars;
pub use vars::{parse_vars, Vars};

/// Zenoh-Flow's result type.
pub type Result<T> = std::result::Result<T, anyhow::Error>;
17 changes: 17 additions & 0 deletions zenoh-flow-commons/src/vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use crate::IMergeOverwrite;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::error::Error;
use std::ops::Deref;
use std::rc::Rc;

Expand Down Expand Up @@ -78,3 +79,19 @@ impl<T: AsRef<str>, U: AsRef<str>> From<Vec<(T, U)>> for Vars {
}
}
}

/// Parse a Vars from a string of the format "KEY=VALUE".
pub fn parse_vars<T, U>(
s: &str,
) -> std::result::Result<(T, U), Box<dyn Error + Send + Sync + 'static>>
where
T: std::str::FromStr,
T::Err: Error + Send + Sync + 'static,
U: std::str::FromStr,
U::Err: Error + Send + Sync + 'static,
{
let pos = s
.find('=')
.ok_or_else(|| format!("invalid KEY=value: no `=` found in `{s}`"))?;
Ok((s[..pos].parse()?, s[pos + 1..].parse()?))
}
6 changes: 3 additions & 3 deletions zfctl/src/instance_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use crate::row;

use super::{parse_key_val, ZENOH_FLOW_INTERNAL_ERROR};
use super::ZENOH_FLOW_INTERNAL_ERROR;

use std::path::PathBuf;

Expand All @@ -23,7 +23,7 @@ use clap::Subcommand;
use comfy_table::Table;
use uuid::Uuid;
use zenoh::prelude::r#async::*;
use zenoh_flow_commons::{Result, RuntimeId, Vars};
use zenoh_flow_commons::{parse_vars, Result, RuntimeId, Vars};
use zenoh_flow_daemon::{selectors, InstanceStatus, InstancesQuery, Origin};
use zenoh_flow_descriptors::{DataFlowDescriptor, FlattenedDataFlowDescriptor};

Expand Down Expand Up @@ -56,7 +56,7 @@ pub(crate) enum InstanceCommand {
///
/// Example:
/// --vars HOME_DIR=/home/zenoh-flow --vars BUILD=debug
#[arg(long, value_parser = parse_key_val::<String, String>, verbatim_doc_comment)]
#[arg(long, value_parser = parse_vars::<String, String>, verbatim_doc_comment)]
vars: Option<Vec<(String, String)>>,
},
/// To delete (and abort, if required) the data flow instance
Expand Down
18 changes: 0 additions & 18 deletions zfctl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
mod instance_command;
mod runtime_command;

use std::error::Error;

use anyhow::anyhow;
use clap::{Parser, Subcommand};
use instance_command::InstanceCommand;
Expand Down Expand Up @@ -67,22 +65,6 @@ enum Command {
Runtime(RuntimeCommand),
}

/// Parse a single key-value pair
fn parse_key_val<T, U>(
s: &str,
) -> std::result::Result<(T, U), Box<dyn Error + Send + Sync + 'static>>
where
T: std::str::FromStr,
T::Err: Error + Send + Sync + 'static,
U: std::str::FromStr,
U::Err: Error + Send + Sync + 'static,
{
let pos = s
.find('=')
.ok_or_else(|| format!("invalid KEY=value: no `=` found in `{s}`"))?;
Ok((s[..pos].parse()?, s[pos + 1..].parse()?))
}

#[async_std::main]
async fn main() -> Result<()> {
// TODO Configure tracing such that:
Expand Down

0 comments on commit e5968b4

Please sign in to comment.