Skip to content

Commit

Permalink
Remove hard-coded names and boilerplate expect_*
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeeter committed Aug 17, 2023
1 parent 1e180ae commit ee19463
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 444 deletions.
25 changes: 23 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ hex = "0.4.3"
hubpack = "0.1.2"
lru-cache = "0.1.2"
once_cell = "1.15.0"
paste = "1.0.14"
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde-big-array = "0.5.0"
serde_json = "1.0.95"
Expand All @@ -43,6 +44,7 @@ slog-term = "2.9"
smoltcp = { version = "0.9", default-features = false, features = ["proto-ipv6"] }
socket2 = "0.5.3"
static_assertions = "1.1.0"
strum_macros = "0.25"
string_cache = "0.8.4"
termios = "0.3"
thiserror = "1.0.37"
Expand Down
1 change: 1 addition & 0 deletions gateway-messages/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ serde.workspace = true
serde_repr.workspace = true
smoltcp = { workspace = true, optional = true }
static_assertions.workspace = true
strum_macros.workspace = true
uuid.workspace = true
zerocopy.workspace = true

Expand Down
11 changes: 10 additions & 1 deletion gateway-messages/src/sp_to_mgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,17 @@ pub enum SpRequest {
}

#[derive(
Debug, Clone, Copy, SerializedSize, Serialize, Deserialize, PartialEq, Eq,
Debug,
Clone,
Copy,
SerializedSize,
Serialize,
Deserialize,
PartialEq,
Eq,
strum_macros::IntoStaticStr,
)]
#[strum(serialize_all = "snake_case")]
pub enum SpResponse {
Discover(DiscoverResponse),
IgnitionState(IgnitionState),
Expand Down
1 change: 1 addition & 0 deletions gateway-sp-comms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ hubtools.workspace = true
nix.workspace = true
lru-cache.workspace = true
once_cell.workspace = true
paste.workspace = true
serde.workspace = true
serde-big-array.workspace = true
slog.workspace = true
Expand Down
42 changes: 15 additions & 27 deletions gateway-sp-comms/src/single_sp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,7 @@ impl SingleSp {
/// This will fail if this SP is not connected to an ignition controller.
pub async fn ignition_state(&self, target: u8) -> Result<IgnitionState> {
self.rpc(MgsRequest::IgnitionState { target }).await.and_then(
|(_peer, response, _data)| {
response.expect_ignition_state().map_err(Into::into)
},
|(_peer, response, _data)| response.expect_ignition_state(),
)
}

Expand Down Expand Up @@ -370,17 +368,15 @@ impl SingleSp {
self.rpc(MgsRequest::IgnitionCommand { target, command })
.await
.and_then(|(_peer, response, _data)| {
response.expect_ignition_command_ack().map_err(Into::into)
response.expect_ignition_command_ack()
})
}

/// Request the state of the SP.
pub async fn state(&self) -> Result<VersionedSpState> {
self.rpc(MgsRequest::SpState).await.and_then(
|(_peer, response, _data)| {
response.expect_sp_state().map_err(Into::into)
},
)
self.rpc(MgsRequest::SpState)
.await
.and_then(|(_peer, response, _data)| response.expect_sp_state())
}

/// Request the inventory of the SP.
Expand Down Expand Up @@ -542,9 +538,7 @@ impl SingleSp {
/// the next time the sled starts up.
pub async fn get_startup_options(&self) -> Result<StartupOptions> {
self.rpc(MgsRequest::GetStartupOptions).await.and_then(
|(_peer, response, _data)| {
response.expect_startup_options().map_err(Into::into)
},
|(_peer, response, _data)| response.expect_startup_options(),
)
}

Expand All @@ -558,7 +552,7 @@ impl SingleSp {
) -> Result<()> {
self.rpc(MgsRequest::SetStartupOptions(startup_options)).await.and_then(
|(_peer, response, _data)| {
response.expect_set_startup_options_ack().map_err(Into::into)
response.expect_set_startup_options_ack()
},
)
}
Expand Down Expand Up @@ -626,25 +620,21 @@ impl SingleSp {
self.rpc(MgsRequest::UpdateAbort { component, id: update_id.into() })
.await
.and_then(|(_peer, response, _data)| {
response.expect_update_abort_ack().map_err(Into::into)
response.expect_update_abort_ack()
})
}

/// Get the current power state.
pub async fn power_state(&self) -> Result<PowerState> {
self.rpc(MgsRequest::GetPowerState).await.and_then(
|(_peer, response, _data)| {
response.expect_power_state().map_err(Into::into)
},
)
self.rpc(MgsRequest::GetPowerState)
.await
.and_then(|(_peer, response, _data)| response.expect_power_state())
}

/// Set the current power state.
pub async fn set_power_state(&self, power_state: PowerState) -> Result<()> {
self.rpc(MgsRequest::SetPowerState(power_state)).await.and_then(
|(_peer, response, _data)| {
response.expect_set_power_state_ack().map_err(Into::into)
},
|(_peer, response, _data)| response.expect_set_power_state_ack(),
)
}

Expand Down Expand Up @@ -760,9 +750,7 @@ impl SingleSp {
self.rpc(MgsRequest::ResetComponentPrepare { component })
.await
.and_then(|(_peer, response, _data)| {
response
.expect_sys_reset_component_prepare_ack()
.map_err(Into::into)
response.expect_sys_reset_component_prepare_ack()
})
}

Expand Down Expand Up @@ -791,7 +779,7 @@ impl SingleSp {
// (because it has reset!).
Err(CommunicationError::BadResponseType {
expected: "system-reset",
got: response.name(),
got: response.into(),
})
} else {
response.expect_sys_reset_component_trigger_ack()
Expand Down Expand Up @@ -1198,7 +1186,7 @@ impl AttachedSerialConsoleSend {
as u64;

let n = result.and_then(|(_peer, response, _data)| {
response.expect_serial_console_write_ack().map_err(Into::into)
response.expect_serial_console_write_ack()
})?;

// Confirm the ack we got back makes sense; its `n` should be in the
Expand Down
Loading

0 comments on commit ee19463

Please sign in to comment.