Skip to content

Commit

Permalink
fix: validate configuration before attempting to use
Browse files Browse the repository at this point in the history
closes #38
  • Loading branch information
crazyscot committed Jan 8, 2025
1 parent 3098288 commit d3f13ec
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ derive-deftly = "0.14.2"
dirs = "5.0.1"
dns-lookup = "2.0.4"
document-features = "0.2.10"
engineering-repr = { version = "0.3.0", features = [ "serde" ] }
engineering-repr = { version = "0.3.1", features = [ "serde" ] }
expanduser = "1.2.2"
figment = { version = "0.10.19" }
futures-util = { version = "0.3.31", default-features = false }
Expand Down
3 changes: 2 additions & 1 deletion src/cli/cli_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ pub async fn cli() -> anyhow::Result<ExitCode> {
err.into_iter().for_each(|e| eprintln!("{e}"));
return Ok(ExitCode::FAILURE);
}
};
}
.validate()?;

setup_tracing(
trace_level(&args.client_params),
Expand Down
39 changes: 38 additions & 1 deletion src/config/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::time::Duration;

use anyhow::Result;
use clap::Parser;
use engineering_repr::EngineeringQuantity;
use engineering_repr::{EngineeringQuantity, EngineeringRepr};
use human_repr::{HumanCount as _, HumanDuration as _};
use serde::{Deserialize, Serialize};
use struct_field_names_as_array::FieldNamesAsSlice;
Expand All @@ -17,6 +17,10 @@ use crate::{

use derive_deftly::Deftly;

/// Minimum bandwidth we will accept in either direction.
/// You have to have a limit somewhere; zero doesn't work. So I chose 1200 baud ...
const MINIMUM_BANDWIDTH: u64 = 150;

/// The set of configurable options supported by qcp.
///
/// **Note:** The implementation of `default()` for this struct returns qcp's hard-wired configuration defaults.
Expand Down Expand Up @@ -285,6 +289,39 @@ impl Configuration {
congestion = self.congestion,
)
}

/// Performs additional validation checks on the configuration.
pub fn validate(self) -> Result<Self> {
if self.rx() < MINIMUM_BANDWIDTH {
anyhow::bail!(
"The receive bandwidth (rx {}B) is too small; it must be at least {}",
self.rx.with_precision(0),
MINIMUM_BANDWIDTH.to_eng(3)
);
}
if self.tx() < MINIMUM_BANDWIDTH {
anyhow::bail!(
"The transmit bandwidth (rx {}B) is too small; it must be at least {}",
self.tx.with_precision(0),
MINIMUM_BANDWIDTH.to_eng(3)
);
}
if self.rx().checked_mul(self.rtt.into()).is_none() {
anyhow::bail!(
"The receive bandwidth delay product calculation (rx {}B x rtt {}ms) overflowed",
self.rx,
self.rtt
);
}
if self.tx().checked_mul(self.rtt.into()).is_none() {
anyhow::bail!(
"The transmit bandwidth delay product calculation (rx {}B x rtt {}ms) overflowed",
self.tx,
self.rtt
);
}
Ok(self)
}
}

impl Default for Configuration {
Expand Down

0 comments on commit d3f13ec

Please sign in to comment.