1use std::fmt;
18
19pub const BAG_YIELD: [(u32, f64); 3] = [(80, 0.60), (60, 0.45), (40, 0.30)];
21
22#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct InputError(pub String);
25impl fmt::Display for InputError {
26 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(&self.0) }
27}
28impl std::error::Error for InputError {}
29
30type R<T> = Result<T, InputError>;
31
32fn pos(name: &str, v: f64) -> R<f64> {
33 if v.is_finite() && v > 0.0 { Ok(v) } else { Err(InputError(format!("{name} must be a positive number"))) }
34}
35fn r2(x: f64) -> f64 { (x * 100.0 + 0.5 + 1e-9).floor() / 100.0 }
36fn ceil6(x: f64) -> u64 { ((x * 1e6).round() / 1e6).ceil() as u64 }
37
38pub fn slab_volume(length_ft: f64, width_ft: f64, thickness_in: f64) -> R<f64> {
40 Ok(pos("length", length_ft)? * pos("width", width_ft)? * pos("thickness", thickness_in)? / 12.0)
41}
42pub fn footing_volume(length_ft: f64, width_in: f64, depth_in: f64) -> R<f64> {
44 Ok(pos("length", length_ft)? * pos("width", width_in)? / 12.0 * pos("depth", depth_in)? / 12.0)
45}
46pub fn column_volume(diameter_in: f64, height_ft: f64, count: u32) -> R<f64> {
48 let r = pos("diameter", diameter_in)? / 24.0;
49 Ok(std::f64::consts::PI * r * r * pos("height", height_ft)? * pos("count", count as f64)?)
50}
51pub fn steps_volume(steps: u32, rise_in: f64, run_in: f64, width_ft: f64) -> R<f64> {
53 let n = pos("steps", steps as f64)?;
54 Ok(pos("width", width_ft)? * pos("run", run_in)? / 12.0 * pos("rise", rise_in)? / 12.0 * n * (n + 1.0) / 2.0)
55}
56
57#[derive(Debug, Clone, PartialEq)]
59pub struct Estimate {
60 pub cubic_feet: f64,
61 pub cubic_yards: f64,
62 pub order_yards: f64,
63 pub bags80: u64,
64 pub bags60: u64,
65 pub bags40: u64,
66 pub suggestion: &'static str,
68}
69
70pub fn estimate(cubic_feet: f64, waste: f64) -> R<Estimate> {
72 pos("volume", cubic_feet)?;
73 if !waste.is_finite() || !(0.0..=1.0).contains(&waste) {
74 return Err(InputError("waste is a fraction between 0 and 1".into()));
75 }
76 let v = cubic_feet * (1.0 + waste);
77 let yd = v / 27.0;
78 Ok(Estimate {
79 cubic_feet: r2(v),
80 cubic_yards: r2(yd),
81 order_yards: ((yd * 4.0 * 1e6).round() / 1e6).ceil() / 4.0,
82 bags80: ceil6(v / 0.60),
83 bags60: ceil6(v / 0.45),
84 bags40: ceil6(v / 0.30),
85 suggestion: if yd < 1.0 { "bags" } else { "ready-mix" },
86 })
87}
88
89#[derive(Debug, Clone, PartialEq)]
91pub struct Rebar { pub bars_along_length: u64, pub bars_along_width: u64, pub linear_feet: f64, pub sticks20: u64 }
92
93pub fn rebar(length_ft: f64, width_ft: f64, spacing_in: f64) -> R<Rebar> {
95 pos("length", length_ft)?; pos("width", width_ft)?; pos("spacing", spacing_in)?;
96 let al = (width_ft * 12.0 / spacing_in).floor() as u64 + 1;
97 let aw = (length_ft * 12.0 / spacing_in).floor() as u64 + 1;
98 let lf = al as f64 * length_ft + aw as f64 * width_ft;
99 Ok(Rebar { bars_along_length: al, bars_along_width: aw, linear_feet: r2(lf), sticks20: ceil6(lf * 1.1 / 20.0) })
100}
101
102pub fn mesh_sheets(area_sq_ft: f64) -> R<u64> { Ok(ceil6(pos("area", area_sq_ft)? * 1.1 / 50.0)) }
104
105#[derive(Debug, Clone, Copy, PartialEq, Eq)]
107pub enum Finish { Broom, Colored, Exposed, Stamped }
108impl Finish {
109 pub fn rate(self) -> (f64, f64) {
111 match self { Finish::Broom => (6.0, 10.0), Finish::Colored => (8.0, 13.0), Finish::Exposed => (9.0, 14.0), Finish::Stamped => (12.0, 20.0) }
112 }
113}
114impl std::str::FromStr for Finish {
115 type Err = InputError;
116 fn from_str(s: &str) -> R<Self> {
117 match s { "broom" => Ok(Finish::Broom), "colored" => Ok(Finish::Colored), "exposed" => Ok(Finish::Exposed), "stamped" => Ok(Finish::Stamped),
118 _ => Err(InputError("finish must be one of: broom, colored, exposed, stamped".into())) }
119 }
120}
121
122pub fn cost_range(area_sq_ft: f64, finish: Finish, thickness_in: f64, remove_old: bool) -> R<(u64, u64)> {
124 pos("area", area_sq_ft)?; pos("thickness", thickness_in)?;
125 let (lo, hi) = finish.rate();
126 let m = if thickness_in > 4.0 { 1.0 + 0.1 * (thickness_in - 4.0) } else { 1.0 };
127 let (dl, dh) = if remove_old { (2.0, 4.0) } else { (0.0, 0.0) };
128 Ok(((area_sq_ft * (lo * m + dl) + 0.5).floor() as u64, (area_sq_ft * (hi * m + dh) + 0.5).floor() as u64))
129}
130
131#[derive(Debug, Clone, PartialEq)]
133pub struct PourAdvice { pub risk: &'static str, pub start: &'static str, pub tips: Vec<&'static str>, pub monsoon: bool }
134
135pub const CURE_DAYS: (u32, u32, u32, u32) = (2, 7, 7, 28);
137
138pub fn pour_advice(forecast_high_f: f64, month: Option<u32>) -> R<PourAdvice> {
140 if !forecast_high_f.is_finite() { return Err(InputError("forecast high must be a number".into())); }
141 if let Some(m) = month { if !(1..=12).contains(&m) { return Err(InputError("month must be 1-12".into())); } }
142 let t = forecast_high_f;
143 let mut tips = vec!["Check the slump on arrival and do not add extra water to make it easier to place."];
144 let (risk, start) = if t < 90.0 { ("low", "Any time; finish before the afternoon peak.") }
145 else if t < 100.0 { ("moderate", "Start by 7:00 am.") }
146 else if t < 110.0 { ("high", "Start 5:00-6:00 am and finish by 10:00 am.") }
147 else { ("extreme", "Reschedule if you can; otherwise start at first light and finish by 9:00 am.") };
148 if t >= 90.0 {
149 tips.push("Pre-wet the subgrade and forms so dry desert soil does not pull water out of the mix.");
150 tips.push("Have the finishing crew and tools ready before the truck arrives; hot concrete sets fast.");
151 }
152 if t >= 100.0 {
153 tips.push("Ask the plant for a set-retarding admixture and keep the mix below 90 F (ice or chilled water).");
154 tips.push("Put up shade or windbreaks; low humidity and wind cause plastic shrinkage cracks.");
155 }
156 if t >= 110.0 { tips.push("Use a smaller crew-sized pour or split the job; do not place more than you can finish in two hours."); }
157 tips.push("Start curing right after finishing: curing compound, wet burlap or plastic sheeting, kept moist for 7 days.");
158 let monsoon = matches!(month, Some(6..=9));
159 if monsoon { tips.push("Monsoon season: watch for afternoon storm cells, keep plastic on site and route runoff away from the fresh slab."); }
160 Ok(PourAdvice { risk, start, tips, monsoon })
161}
162
163#[cfg(test)]
164mod tests {
165 use super::*;
166 #[test]
167 fn slab_vector() {
168 let e = estimate(slab_volume(20.0, 10.0, 4.0).unwrap(), 0.1).unwrap();
169 assert_eq!(e, Estimate { cubic_feet: 73.33, cubic_yards: 2.72, order_yards: 2.75, bags80: 123, bags60: 163, bags40: 245, suggestion: "ready-mix" });
170 }
171 #[test]
172 fn shapes() {
173 assert_eq!(estimate(footing_volume(40.0, 12.0, 12.0).unwrap(), 0.1).unwrap().cubic_feet, 44.0);
174 assert_eq!(estimate(column_volume(12.0, 4.0, 4).unwrap(), 0.1).unwrap().bags80, 24);
175 let s = estimate(steps_volume(3, 7.0, 11.0, 4.0).unwrap(), 0.1).unwrap();
176 assert_eq!(s.cubic_feet, 14.12);
177 assert_eq!(s.suggestion, "bags");
178 assert_eq!(estimate(27.0, 0.0).unwrap().order_yards, 1.0);
179 }
180 #[test]
181 fn rebar_mesh_cost() {
182 assert_eq!(rebar(20.0, 10.0, 18.0).unwrap(), Rebar { bars_along_length: 7, bars_along_width: 14, linear_feet: 280.0, sticks20: 16 });
183 assert_eq!(mesh_sheets(200.0).unwrap(), 5);
184 assert_eq!(cost_range(200.0, Finish::Stamped, 5.0, true).unwrap(), (3040, 5200));
185 assert_eq!(cost_range(100.0, Finish::Broom, 4.0, false).unwrap(), (600, 1000));
186 assert!("gold".parse::<Finish>().is_err());
187 }
188 #[test]
189 fn pour() {
190 assert_eq!(pour_advice(85.0, None).unwrap().risk, "low");
191 assert_eq!(pour_advice(95.0, None).unwrap().risk, "moderate");
192 assert_eq!(pour_advice(105.0, None).unwrap().risk, "high");
193 let x = pour_advice(112.0, Some(7)).unwrap();
194 assert_eq!(x.risk, "extreme");
195 assert!(x.monsoon);
196 assert_eq!(x.tips.len(), 8);
197 assert_eq!(pour_advice(85.0, Some(1)).unwrap().tips.len(), 2);
198 assert!(pour_advice(100.0, Some(13)).is_err());
199 }
200 #[test]
201 fn validation() {
202 assert!(slab_volume(0.0, 10.0, 4.0).is_err());
203 assert!(estimate(10.0, 2.0).is_err());
204 assert!(steps_volume(0, 7.0, 11.0, 4.0).is_err());
205 }
206}