Skip to content

Commit

Permalink
Protobuf conversions -> Introduce shorthand for field_not_set errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfm committed Apr 19, 2024
1 parent ba12fae commit 068a7f1
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 130 deletions.
98 changes: 49 additions & 49 deletions opendut-carl/opendut-carl-api/src/proto/services.rs

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions opendut-types/src/proto/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl TryFrom<ClusterId> for crate::cluster::ClusterId {
type ErrorBuilder = ConversionErrorBuilder<ClusterId, crate::cluster::ClusterId>;

value.uuid
.ok_or(ErrorBuilder::new("Uuid not set"))
.ok_or(ErrorBuilder::field_not_set("uuid"))
.map(|uuid| Self(uuid.into()))
}
}
Expand Down Expand Up @@ -46,7 +46,7 @@ impl TryFrom<ClusterName> for crate::cluster::ClusterName {
type ErrorBuilder = ConversionErrorBuilder<ClusterName, crate::cluster::ClusterName>;

crate::cluster::ClusterName::try_from(value.value)
.map_err(|cause| ErrorBuilder::new(cause.to_string()))
.map_err(|cause| ErrorBuilder::message(cause.to_string()))
}
}

Expand All @@ -70,15 +70,15 @@ impl TryFrom<ClusterConfiguration> for crate::cluster::ClusterConfiguration {
type ErrorBuilder = ConversionErrorBuilder<ClusterConfiguration, crate::cluster::ClusterConfiguration>;

let cluster_id: crate::cluster::ClusterId = configuration.id
.ok_or(ErrorBuilder::new("Id not set"))?
.ok_or(ErrorBuilder::field_not_set("id"))?
.try_into()?;

let cluster_name: crate::cluster::ClusterName = configuration.name
.ok_or(ErrorBuilder::new("Name not set"))?
.ok_or(ErrorBuilder::field_not_set("name"))?
.try_into()?;

let leader: crate::peer::PeerId = configuration.leader
.ok_or(ErrorBuilder::new("Leader not set"))?
.ok_or(ErrorBuilder::field_not_set("leader"))?
.try_into()?;

Ok(Self {
Expand Down Expand Up @@ -107,7 +107,7 @@ impl TryFrom<ClusterDeployment> for crate::cluster::ClusterDeployment {
type ErrorBuilder = ConversionErrorBuilder<ClusterDeployment, crate::cluster::ClusterDeployment>;

let cluster_id: crate::cluster::ClusterId = deployment.id
.ok_or(ErrorBuilder::new("Id not set"))?
.ok_or(ErrorBuilder::field_not_set("id"))?
.try_into()?;

Ok(Self {
Expand Down Expand Up @@ -158,7 +158,7 @@ impl TryFrom<ClusterState> for crate::cluster::state::ClusterState {
type ErrorBuilder = ConversionErrorBuilder<ClusterState, crate::cluster::state::ClusterState>;

let inner = state.inner
.ok_or(ErrorBuilder::new("Inner state not set"))?;
.ok_or(ErrorBuilder::field_not_set("inner"))?;

match inner {
cluster_state::Inner::Undeployed(_) => {
Expand All @@ -169,7 +169,7 @@ impl TryFrom<ClusterState> for crate::cluster::state::ClusterState {
}
cluster_state::Inner::Deployed(state) => {
let inner = state.inner
.ok_or(ErrorBuilder::new("Inner state not set"))?;
.ok_or(ErrorBuilder::field_not_set("inner"))?;
let inner = match inner {
cluster_state_deployed::Inner::Unhealthy(_) => {
crate::cluster::state::DeployedClusterState::Unhealthy
Expand Down Expand Up @@ -200,11 +200,11 @@ impl TryFrom<ClusterAssignment> for crate::cluster::ClusterAssignment {
type ErrorBuilder = ConversionErrorBuilder<ClusterAssignment, crate::cluster::ClusterAssignment>;

let cluster_id: crate::cluster::ClusterId = value.id
.ok_or(ErrorBuilder::new("field 'id' not set"))?
.ok_or(ErrorBuilder::field_not_set("id"))?
.try_into()?;

let leader: crate::peer::PeerId = value.leader
.ok_or(ErrorBuilder::new("field 'leader' not set"))?
.ok_or(ErrorBuilder::field_not_set("leader"))?
.try_into()?;

let assignments: Vec<crate::cluster::PeerClusterAssignment> = value.assignments
Expand Down Expand Up @@ -237,15 +237,15 @@ impl TryFrom<PeerClusterAssignment> for crate::cluster::PeerClusterAssignment {
type ErrorBuilder = ConversionErrorBuilder<PeerClusterAssignment, crate::cluster::PeerClusterAssignment>;

let peer_id: crate::peer::PeerId = value.peer_id
.ok_or(ErrorBuilder::new("field 'peer_id' not set"))?
.ok_or(ErrorBuilder::field_not_set("peer_id"))?
.try_into()?;

let vpn_address: std::net::IpAddr = value.vpn_address
.ok_or(ErrorBuilder::new("field 'vpn_address' not set"))?
.ok_or(ErrorBuilder::field_not_set("vpn_address"))?
.try_into()?;

let can_server_port: crate::util::Port = value.can_server_port
.ok_or(ErrorBuilder::new("field 'can_server_port' not set"))?
.ok_or(ErrorBuilder::field_not_set("can_server_port"))?
.try_into()?;

let device_interfaces: Vec<crate::util::net::NetworkInterfaceDescriptor> = value.device_interfaces
Expand Down
6 changes: 5 additions & 1 deletion opendut-types/src/proto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ pub struct ConversionErrorBuilder<From, To> {

#[allow(clippy::new_ret_no_self)]
impl<From, To> ConversionErrorBuilder<From, To> {
pub fn new(details: impl Into<String>) -> ConversionError {
pub fn message(details: impl Into<String>) -> ConversionError {
ConversionError::new::<From, To>(details)
}
pub fn field_not_set(field: impl Into<String>) -> ConversionError {
let details = format!("Field '{}' not set", field.into());
ConversionError::new::<From, To>(details)
}
}
2 changes: 1 addition & 1 deletion opendut-types/src/proto/peer/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl TryFrom<PeerConfiguration> for crate::peer::configuration::PeerConfiguratio
type ErrorBuilder = ConversionErrorBuilder<PeerConfiguration, crate::peer::configuration::PeerConfiguration>;

let executors = value.executors
.ok_or(ErrorBuilder::new("Executor not set"))?
.ok_or(ErrorBuilder::field_not_set("executors"))?
.try_into()?;

let cluster_assignment = value.cluster_assignment
Expand Down
30 changes: 15 additions & 15 deletions opendut-types/src/proto/peer/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl TryFrom<ExecutorDescriptor> for crate::peer::executor::ExecutorDescriptor {
type ErrorBuilder = ConversionErrorBuilder<ExecutorDescriptor, crate::peer::executor::ExecutorDescriptor>;

let descriptor = value.descriptor
.ok_or(ErrorBuilder::new("Executor not set not set"))?;
.ok_or(ErrorBuilder::field_not_set("descriptor"))?;

let result = match descriptor {
executor_descriptor::Descriptor::Executable(_) => {
Expand All @@ -94,13 +94,13 @@ impl TryFrom<ExecutorDescriptor> for crate::peer::executor::ExecutorDescriptor {
args
} = descriptor;
let engine = engine
.ok_or(ErrorBuilder::new("Engine not set"))?
.ok_or(ErrorBuilder::field_not_set("engine"))?
.try_into()?;
let name = name
.ok_or(ErrorBuilder::new("Container Name not set"))?
.ok_or(ErrorBuilder::field_not_set("name"))?
.try_into()?;
let image = image
.ok_or(ErrorBuilder::new("Container Image not set"))?
.ok_or(ErrorBuilder::field_not_set("image"))?
.try_into()?;
let volumes = volumes
.into_iter()
Expand All @@ -119,7 +119,7 @@ impl TryFrom<ExecutorDescriptor> for crate::peer::executor::ExecutorDescriptor {
.map(TryFrom::try_from)
.collect::<Result<_, _>>()?;
let command = command
.ok_or(ErrorBuilder::new("Container Command not set"))?
.ok_or(ErrorBuilder::field_not_set("command"))?
.try_into()?;
let args = args
.into_iter()
Expand Down Expand Up @@ -171,7 +171,7 @@ impl TryFrom<Engine> for crate::peer::executor::Engine {
type ErrorBuilder = ConversionErrorBuilder<Engine, crate::peer::executor::Engine>;

let inner = value.inner
.ok_or(ErrorBuilder::new("Engine not set"))?;
.ok_or(ErrorBuilder::field_not_set("inner"))?;

let result = match inner {
engine::Inner::Docker(_) => {
Expand Down Expand Up @@ -202,7 +202,7 @@ impl TryFrom<ContainerName> for crate::peer::executor::ContainerName {
type ErrorBuilder = ConversionErrorBuilder<ContainerName, crate::peer::executor::ContainerName>;

crate::peer::executor::ContainerName::try_from(value.value)
.map_err(|cause| ErrorBuilder::new(cause.to_string()))
.map_err(|cause| ErrorBuilder::message(cause.to_string()))
}
}

Expand All @@ -221,7 +221,7 @@ impl TryFrom<ContainerImage> for crate::peer::executor::ContainerImage {
type ErrorBuilder = ConversionErrorBuilder<ContainerImage, crate::peer::executor::ContainerImage>;

crate::peer::executor::ContainerImage::try_from(value.value)
.map_err(|cause| ErrorBuilder::new(cause.to_string()))
.map_err(|cause| ErrorBuilder::message(cause.to_string()))
}
}

Expand All @@ -240,7 +240,7 @@ impl TryFrom<ContainerVolume> for crate::peer::executor::ContainerVolume {
type ErrorBuilder = ConversionErrorBuilder<ContainerVolume, crate::peer::executor::ContainerVolume>;

crate::peer::executor::ContainerVolume::try_from(value.value)
.map_err(|cause| ErrorBuilder::new(cause.to_string()))
.map_err(|cause| ErrorBuilder::message(cause.to_string()))
}
}

Expand All @@ -259,7 +259,7 @@ impl TryFrom<ContainerDevice> for crate::peer::executor::ContainerDevice {
type ErrorBuilder = ConversionErrorBuilder<ContainerVolume, crate::peer::executor::ContainerVolume>;

crate::peer::executor::ContainerDevice::try_from(value.value)
.map_err(|cause| ErrorBuilder::new(cause.to_string()))
.map_err(|cause| ErrorBuilder::message(cause.to_string()))
}
}

Expand All @@ -280,7 +280,7 @@ impl TryFrom<ContainerEnvironmentVariable> for crate::peer::executor::ContainerE
type ErrorBuilder = ConversionErrorBuilder<ContainerEnvironmentVariable, crate::peer::executor::ContainerEnvironmentVariable>;

crate::peer::executor::ContainerEnvironmentVariable::new(value.name, value.value)
.map_err(|cause| ErrorBuilder::new(cause.to_string()))
.map_err(|cause| ErrorBuilder::message(cause.to_string()))
}
}

Expand All @@ -299,7 +299,7 @@ impl TryFrom<ContainerPortSpec> for crate::peer::executor::ContainerPortSpec {
type ErrorBuilder = ConversionErrorBuilder<ContainerPortSpec, crate::peer::executor::ContainerPortSpec>;

crate::peer::executor::ContainerPortSpec::try_from(value.value)
.map_err(|cause| ErrorBuilder::new(cause.to_string()))
.map_err(|cause| ErrorBuilder::message(cause.to_string()))
}
}

Expand All @@ -310,7 +310,7 @@ impl TryFrom<ContainerCommand> for crate::peer::executor::ContainerCommand {
type ErrorBuilder = ConversionErrorBuilder<ContainerCommand, crate::peer::executor::ContainerCommand>;

crate::peer::executor::ContainerCommand::try_from(value.value)
.map_err(|cause| ErrorBuilder::new(cause.to_string()))
.map_err(|cause| ErrorBuilder::message(cause.to_string()))
}
}

Expand All @@ -337,6 +337,6 @@ impl TryFrom<ContainerCommandArgument> for crate::peer::executor::ContainerComma
type ErrorBuilder = ConversionErrorBuilder<ContainerCommandArgument, crate::peer::executor::ContainerCommandArgument>;

crate::peer::executor::ContainerCommandArgument::try_from(value.value)
.map_err(|cause| ErrorBuilder::new(cause.to_string()))
.map_err(|cause| ErrorBuilder::message(cause.to_string()))
}
}
}
36 changes: 18 additions & 18 deletions opendut-types/src/proto/peer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl TryFrom<PeerId> for crate::peer::PeerId {
type ErrorBuilder = ConversionErrorBuilder<PeerId, crate::peer::PeerId>;

value.uuid
.ok_or(ErrorBuilder::new("Uuid not set"))
.ok_or(ErrorBuilder::field_not_set("uuid"))
.map(|uuid| Self(uuid.into()))
}
}
Expand Down Expand Up @@ -53,7 +53,7 @@ impl TryFrom<PeerName> for crate::peer::PeerName {
type ErrorBuilder = ConversionErrorBuilder<PeerName, crate::peer::PeerName>;

crate::peer::PeerName::try_from(value.value)
.map_err(|cause| ErrorBuilder::new(cause.to_string()))
.map_err(|cause| ErrorBuilder::message(cause.to_string()))
}
}

Expand All @@ -80,7 +80,7 @@ impl TryFrom<PeerLocation> for crate::peer::PeerLocation {
type ErrorBuilder = ConversionErrorBuilder<PeerLocation, crate::peer::PeerLocation>;

crate::peer::PeerLocation::try_from(value.value)
.map_err(|cause| ErrorBuilder::new(cause.to_string()))
.map_err(|cause| ErrorBuilder::message(cause.to_string()))
}
}

Expand Down Expand Up @@ -129,27 +129,27 @@ impl TryFrom<PeerDescriptor> for crate::peer::PeerDescriptor {
type ErrorBuilder = ConversionErrorBuilder<PeerDescriptor, crate::peer::PeerDescriptor>;

let id = value.id
.ok_or(ErrorBuilder::new("Id not set"))?
.ok_or(ErrorBuilder::field_not_set("id"))?
.try_into()?;

let name = value.name
.ok_or(ErrorBuilder::new("Name not set"))?
.ok_or(ErrorBuilder::field_not_set("name"))?
.try_into()?;

let location = value.location
.map(crate::peer::PeerLocation::try_from)
.transpose()?;

let network_configuration = value.network_configuration
.ok_or(ErrorBuilder::new("Network configuration not set"))?
.ok_or(ErrorBuilder::field_not_set("network_configuration"))?
.try_into()?;

let topology = value.topology
.ok_or(ErrorBuilder::new("Topology not set"))?
.ok_or(ErrorBuilder::field_not_set("topology"))?
.try_into()?;

let executors = value.executors
.ok_or(ErrorBuilder::new("Executor not set"))?
.ok_or(ErrorBuilder::field_not_set("executors"))?
.try_into()?;

Ok(crate::peer::PeerDescriptor {
Expand Down Expand Up @@ -182,24 +182,24 @@ impl TryFrom<PeerSetup> for crate::peer::PeerSetup {
type ErrorBuilder = ConversionErrorBuilder<PeerSetup, crate::peer::PeerSetup>;

let id: crate::peer::PeerId = value.id
.ok_or(ErrorBuilder::new("PeerId not set"))?
.ok_or(ErrorBuilder::field_not_set("id"))?
.try_into()?;

let carl: url::Url = value.carl
.ok_or(ErrorBuilder::new("Carl not set"))
.ok_or(ErrorBuilder::field_not_set("carl"))
.and_then(|url| url::Url::parse(&url.value)
.map_err(|cause| ErrorBuilder::new(format!("Carl URL could not be parsed: {}", cause))))?;
.map_err(|cause| ErrorBuilder::message(format!("Carl URL could not be parsed: {}", cause))))?;

let ca: crate::util::net::Certificate = value.ca
.ok_or(ErrorBuilder::new("No CA Certificate provided."))
.ok_or(ErrorBuilder::field_not_set("ca"))
.and_then(crate::util::net::Certificate::try_from)?;

let vpn: crate::vpn::VpnPeerConfiguration = value.vpn
.ok_or(ErrorBuilder::new("VpnConfig not set"))
.ok_or(ErrorBuilder::field_not_set("vpn"))
.and_then(VpnPeerConfig::try_into)?;

let auth_config = value.auth_config
.ok_or(ErrorBuilder::new("PeerId not set"))?
.ok_or(ErrorBuilder::field_not_set("auth_config"))?
.try_into()?;

Ok(Self {
Expand Down Expand Up @@ -282,7 +282,7 @@ impl TryFrom<PeerState> for crate::peer::state::PeerState {
type ErrorBuilder = ConversionErrorBuilder<PeerState, crate::peer::state::PeerState>;

let inner = state.inner
.ok_or(ErrorBuilder::new("Inner state not set"))?;
.ok_or(ErrorBuilder::field_not_set("inner"))?;

match inner {
peer_state::Inner::Down(_) => {
Expand All @@ -291,12 +291,12 @@ impl TryFrom<PeerState> for crate::peer::state::PeerState {
peer_state::Inner::Up(PeerStateUp { inner, remote_host }) => {

let remote_host: std::net::IpAddr = remote_host
.ok_or(ErrorBuilder::new("field 'remote_host' not set"))?
.ok_or(ErrorBuilder::field_not_set("remote_host"))?
.try_into()?;


let inner = inner
.ok_or(ErrorBuilder::new("Inner 'Up' state not set"))?;
.ok_or(ErrorBuilder::message("Inner 'Up' state not set"))?;

match inner {
peer_state_up::Inner::Available(_) => {
Expand All @@ -308,7 +308,7 @@ impl TryFrom<PeerState> for crate::peer::state::PeerState {
peer_state_up::Inner::Blocked(PeerStateUpBlocked { inner }) => {

let inner = inner
.ok_or(ErrorBuilder::new("Inner 'Blocked' state not set"))?;
.ok_or(ErrorBuilder::message("Inner 'Blocked' state not set"))?;

match inner {
peer_state_up_blocked::Inner::Deploying(_) => {
Expand Down
Loading

0 comments on commit 068a7f1

Please sign in to comment.