use super::{BINCODE_PROTOCOL_VERSION, PROST_PROTOCOL_VERSION};
pub mod zccache_v1 {
include!(concat!(env!("OUT_DIR"), "/zccache.v1.rs"));
}
mod api;
mod convert;
mod frame;
mod request;
mod response;
pub use super::wire_frame::{
buffer_starts_running_process_frame, decode_frame_v1_message, encode_frame_v1_request,
encode_frame_v1_response, FrameV1Decoded, ZCCACHE_FRAME_PAYLOAD_PROTOCOL,
};
pub use api::{
default_request_id, full_family_wire_format_from_env, response_from_decoded_wire,
supported_control_request_from_prost, supported_control_request_to_prost,
supported_control_response_from_prost, supported_control_response_to_prost,
};
pub use frame::{decode_prost_message, encode_prost_message};
pub use request::{request_from_prost, request_to_prost};
pub use response::{response_from_prost, response_to_prost};
pub const WIRE_FORMAT_ENV: &str = "ZCCACHE_DAEMON_WIRE";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WireFormat {
BincodeV15,
ProstV16,
FrameV1,
}
impl WireFormat {
#[must_use]
pub const fn protocol_version(self) -> Option<u32> {
match self {
Self::BincodeV15 => Some(BINCODE_PROTOCOL_VERSION),
Self::ProstV16 => Some(PROST_PROTOCOL_VERSION),
Self::FrameV1 => None,
}
}
}
pub const DEFAULT_CLIENT_WIRE_FORMAT: WireFormat = WireFormat::ProstV16;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClientWireSelection {
Auto,
BincodeV15,
ProstV16,
FrameV1,
}
impl ClientWireSelection {
#[must_use]
pub const fn preferred_format(self) -> WireFormat {
match self {
Self::Auto | Self::ProstV16 => WireFormat::ProstV16,
Self::BincodeV15 => WireFormat::BincodeV15,
Self::FrameV1 => WireFormat::FrameV1,
}
}
#[must_use]
pub const fn allows_bincode_fallback(self) -> bool {
matches!(self, Self::Auto)
}
}
#[must_use]
pub const fn wire_format_for_protocol_version(version: u32) -> Option<WireFormat> {
match version {
BINCODE_PROTOCOL_VERSION => Some(WireFormat::BincodeV15),
PROST_PROTOCOL_VERSION => Some(WireFormat::ProstV16),
_ => None,
}
}
pub fn wire_format_from_env_value(value: Option<&str>) -> Result<WireFormat, String> {
client_wire_selection_from_env_value(value).map(ClientWireSelection::preferred_format)
}
pub fn wire_format_from_env() -> Result<WireFormat, String> {
wire_format_from_env_value(std::env::var(WIRE_FORMAT_ENV).ok().as_deref())
}
pub fn client_wire_selection_from_env_value(
value: Option<&str>,
) -> Result<ClientWireSelection, String> {
let Some(value) = value else {
return Ok(ClientWireSelection::Auto);
};
match value.trim().to_ascii_lowercase().as_str() {
"" | "auto" => Ok(ClientWireSelection::Auto),
"bincode" | "bincode-v15" | "v15" => Ok(ClientWireSelection::BincodeV15),
"prost" | "prost-v16" | "v16" => Ok(ClientWireSelection::ProstV16),
"frame" | "frame-v1" => Ok(ClientWireSelection::FrameV1),
other => Err(format!(
"invalid {WIRE_FORMAT_ENV}={other:?}; expected auto, bincode, prost, or frame"
)),
}
}
pub fn client_wire_selection_from_env() -> Result<ClientWireSelection, String> {
client_wire_selection_from_env_value(std::env::var(WIRE_FORMAT_ENV).ok().as_deref())
}