Skip to content

Commit

Permalink
Apply minor refactoring on builders
Browse files Browse the repository at this point in the history
  • Loading branch information
reinterpretcat committed Aug 4, 2023
1 parent ed61210 commit 2403736
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 40 deletions.
18 changes: 8 additions & 10 deletions vrp-core/src/models/goal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,7 @@ pub type StateKey = i32;

/// Provides a way to build feature with some checks.
#[derive(Default)]
pub struct FeatureBuilder {
feature: Feature,
}
pub struct FeatureBuilder(Feature);

impl FeatureBuilder {
/// Combines multiple features into one.
Expand All @@ -198,19 +196,19 @@ impl FeatureBuilder {
}

/// Creates a builder from another feature
pub fn from_feature(other: Feature) -> Self {
Self { feature: other }
pub fn from_feature(feature: Feature) -> Self {
Self(feature)
}

/// Sets given name.
pub fn with_name(mut self, name: &str) -> Self {
self.feature.name = name.to_string();
self.0.name = name.to_string();
self
}

/// Adds given constraint.
pub fn with_constraint<T: FeatureConstraint + Send + Sync + 'static>(mut self, constraint: T) -> Self {
self.feature.constraint = Some(Arc::new(constraint));
self.0.constraint = Some(Arc::new(constraint));
self
}

Expand All @@ -219,19 +217,19 @@ impl FeatureBuilder {
mut self,
objective: T,
) -> Self {
self.feature.objective = Some(Arc::new(objective));
self.0.objective = Some(Arc::new(objective));
self
}

/// Adds given state.
pub fn with_state<T: FeatureState + Send + Sync + 'static>(mut self, state: T) -> Self {
self.feature.state = Some(Arc::new(state));
self.0.state = Some(Arc::new(state));
self
}

/// Tries to builds a feature.
pub fn build(self) -> Result<Feature, GenericError> {
let feature = self.feature;
let feature = self.0;

if feature.name == String::default() {
return Err("features with default id are not allowed".into());
Expand Down
18 changes: 8 additions & 10 deletions vrp-core/tests/helpers/models/problem/fleet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,44 +69,42 @@ pub fn get_test_actor_from_fleet(fleet: &Fleet, vehicle_id: &str) -> Arc<Actor>
fleet.actors.iter().find(|actor| get_vehicle_id(&actor.vehicle) == vehicle_id).unwrap().clone()
}

pub struct VehicleBuilder {
vehicle: Vehicle,
}
pub struct VehicleBuilder(Vehicle);

impl Default for VehicleBuilder {
fn default() -> VehicleBuilder {
VehicleBuilder { vehicle: test_vehicle(0) }
Self(test_vehicle(0))
}
}

impl VehicleBuilder {
pub fn id(&mut self, id: &str) -> &mut VehicleBuilder {
self.vehicle.dimens.set_id(id);
self.0.dimens.set_id(id);
self
}

pub fn profile(&mut self, profile: Profile) -> &mut VehicleBuilder {
self.vehicle.profile = profile;
self.0.profile = profile;
self
}

pub fn capacity(&mut self, capacity: i32) -> &mut VehicleBuilder {
self.vehicle.dimens.set_capacity(SingleDimLoad::new(capacity));
self.0.dimens.set_capacity(SingleDimLoad::new(capacity));
self
}

pub fn costs(&mut self, costs: Costs) -> &mut VehicleBuilder {
self.vehicle.costs = costs;
self.0.costs = costs;
self
}

pub fn details(&mut self, details: Vec<VehicleDetail>) -> &mut VehicleBuilder {
self.vehicle.details = details;
self.0.details = details;
self
}

pub fn build(&mut self) -> Vehicle {
std::mem::replace(&mut self.vehicle, test_vehicle(0))
std::mem::replace(&mut self.0, test_vehicle(0))
}
}

Expand Down
22 changes: 10 additions & 12 deletions vrp-core/tests/helpers/models/problem/jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,49 +71,47 @@ pub fn get_job_id(job: &Job) -> &String {
job.dimens().get_id().unwrap()
}

pub struct SingleBuilder {
single: Single,
}
pub struct SingleBuilder(Single);

impl Default for SingleBuilder {
fn default() -> Self {
Self { single: test_single() }
Self(test_single())
}
}

impl SingleBuilder {
pub fn id(&mut self, id: &str) -> &mut Self {
self.single.dimens.set_value("id", id.to_string());
self.0.dimens.set_value("id", id.to_string());
self
}

pub fn dimens(&mut self, dimens: Dimensions) -> &mut Self {
self.single.dimens = dimens;
self.0.dimens = dimens;
self
}

pub fn location(&mut self, loc: Option<Location>) -> &mut Self {
self.single.places.first_mut().unwrap().location = loc;
self.0.places.first_mut().unwrap().location = loc;
self
}

pub fn duration(&mut self, dur: Duration) -> &mut Self {
self.single.places.first_mut().unwrap().duration = dur;
self.0.places.first_mut().unwrap().duration = dur;
self
}

pub fn times(&mut self, times: Vec<TimeWindow>) -> &mut Self {
self.single.places.first_mut().unwrap().times = times.into_iter().map(TimeSpan::Window).collect();
self.0.places.first_mut().unwrap().times = times.into_iter().map(TimeSpan::Window).collect();
self
}

pub fn demand(&mut self, demand: Demand<SingleDimLoad>) -> &mut Self {
self.single.dimens.set_demand(demand);
self.0.dimens.set_demand(demand);
self
}

pub fn places(&mut self, places: Vec<TestPlace>) -> &mut Self {
self.single.places = places
self.0.places = places
.into_iter()
.map(|p| Place {
location: p.0,
Expand All @@ -126,7 +124,7 @@ impl SingleBuilder {
}

pub fn build(&mut self) -> Single {
std::mem::replace(&mut self.single, test_single())
std::mem::replace(&mut self.0, test_single())
}

pub fn build_shared(&mut self) -> Arc<Single> {
Expand Down
14 changes: 6 additions & 8 deletions vrp-core/tests/helpers/models/solution/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,33 +126,31 @@ fn create_route(actor: Arc<Actor>, mut tour: Tour, activities: Vec<Activity>) ->
Route { actor, tour }
}

pub struct ActivityBuilder {
activity: Activity,
}
pub struct ActivityBuilder(Activity);

impl Default for ActivityBuilder {
fn default() -> Self {
Self { activity: test_activity() }
Self(test_activity())
}
}

impl ActivityBuilder {
pub fn place(&mut self, place: Place) -> &mut Self {
self.activity.place = place;
self.0.place = place;
self
}

pub fn schedule(&mut self, schedule: Schedule) -> &mut Self {
self.activity.schedule = schedule;
self.0.schedule = schedule;
self
}

pub fn job(&mut self, job: Option<Arc<Single>>) -> &mut Self {
self.activity.job = job;
self.0.job = job;
self
}

pub fn build(&mut self) -> Activity {
std::mem::replace(&mut self.activity, test_activity())
std::mem::replace(&mut self.0, test_activity())
}
}

0 comments on commit 2403736

Please sign in to comment.