Added validation to the config file

This commit is contained in:
QuantumEntangledAndy committed 2020-07-01 11:34:19 +07:00
1 parent 7ab104a4ac
commit c4e3745d27
3 files changed
+28 -2

No files matched your search

+4
View File
@@ -17,10 +17,12 @@ gstreamer = "0.15"
gstreamer-app = "0.15"
gstreamer-rtsp = "0.15"
gstreamer-rtsp-server = "0.15"
lazy_static = "1.4"
log = { version = "0.4" }
indoc = "0.3"
md5 = "0.7"
nom = "5.1"
regex = "1"
serde = { version = "1.0", features = ["derive"] }
socket2 = "0.3"
structopt = "0.3"
@@ -28,3 +30,5 @@ toml = "0.5"
yaserde = "0.3.16"
yaserde_derive = "0.3.16"
xml-rs = "0.8"
validator = "0.10"
validator_derive = "0.10"
+10 -1
View File
@@ -1,6 +1,13 @@
use serde::Deserialize;
use std::net::SocketAddr;
use std::time::Duration;
use validator::Validate;
use regex::Regex;
lazy_static! {
static ref RE_STREAM_FORM: Regex = Regex::new(r"^([hH]26[45]|[ \t]*[!].*)$").unwrap();
static ref RE_STREAM_SRC: Regex = Regex::new(r"^(mainStream|subStream)$").unwrap();
}
#[derive(Debug, Deserialize)]
pub struct Config {
@@ -10,7 +17,7 @@ pub struct Config {
pub bind_addr: String,
}
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Validate)]
pub struct CameraConfig {
pub name: String,
@@ -22,9 +29,11 @@ pub struct CameraConfig {
pub timeout: Option<Duration>,
#[validate(regex(path = "RE_STREAM_FORM", message = "Incorrect stream format", code = "format"))]
#[serde(default = "default_format")]
pub format: String,
#[validate(regex(path = "RE_STREAM_SRC", message = "Incorrect stream source", code = "stream"))]
#[serde(default = "default_stream")]
pub stream: String,
}
+14 -1
View File
@@ -1,13 +1,17 @@
#[macro_use] extern crate validator_derive;
#[macro_use] extern crate lazy_static;
use env_logger::Env;
use err_derive::Error;
use log::*;
use neolink::bc_protocol::BcCamera;
use neolink::gst::{MaybeAppSrc, RtspServer};
use neolink::gst::{MaybeAppSrc, RtspServer, StreamFormat};
use neolink::Never;
use std::fs;
use std::io::Write;
use std::time::Duration;
use structopt::StructOpt;
use validator::Validate;
mod cmdline;
mod config;
@@ -23,6 +27,8 @@ pub enum Error {
ProtocolError(#[error(source)] neolink::Error),
#[error(display = "I/O error")]
IoError(#[error(source)] std::io::Error),
#[error(display = "Validation error")]
ValidationError(#[error(source)] validator::ValidationErrors),
}
fn main() -> Result<(), Error> {
@@ -37,6 +43,13 @@ fn main() -> Result<(), Error> {
let opt = Opt::from_args();
let config: Config = toml::from_str(&fs::read_to_string(opt.config)?)?;
for camera in &config.cameras {
match camera.validate() {
Ok(_) => (),
Err(e) => return Err(Error::ValidationError(e)),
};
}
let rtsp = &RtspServer::new();
crossbeam::scope(|s| {