Change the type for port to u16 in config.

This commit is contained in:
Dru Sellers 2017-01-09 12:15:33 -06:00 committed by Sergio Benitez
parent 44296980fc
commit a6084ab3e2
3 changed files with 26 additions and 6 deletions

View File

@ -15,7 +15,7 @@ pub struct Config {
/// The address to serve on.
pub address: String,
/// The port to serve on.
pub port: usize,
pub port: u16,
/// How much information to log.
pub log_level: LoggingLevel,
/// The environment that this configuration corresponds to.
@ -99,7 +99,7 @@ impl Config {
/// returned:
///
/// * **address**: String
/// * **port**: Integer
/// * **port**: Integer (16-bit unsigned)
/// * **session_key**: String (192-bit base64)
/// * **log**: String
///
@ -119,7 +119,11 @@ impl Config {
return Err(self.bad_type(name, val, "an unsigned integer"));
}
self.port = port as usize;
if port > (u16::max_value() as i64) {
return Err(self.bad_type(name, val, "a 16-bit unsigned integer"))
}
self.port = port as u16;
} else if name == "session_key" {
let key = parse!(self, name, val, as_str, "a string")?;
if key.len() != 32 {
@ -231,7 +235,7 @@ impl Config {
/// Sets the `port` in `self` to `var` and returns the structure.
#[inline(always)]
pub fn port(mut self, var: usize) -> Self {
pub fn port(mut self, var: u16) -> Self {
self.port = var;
self
}
@ -284,4 +288,3 @@ impl PartialEq for Config {
&& self.filepath == other.filepath
}
}

View File

@ -567,6 +567,13 @@ mod test {
"#.to_string(), TEST_CONFIG_FILENAME), {
default_config(Staging).port(6000)
});
check_config!(RocketConfig::parse(r#"
[stage]
port = 65535
"#.to_string(), TEST_CONFIG_FILENAME), {
default_config(Staging).port(65535)
});
}
#[test]
@ -589,6 +596,16 @@ mod test {
[staging]
port = -1
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
assert!(RocketConfig::parse(r#"
[staging]
port = 65536
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
assert!(RocketConfig::parse(r#"
[staging]
port = 105836
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
}
#[test]

View File

@ -25,7 +25,7 @@ use http::uri::URI;
/// application.
pub struct Rocket {
address: String,
port: usize,
port: u16,
router: Router,
default_catchers: HashMap<u16, Catcher>,
catchers: HashMap<u16, Catcher>,