Skip to content

Commit

Permalink
collapse nested match blocks (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleades authored Oct 25, 2021
1 parent de3fa56 commit c35bc4a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 69 deletions.
59 changes: 25 additions & 34 deletions src/generated/info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,41 +55,32 @@ pub type GetVersionResult = RequestResult<Version, InfoError>;

impl FromRpcResponse<pb::GetVersionResponse> for GetVersionResult {
fn from_rpc_response(rpc_get_version_response: TonicResult<pb::GetVersionResponse>) -> Self {
match rpc_get_version_response {
Ok(tonic_response) => {
let get_version_response = tonic_response.into_inner();
match &get_version_response.info_result {
Some(ref rpc_info_result) => {
match pb::info_result::Result::from_i32(rpc_info_result.result) {
Some(info_result) => match info_result {
pb::info_result::Result::Success => {
match get_version_response.version {
Some(ref rpc_version) => Ok(Version::from(rpc_version)),
None => Err(MavErr(InfoError::Unknown(
"Version does not received".into(),
))),
}
}
pb::info_result::Result::Unknown => Err(MavErr(
InfoError::Unknown(rpc_info_result.result_str.clone()),
)),
pb::info_result::Result::InformationNotReceivedYet => {
Err(MavErr(InfoError::InformationNotReceivedYet(
rpc_info_result.result_str.clone(),
)))
}
},
None => Err(MavErr(InfoError::Unknown(
"Unsupported InfoResult.result value".into(),
))),
}
}
None => Err(MavErr(InfoError::Unknown(
"InfoResult does not received".into(),
))),
}
let get_version_response = rpc_get_version_response.map_err(RpcErr)?.into_inner();

let rpc_info_result = get_version_response
.info_result
.ok_or_else(|| MavErr(InfoError::Unknown("InfoResult does not received".into())))?;

let info_result =
pb::info_result::Result::from_i32(rpc_info_result.result).ok_or_else(|| {
MavErr(InfoError::Unknown(
"Unsupported InfoResult.result value".into(),
))
})?;

match info_result {
pb::info_result::Result::Success => match get_version_response.version {
Some(ref rpc_version) => Ok(Version::from(rpc_version)),
None => Err(MavErr(InfoError::Unknown(
"Version does not received".into(),
))),
},
pb::info_result::Result::Unknown => {
Err(MavErr(InfoError::Unknown(rpc_info_result.result_str)))
}
Err(err) => Err(RpcErr(err)),
pb::info_result::Result::InformationNotReceivedYet => Err(MavErr(
InfoError::InformationNotReceivedYet(rpc_info_result.result_str),
)),
}
}
}
Expand Down
61 changes: 26 additions & 35 deletions src/generated/mocap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,42 +245,33 @@ impl FromRpcResponse<pb::SetVisionPositionEstimateResponse> for SetVisionPositio
pb::SetVisionPositionEstimateResponse,
>,
) -> Self {
match rpc_set_vision_position_estimate_response {
Ok(tonic_response) => {
let set_vision_position_estimate_response = tonic_response.into_inner();
match &set_vision_position_estimate_response.mocap_result {
Some(ref rpc_mocap_result) => {
match pb::mocap_result::Result::from_i32(rpc_mocap_result.result) {
Some(mocap_result) => match mocap_result {
pb::mocap_result::Result::Success => Ok(()),
pb::mocap_result::Result::Unknown => Err(MavErr(
MocapError::Unknown(rpc_mocap_result.result_str.clone()),
)),
pb::mocap_result::Result::NoSystem => Err(MavErr(
MocapError::NoSystem(rpc_mocap_result.result_str.clone()),
)),
pb::mocap_result::Result::ConnectionError => {
Err(MavErr(MocapError::ConnectionError(
rpc_mocap_result.result_str.clone(),
)))
}
pb::mocap_result::Result::InvalidRequestData => {
Err(MavErr(MocapError::InvalidRequestData(
rpc_mocap_result.result_str.clone(),
)))
}
},
None => Err(MavErr(MocapError::Unknown(
"Unsupported MocapResult.result value".into(),
))),
}
}
None => Err(MavErr(MocapError::Unknown(
"MocapResult does not received".into(),
))),
}
let rpc_mocap_result = rpc_set_vision_position_estimate_response
.map_err(RpcErr)?
.into_inner()
.mocap_result
.ok_or_else(|| MavErr(MocapError::Unknown("MocapResult does not received".into())))?;

let mocap_result =
pb::mocap_result::Result::from_i32(rpc_mocap_result.result).ok_or_else(|| {
MavErr(MocapError::Unknown(
"Unsupported MocapResult.result value".into(),
))
})?;

match mocap_result {
pb::mocap_result::Result::Success => Ok(()),
pb::mocap_result::Result::Unknown => {
Err(MavErr(MocapError::Unknown(rpc_mocap_result.result_str)))
}
pb::mocap_result::Result::NoSystem => {
Err(MavErr(MocapError::NoSystem(rpc_mocap_result.result_str)))
}
Err(err) => Err(RpcErr(err)),
pb::mocap_result::Result::ConnectionError => Err(MavErr(MocapError::ConnectionError(
rpc_mocap_result.result_str,
))),
pb::mocap_result::Result::InvalidRequestData => Err(MavErr(
MocapError::InvalidRequestData(rpc_mocap_result.result_str),
)),
}
}
}
Expand Down

0 comments on commit c35bc4a

Please sign in to comment.