Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/commands/delete_command/delete_endpoint_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ impl DeleteEndpointCommand {

pub fn execute(&self, config: &mut HtrsConfig) -> Result<HtrsAction, HtrsError> {
let Some(service) = config.get_service_mut(&self.service) else {
return Err(HtrsError::new(format!("No service could be found with name or alias `{}`", self.service).as_str()));
return Err(HtrsError::aliased_item_not_found("service", self.service.as_str()));
};
match service.remove_endpoint(&self.name) {
true => Ok(UpdateConfig),
false => Err(HtrsError::new(format!("No endpoint could be found with name `{}` for service `{}`", self.name, service.name).as_str()))
false => Err(HtrsError::item_not_found("endpoint", self.name.as_str()))
}
}
}
4 changes: 2 additions & 2 deletions src/commands/delete_command/delete_environment_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ impl DeleteEnvironmentCommand {

pub fn execute(&self, config: &mut HtrsConfig) -> Result<HtrsAction, HtrsError> {
let Some(service) = config.get_service_mut(&self.service) else {
return Err(HtrsError::new(format!("No service could be found with name or alias `{}`", self.service).as_str()))
return Err(HtrsError::aliased_item_not_found("service", self.service.as_str()));
};
match service.remove_environment(&self.name) {
true => Ok(UpdateConfig),
false => Err(HtrsError::new(format!("No environment could be found with name or alias `{}`", self.name).as_str()))
false => Err(HtrsError::aliased_item_not_found("environment", self.name.as_str()))
}
}
}
6 changes: 3 additions & 3 deletions src/commands/delete_command/delete_header_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ impl DeleteHeaderCommand {

(Some(service_name), None) => {
let Some(service) = config.get_service_mut(service_name) else {
return Err(HtrsError::new(format!("Unable to find service with name or alias `{}`", service_name).as_str()))
return Err(HtrsError::aliased_item_not_found("service", service_name));
};
service
},

(Some(service_name), Some(environment_name)) => {
let Some(service) = config.get_service_mut(service_name) else {
return Err(HtrsError::new(format!("Unable to find service with name or alias `{}`", service_name).as_str()))
return Err(HtrsError::aliased_item_not_found("service", service_name));
};
let Some(environment) = service.get_environment_mut(environment_name) else {
return Err(HtrsError::new(format!("Unable to find environment with name or alias `{}` for service `{}`", environment_name, service.name).as_str()))
return Err(HtrsError::aliased_item_not_found("environment", environment_name));
};
environment
},
Expand Down
2 changes: 1 addition & 1 deletion src/commands/delete_command/delete_preset_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl DeletePresetCommand {
pub fn execute(&self, config: &mut HtrsConfig) -> Result<HtrsAction, HtrsError> {
match config.remove_preset(self.name.as_str()) {
true => Ok(UpdateConfig),
false => Err(HtrsError::new(format!("Unable to find preset with name `{}`", self.name).as_str())),
false => Err(HtrsError::aliased_item_not_found("preset", self.name.as_str())),
}
}
}
2 changes: 1 addition & 1 deletion src/commands/delete_command/delete_service_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl DeleteServiceCommand {
pub fn execute(&self, config: &mut HtrsConfig) -> Result<HtrsAction, HtrsError> {
match config.remove_service(&self.name) {
true => Ok(HtrsAction::UpdateConfig),
false => Err(HtrsError::new(format!("No service could be found with name or alias `{}`", self.name).as_str())),
false => Err(HtrsError::aliased_item_not_found("service", self.name.as_str())),
}
}
}
10 changes: 5 additions & 5 deletions src/commands/edit_command/edit_endpoint_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ impl EditEndpointCommand {
}

pub fn execute(&self, config: &mut HtrsConfig) -> Result<HtrsAction, HtrsError> {
let Some(service) = config.get_service_mut(&self.service) else {
return Err(HtrsError::new(format!("No service could be found with name or alias `{}`", self.service).as_str()))
let Some(service) = config.get_service_mut(self.service.as_str()) else {
return Err(HtrsError::aliased_item_not_found("service", self.service.as_str()));
};
if service.get_endpoint(&self.name).is_none() {
return Err(HtrsError::new(format!("No endpoint could be found with name `{}` for service `{}`", self.name, service.name).as_str()));
if service.get_endpoint(self.name.as_str()).is_none() {
return Err(HtrsError::item_not_found("endpoint", self.name.as_str()));
}

if let Some(new_name) = &self.new_name {
if service.get_endpoint(new_name).is_some() {
return Err(HtrsError::new(format!("An endpoint already exists with name `{}` for service `{}`", new_name, service.name).as_str()));
return Err(HtrsError::item_already_exists("endpoint", new_name));
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/commands/edit_command/edit_environment_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ impl EditEnvironmentCommand {

pub fn execute(&self, config: &mut HtrsConfig) -> Result<HtrsAction, HtrsError> {
let Some(service) = config.get_service_mut(&self.service) else {
return Err(HtrsError::new(format!("No service could be found with name or alias `{}`", self.service).as_ref()))
return Err(HtrsError::aliased_item_not_found("service", self.service.as_str()))
};
if service.get_environment_mut(&self.name).is_none() {
return Err(HtrsError::new(format!("No environment could be found with name or alias `{}`", self.name).as_ref()))
return Err(HtrsError::aliased_item_not_found("environment", self.name.as_str()))
};

if self.new_name.is_some() && service.get_environment_mut(self.new_name.as_ref().unwrap()).is_some() {
Expand Down
24 changes: 24 additions & 0 deletions src/outcomes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@ impl HtrsError {
pub fn new(msg: &str) -> HtrsError {
HtrsError { details: msg.to_string() }
}

pub fn item_not_found(name: &str, searched_term: &str) -> Self {
Self {
details: format!("No {name} could be found with name `{searched_term}`")
}
}

pub fn aliased_item_not_found(name: &str, searched_term: &str) -> Self {
Self {
details: format!("No {name} could be found with name or alias `{searched_term}`")
}
}

pub fn item_already_exists(name: &str, conflicting_term: &str) -> Self {
Self {
details: format!("A {name} already exists with name `{conflicting_term}")
}
}

pub fn aliased_item_already_exists(name: &str, conflicting_term: &str) -> Self {
Self {
details: format!("A {name} already exists with name or alias `{conflicting_term}`")
}
}
}

impl fmt::Display for HtrsError {
Expand Down
6 changes: 3 additions & 3 deletions tests/delete_header_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ mod delete_header_tests {
.arg("foo_service")
.assert()
.failure()
.stdout("Unable to find service with name or alias `foo_service`\n");
.stdout("No service could be found with name or alias `foo_service`\n");

clear_config(&path);
Ok(())
Expand Down Expand Up @@ -161,7 +161,7 @@ mod delete_header_tests {
.arg("foo_environment")
.assert()
.failure()
.stdout("Unable to find service with name or alias `foo_service`\n");
.stdout("No service could be found with name or alias `foo_service`\n");

clear_config(&path);
Ok(())
Expand All @@ -188,7 +188,7 @@ mod delete_header_tests {
.arg("foo_environment")
.assert()
.failure()
.stdout("Unable to find environment with name or alias `foo_environment` for service `foo_service`\n");
.stdout("No environment could be found with name or alias `foo_environment`\n");

clear_config(&path);
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion tests/endpoint/delete_endpoint_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ mod delete_endpoint_tests {
.arg("foo_service")
.assert()
.failure()
.stdout("No endpoint could be found with name `foo_endpoint` for service `foo_service`\n");
.stdout("No endpoint could be found with name `foo_endpoint`\n");

clear_config(&path);
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion tests/endpoint/edit_endpoint_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ mod edit_endpoint_tests {
.arg("foo_service")
.assert()
.failure()
.stdout("No endpoint could be found with name `foo_endpoint` for service `foo_service`\n");
.stdout("No endpoint could be found with name `foo_endpoint`\n");

clear_config(&path);
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion tests/presets/delete_preset_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod delete_preset_tests {
.arg("unknown_preset")
.assert()
.failure()
.stdout("Unable to find preset with name `unknown_preset`\n");
.stdout("No preset could be found with name or alias `unknown_preset`\n");

clear_config(&path);
Ok(())
Expand Down
Loading