use super::zccache_v1;
use super::{
client_wire_selection_from_env, request_from_prost, request_to_prost, response_from_prost,
response_to_prost, ClientWireSelection, WireFormat, WIRE_FORMAT_ENV,
};
pub fn supported_control_request_from_prost(
request: zccache_v1::Request,
) -> Result<crate::protocol::Request, String> {
use zccache_v1::request::Body;
match &request.body {
Some(
Body::Ping(_)
| Body::Status(_)
| Body::Shutdown(_)
| Body::Clear(_)
| Body::ReleaseWorktreeHandles(_),
) => request_from_prost(request),
Some(other) => Err(format!(
"unsupported v16 prost control request body {other:?}; only Ping, Status, Shutdown, \
Clear, and ReleaseWorktreeHandles may use the prost control request path"
)),
None => Err(
"unsupported v16 prost request: missing request body; only Ping, Status, Shutdown, \
Clear, and ReleaseWorktreeHandles may use the prost control request path"
.to_string(),
),
}
}
pub fn supported_control_request_to_prost(
request: &crate::protocol::Request,
) -> Result<zccache_v1::Request, String> {
match request {
crate::protocol::Request::Ping
| crate::protocol::Request::Status
| crate::protocol::Request::Shutdown
| crate::protocol::Request::Clear
| crate::protocol::Request::ReleaseWorktreeHandles { .. } => {
Ok(request_to_prost(request, default_request_id(request)))
}
other => Err(format!(
"unsupported v16 prost control request {other:?}; only Ping, Status, Shutdown, \
Clear, and ReleaseWorktreeHandles may select {WIRE_FORMAT_ENV} through the prost \
control request path"
)),
}
}
pub fn supported_control_response_from_prost(
response: zccache_v1::Response,
) -> Result<crate::protocol::Response, String> {
use zccache_v1::response::Body;
match &response.body {
Some(
Body::Pong(_)
| Body::ShuttingDown(_)
| Body::Status(_)
| Body::Cleared(_)
| Body::Error(_)
| Body::ReleaseWorktreeHandlesResult(_),
) => response_from_prost(response),
Some(other) => Err(format!(
"unsupported v16 prost control response body {other:?}; only Pong, Status, \
ShuttingDown, Cleared, Error, and ReleaseWorktreeHandlesResult may use the prost \
control response path"
)),
None => Err(
"unsupported v16 prost response: missing response body; only Pong, Status, \
ShuttingDown, Cleared, Error, and ReleaseWorktreeHandlesResult may use the prost \
control response path"
.to_string(),
),
}
}
pub fn supported_control_response_to_prost(
response: &crate::protocol::Response,
request_id: &str,
) -> Result<zccache_v1::Response, String> {
match response {
crate::protocol::Response::Pong
| crate::protocol::Response::ShuttingDown
| crate::protocol::Response::Status(_)
| crate::protocol::Response::Cleared { .. }
| crate::protocol::Response::Error { .. }
| crate::protocol::Response::ReleaseWorktreeHandlesResult { .. } => {
Ok(response_to_prost(response, request_id))
}
other => Err(format!(
"unsupported v16 prost control response {other:?}; only Pong, Status, \
ShuttingDown, Cleared, Error, and ReleaseWorktreeHandlesResult may use the prost \
control response path"
)),
}
}
#[must_use]
pub const fn default_request_id(request: &crate::protocol::Request) -> &'static str {
match request {
crate::protocol::Request::Ping => "control-ping",
crate::protocol::Request::Status => "control-status",
crate::protocol::Request::Shutdown => "control-shutdown",
crate::protocol::Request::Clear => "control-clear",
crate::protocol::Request::ReleaseWorktreeHandles { .. } => {
"control-release-worktree-handles"
}
crate::protocol::Request::Lookup { .. } => "lookup",
crate::protocol::Request::Store { .. } => "store",
crate::protocol::Request::SessionStart { .. } => "session-start",
crate::protocol::Request::Compile { .. } => "compile",
crate::protocol::Request::SessionEnd { .. } => "session-end",
crate::protocol::Request::CompileEphemeral { .. } => "compile-ephemeral",
crate::protocol::Request::LinkEphemeral { .. } => "link-ephemeral",
crate::protocol::Request::SessionStats { .. } => "session-stats",
crate::protocol::Request::FingerprintCheck { .. } => "fingerprint-check",
crate::protocol::Request::FingerprintMarkSuccess { .. } => "fingerprint-mark-success",
crate::protocol::Request::FingerprintMarkFailure { .. } => "fingerprint-mark-failure",
crate::protocol::Request::FingerprintInvalidate { .. } => "fingerprint-invalidate",
crate::protocol::Request::ListRustArtifacts => "list-rust-artifacts",
crate::protocol::Request::GenericToolExec { .. } => "generic-tool-exec",
}
}
#[must_use]
pub fn full_family_wire_format_from_env() -> WireFormat {
match client_wire_selection_from_env() {
Ok(ClientWireSelection::ProstV16) => WireFormat::ProstV16,
Ok(ClientWireSelection::FrameV1) => WireFormat::FrameV1,
Ok(ClientWireSelection::Auto | ClientWireSelection::BincodeV15) | Err(_) => {
WireFormat::BincodeV15
}
}
}
pub fn response_from_decoded_wire(
message: crate::protocol::DecodedWireMessage<crate::protocol::Response, zccache_v1::Response>,
) -> Result<crate::protocol::Response, crate::protocol::ProtocolError> {
match message {
crate::protocol::DecodedWireMessage::BincodeV15(response) => Ok(response),
crate::protocol::DecodedWireMessage::ProstV16(response)
| crate::protocol::DecodedWireMessage::FrameV1 {
message: response, ..
} => response_from_prost(response).map_err(crate::protocol::ProtocolError::Deserialization),
}
}