Skip to content

Commit

Permalink
Updated based on reviews.
Browse files Browse the repository at this point in the history
  • Loading branch information
markmandel committed Jul 10, 2020
1 parent fd4cc0c commit 9b579f7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 41 deletions.
53 changes: 12 additions & 41 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,16 @@
* limitations under the License.
*/

use std::collections::HashMap;
use std::collections::HashSet;
use std::fmt;
use std::io;
use std::net::SocketAddr;
use std::{convert, io};

use serde::export::Formatter;
use serde::{Deserialize, Serialize};

/// Error type for Configuration
#[derive(Debug)]
pub enum Error {
Validation(ValidationError),
Deserialise(serde_yaml::Error),
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Error::Validation(err) => write!(f, "validation error: {}", err),
Error::Deserialise(err) => write!(f, "deserialisation error: {}", err),
}
}
}

impl convert::From<serde_yaml::Error> for Error {
fn from(err: serde_yaml::Error) -> Self {
Error::Deserialise(err)
}
}

impl convert::From<ValidationError> for Error {
fn from(err: ValidationError) -> Self {
Error::Validation(err)
}
}

/// Validation failure for a Config
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum ValidationError {
NotUnique(String),
}
Expand Down Expand Up @@ -128,21 +100,20 @@ pub struct EndPoint {

impl Config {
/// from_reader returns a config from a given Reader
pub fn from_reader<R: io::Read>(input: R) -> Result<Config, Error> {
let config: Config = serde_yaml::from_reader(input)?;
config.validate()?;
return Ok(config);
pub fn from_reader<R: io::Read>(input: R) -> Result<Config, serde_yaml::Error> {
serde_yaml::from_reader(input)
}

/// validates the current Config.
pub fn validate(&self) -> Result<(), ValidationError> {
if let ConnectionConfig::Server { endpoints } = &self.connections {
let set = endpoints.iter().fold(HashMap::new(), |mut map, item| {
map.insert(item.name.clone(), true);
map
});

if set.len() != endpoints.len() {
if endpoints
.iter()
.map(|ep| ep.name.clone())
.collect::<HashSet<_>>()
.len()
!= endpoints.len()
{
return Err(ValidationError::NotUnique("endpoint.name".to_string()));
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ async fn main() {
info!(log, "Starting Quilkin"; "version" => VERSION);

let config = Arc::new(Config::from_reader(File::open(filename).unwrap()).unwrap());
config.validate().unwrap();
let server = Server::new(base_logger, filter_registry);

let (close, stop) = oneshot::channel::<()>();
Expand Down
3 changes: 3 additions & 0 deletions tests/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ mod tests {
}],
},
};
assert_eq!(Ok(()), server_config.validate());

let mut registry = default_filters(&base_logger);
registry.insert("TestFilter".to_string(), TestFilter {});
Expand All @@ -71,6 +72,8 @@ mod tests {
lb_policy: None,
},
};
assert_eq!(Ok(()), client_config.validate());

let mut registry = default_filters(&base_logger);
registry.insert("TestFilter".to_string(), TestFilter {});
let close_client = run_proxy(&base_logger, registry, client_config);
Expand Down
2 changes: 2 additions & 0 deletions tests/no_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ mod tests {
],
},
};
assert_eq!(Ok(()), server_config.validate());

let close_server = run_proxy(&base_logger, default_filters(&base_logger), server_config);

Expand All @@ -72,6 +73,7 @@ mod tests {
lb_policy: None,
},
};
assert_eq!(Ok(()), client_config.validate());

let close_client = run_proxy(&base_logger, default_filters(&base_logger), client_config);

Expand Down

0 comments on commit 9b579f7

Please sign in to comment.