Re-unify configuration files

Unfortunately the grammar of the --config parameter is a bit of a
football right now; previously you were allowed to specify it anywhere
because there wasn't much in the way of a CLI, then when subcommands
were added it became required to specify it after the subcommand.

This change makes it so that it must be specified *before* the
subcommand.  Ideally it would just be required *somewhere* in the
command line but clap doesn't support "global" for required arguments
(and it is indeed required).

On the whole, I'm okay with the change - the net win of having a unified
config struct makes up for the ergonomics hit in my opinion, and it
unlocks some further refactoring I will do next.
This commit is contained in:
George Hilliard committed 2021-10-29 00:25:54 -05:00
1 parent f10f9f9706
commit de39e7ed2a
14 files changed
+40 -237

No files matched your search

+133
View File
@@ -0,0 +1,133 @@
use lazy_static::lazy_static;
use regex::Regex;
use serde::Deserialize;
use std::clone::Clone;
use std::time::Duration;
use validator::{Validate, ValidationError};
use validator_derive::Validate;
lazy_static! {
static ref RE_STREAM_SRC: Regex =
Regex::new(r"^(mainStream|subStream|externStream|both|all)$").unwrap();
static ref RE_TLS_CLIENT_AUTH: Regex = Regex::new(r"^(none|request|require)$").unwrap();
}
#[derive(Debug, Deserialize, Validate, Clone)]
pub(crate) struct Config {
#[validate]
pub(crate) cameras: Vec<CameraConfig>,
#[serde(rename = "bind", default = "default_bind_addr")]
pub(crate) bind_addr: String,
#[validate(range(min = 0, max = 65535, message = "Invalid port", code = "bind_port"))]
#[serde(default = "default_bind_port")]
pub(crate) bind_port: u16,
#[serde(default = "default_certificate")]
pub(crate) certificate: Option<String>,
#[validate(regex(
path = "RE_TLS_CLIENT_AUTH",
message = "Incorrect tls auth",
code = "tls_client_auth"
))]
#[serde(default = "default_tls_client_auth")]
pub(crate) tls_client_auth: String,
#[validate]
#[serde(default)]
pub(crate) users: Vec<UserConfig>,
}
#[derive(Debug, Deserialize, Validate, Clone)]
#[validate(schema(function = "validate_camera_config"))]
pub(crate) struct CameraConfig {
pub(crate) name: String,
#[serde(rename = "address")]
pub(crate) camera_addr: Option<String>,
#[serde(rename = "uid")]
pub(crate) camera_uid: Option<String>,
pub(crate) username: String,
pub(crate) password: Option<String>,
// no longer used, but still here so we can warn users:
pub(crate) timeout: Option<Duration>,
// no longer used, but still here so we can warn users:
pub(crate) format: Option<String>,
#[validate(regex(
path = "RE_STREAM_SRC",
message = "Incorrect stream source",
code = "stream"
))]
#[serde(default = "default_stream")]
pub(crate) stream: String,
pub(crate) permitted_users: Option<Vec<String>>,
#[validate(range(min = 0, max = 31, message = "Invalid channel", code = "channel_id"))]
#[serde(default = "default_channel_id")]
pub(crate) channel_id: u8,
}
#[derive(Debug, Deserialize, Validate, Clone)]
pub(crate) struct UserConfig {
#[validate(custom = "validate_username")]
#[serde(alias = "username")]
pub(crate) name: String,
#[serde(alias = "password")]
pub(crate) pass: String,
}
fn default_bind_addr() -> String {
"0.0.0.0".to_string()
}
fn default_bind_port() -> u16 {
8554
}
fn default_stream() -> String {
"both".to_string()
}
fn default_certificate() -> Option<String> {
None
}
fn default_tls_client_auth() -> String {
"none".to_string()
}
fn default_channel_id() -> u8 {
0
}
pub(crate) static RESERVED_NAMES: &[&str] = &["anyone", "anonymous"];
fn validate_username(name: &str) -> Result<(), ValidationError> {
if name.trim().is_empty() {
return Err(ValidationError::new("username cannot be empty"));
}
if RESERVED_NAMES.contains(&name) {
return Err(ValidationError::new("This is a reserved username"));
}
Ok(())
}
fn validate_camera_config(camera_config: &CameraConfig) -> Result<(), ValidationError> {
match (&camera_config.camera_addr, &camera_config.camera_uid) {
(None, None) => Err(ValidationError::new(
"Either camera address or uid must be given",
)),
(Some(_), Some(_)) => Err(ValidationError::new(
"Must provide either camera address or uid not both",
)),
_ => Ok(()),
}
}