use super::codes::{Unadmitted, lookup};
use crate::structures::{CulvertBarrelShape, StructureError};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CulvertInletForm {
One,
Two,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CulvertInlet {
code: Option<u8>,
shape: Option<CulvertBarrelShape>,
form: CulvertInletForm,
k: f64,
m: f64,
c: f64,
y: f64,
mitered: bool,
}
impl CulvertInlet {
pub fn hds5(code: u8) -> Result<Self, StructureError> {
let entry = lookup(code).map_err(|reason| {
let action = "an HDS-5 code from 1 to 26 or 30 to 47 for the barrel shape, or explicit coefficients";
StructureError::definition(match reason {
Unadmitted::MetalBox => format!(
"inlet code={code}: HDS-5 codes 27 to 29 are corrugated metal box inlets, and no barrel shape models a corrugated metal box. Supply {action}"
),
Unadmitted::Tapered => format!(
"inlet code={code}: HDS-5 codes 48 to 57 are tapered inlets, which need throat and face geometry. Supply {action}"
),
Unadmitted::Unknown => format!("inlet code={code}: supply {action}"),
})
})?;
Ok(Self {
code: Some(code),
shape: Some(entry.shape),
form: entry.form,
k: entry.k,
m: entry.m,
c: entry.c,
y: entry.y,
mitered: entry.mitered,
})
}
pub fn explicit(
form: CulvertInletForm,
[k, m, c, y]: [f64; 4],
mitered: bool,
) -> Result<Self, StructureError> {
let fields = format!("form={form:?},k={k:?},m={m:?},c={c:?},y={y:?}");
for (name, value) in [("K", k), ("M", m), ("c", c)] {
if !(value.is_finite() && value > 0.0) {
return Err(StructureError::definition(format!(
"{fields}: supply a finite positive inlet coefficient {name}"
)));
}
}
if !y.is_finite() {
return Err(StructureError::definition(format!(
"{fields}: supply a finite inlet coefficient Y"
)));
}
Ok(Self {
code: None,
shape: None,
form,
k,
m,
c,
y,
mitered,
})
}
pub fn code(&self) -> Option<u8> {
self.code
}
pub fn barrel_shape(&self) -> Option<CulvertBarrelShape> {
self.shape
}
pub fn form(&self) -> CulvertInletForm {
self.form
}
pub fn coefficients(&self) -> [f64; 4] {
[self.k, self.m, self.c, self.y]
}
pub fn mitered(&self) -> bool {
self.mitered
}
pub(crate) fn describe(&self) -> String {
let coefficients = format!(
"form={:?},k={:?},m={:?},c={:?},y={:?},mitered={}",
self.form, self.k, self.m, self.c, self.y, self.mitered
);
match self.code {
Some(code) => format!("inlet code={code},{coefficients}"),
None => format!("explicit inlet {coefficients}"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::structures::StructureInputErrorKind;
fn message(result: Result<CulvertInlet, StructureError>) -> Option<String> {
match result {
Err(StructureError::InvalidInput {
kind: StructureInputErrorKind::Definition,
context,
element: None,
}) => Some(context),
_ => None,
}
}
#[test]
fn codes_carry_their_shape_form_and_mitered_flag() -> Result<(), StructureError> {
let mitered = CulvertInlet::hds5(5)?;
assert!(mitered.mitered());
assert_eq!(mitered.barrel_shape(), Some(CulvertBarrelShape::Circular));
assert_eq!(mitered.coefficients(), [0.0210, 1.33, 0.0463, 0.75]);
let boxed = CulvertInlet::hds5(26)?;
assert_eq!(boxed.form(), CulvertInletForm::Two);
assert_eq!(boxed.barrel_shape(), Some(CulvertBarrelShape::Box));
assert_eq!(boxed.code(), Some(26));
let unmitered = (1..=26)
.chain(30..=47)
.filter(|&code| ![5, 37, 46].contains(&code));
for code in unmitered {
assert!(!CulvertInlet::hds5(code)?.mitered(), "code {code}");
}
let shapes = [
(30..=32, CulvertBarrelShape::HorizontalEllipse),
(33..=35, CulvertBarrelShape::VerticalEllipse),
(36..=44, CulvertBarrelShape::PipeArch),
(45..=47, CulvertBarrelShape::Arch),
];
for (codes, shape) in shapes {
for code in codes {
let inlet = CulvertInlet::hds5(code)?;
assert_eq!(inlet.barrel_shape(), Some(shape), "code {code}");
assert_eq!(inlet.form(), CulvertInletForm::One, "code {code}");
assert_eq!(inlet.code(), Some(code));
}
}
for code in [37, 46] {
assert!(CulvertInlet::hds5(code)?.mitered(), "code {code}");
}
assert_eq!(CulvertInlet::hds5(38)?.coefficients()[3], 0.57);
Ok(())
}
#[test]
fn admission_names_the_value_and_action() {
let cases = [
(
CulvertInlet::hds5(0),
"inlet code=0: supply an HDS-5 code from 1 to 26 or 30 to 47 for the barrel shape",
),
(
CulvertInlet::hds5(58),
"inlet code=58: supply an HDS-5 code from 1 to 26 or 30 to 47",
),
(
CulvertInlet::hds5(27),
"inlet code=27: HDS-5 codes 27 to 29 are corrugated metal box inlets",
),
(
CulvertInlet::hds5(29),
"no barrel shape models a corrugated metal box. Supply an HDS-5 code from 1 to 26 or 30 to 47",
),
(
CulvertInlet::hds5(48),
"inlet code=48: HDS-5 codes 48 to 57 are tapered inlets, which need throat and face geometry",
),
(
CulvertInlet::hds5(57),
"inlet code=57: HDS-5 codes 48 to 57 are tapered inlets",
),
(
CulvertInlet::explicit(CulvertInletForm::One, [0.0, 2.0, 0.04, 0.7], false),
"k=0.0,m=2.0,c=0.04,y=0.7: supply a finite positive inlet coefficient K",
),
(
CulvertInlet::explicit(CulvertInletForm::Two, [0.5, f64::NAN, 0.04, 0.7], false),
"m=NaN,c=0.04,y=0.7: supply a finite positive inlet coefficient M",
),
(
CulvertInlet::explicit(CulvertInletForm::Two, [0.5, 0.6, -0.04, 0.7], true),
"supply a finite positive inlet coefficient c",
),
(
CulvertInlet::explicit(
CulvertInletForm::One,
[0.5, 0.6, 0.04, f64::INFINITY],
false,
),
"y=inf: supply a finite inlet coefficient Y",
),
];
for (result, expected) in cases {
let text = message(result);
assert!(
text.as_deref().is_some_and(|t| t.contains(expected)),
"{text:?} lacks {expected:?}"
);
}
}
}