Skip to content
Snippets Groups Projects
Commit 78e4db31 authored by Michael Hauspie's avatar Michael Hauspie
Browse files

Add an error on the BikeBuilder::build() function

parent ed68c142
No related branches found
No related tags found
No related merge requests found
#![doc = include_str!("../README.md")]
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub enum Brake {
Disk,
Pads
Pads,
}
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub enum Wheel {
Large,
Small,
}
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub enum Transmission {
Standard,
GearHub,
......@@ -37,7 +36,7 @@ impl Bike {
}
}
#[derive(Default,Debug)]
#[derive(Default, Debug)]
/// A bike builder that helps the configuration of a bike.
pub struct BikeBuilder {
brakes: Option<Brake>,
......@@ -64,24 +63,26 @@ impl BikeBuilder {
/// Builds the bike from the builder.
///
/// Will panic if the builder configuration is not correct
pub fn build(self) -> Bike {
Bike {
wheels: self.wheels.unwrap(),
transmission: self.transmission.unwrap(),
brakes: self.brakes.unwrap(),
pub fn build(self) -> Result<Bike, String> {
match (self.wheels, self.transmission, self.brakes) {
(Some(w), Some(t), Some(b)) => Ok(Bike {
wheels: w,
transmission: t,
brakes: b,
}),
_ => Err(format!("The bike builder would create an invalid bike: {:?}", self))
}
}
}
fn main() {
let builder = Bike::builder();
let bike = builder.with_brakes(Brake::Disk)
.with_transmission(Transmission::GearHub)
let bike = builder
.with_brakes(Brake::Disk)
// .with_transmission(Transmission::GearHub)
.with_wheels(Wheel::Small)
.build();
.build().unwrap();
println!("{bike:#?}");
bike.ride();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment