Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
bike
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
hauspie-ens
m1
bike
Commits
78e4db31
Commit
78e4db31
authored
1 month ago
by
Michael Hauspie
Browse files
Options
Downloads
Patches
Plain Diff
Add an error on the BikeBuilder::build() function
parent
ed68c142
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main.rs
+18
-17
18 additions, 17 deletions
src/main.rs
with
18 additions
and
17 deletions
src/main.rs
+
18
−
17
View file @
78e4db31
#![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
();
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment