use icydb_schema::wire::limits;
use serde::{Serialize, de::DeserializeOwned};
pub use icydb_schema::wire::{
Error, MAX_REPLY_BYTES, MAX_REQUEST_BYTES, decode_one, encode_one, encoded_len,
};
const ID_BASE: u64 = u64::from_le_bytes(*b"ICYDBW00");
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Endpoint {
Query,
Ddl,
Update,
Integrity,
FixturesReset,
FixturesLoad,
Metrics,
MetricsReset,
Snapshot,
Schema,
SchemaMigrate,
SchemaMigration,
}
impl Endpoint {
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Query => "icydb_query",
Self::Ddl => "icydb_ddl",
Self::Update => "icydb_update",
Self::Integrity => "icydb_integrity",
Self::FixturesReset => "icydb_fixtures_reset",
Self::FixturesLoad => "icydb_fixtures_load",
Self::Metrics => "icydb_metrics",
Self::MetricsReset => "icydb_metrics_reset",
Self::Snapshot => "icydb_snapshot",
Self::Schema => "icydb_schema",
Self::SchemaMigrate => "icydb_schema_migrate",
Self::SchemaMigration => "icydb_schema_migration",
}
}
#[must_use]
pub const fn id(self) -> u64 {
ID_BASE
+ match self {
Self::Query => 1,
Self::Ddl => 2,
Self::Update => 3,
Self::Integrity => 4,
Self::FixturesReset => 5,
Self::FixturesLoad => 6,
Self::Metrics => 7,
Self::MetricsReset => 8,
Self::Snapshot => 9,
Self::Schema => 10,
Self::SchemaMigrate => 11,
Self::SchemaMigration => 12,
}
}
#[must_use]
pub fn from_name(name: &str) -> Option<Self> {
match name {
"icydb_query" => Some(Self::Query),
"icydb_ddl" => Some(Self::Ddl),
"icydb_update" => Some(Self::Update),
"icydb_integrity" => Some(Self::Integrity),
"icydb_fixtures_reset" => Some(Self::FixturesReset),
"icydb_fixtures_load" => Some(Self::FixturesLoad),
"icydb_metrics" => Some(Self::Metrics),
"icydb_metrics_reset" => Some(Self::MetricsReset),
"icydb_snapshot" => Some(Self::Snapshot),
"icydb_schema" => Some(Self::Schema),
"icydb_schema_migrate" => Some(Self::SchemaMigrate),
"icydb_schema_migration" => Some(Self::SchemaMigration),
_ => None,
}
}
#[must_use]
pub const fn from_id(id: u64) -> Option<Self> {
match id.wrapping_sub(ID_BASE) {
1 => Some(Self::Query),
2 => Some(Self::Ddl),
3 => Some(Self::Update),
4 => Some(Self::Integrity),
5 => Some(Self::FixturesReset),
6 => Some(Self::FixturesLoad),
7 => Some(Self::Metrics),
8 => Some(Self::MetricsReset),
9 => Some(Self::Snapshot),
10 => Some(Self::Schema),
11 => Some(Self::SchemaMigrate),
12 => Some(Self::SchemaMigration),
_ => None,
}
}
pub fn encode_request<T: Serialize>(self, value: T) -> Result<Vec<u8>, Error> {
ic_wire::native::encode_request(self.id(), &value, limits(MAX_REQUEST_BYTES))
}
pub fn decode_request<T: Serialize + DeserializeOwned>(self, bytes: &[u8]) -> Result<T, Error> {
ic_wire::native::decode_request(self.id(), bytes, limits(MAX_REQUEST_BYTES))
}
pub fn encode_response<T: Serialize>(self, value: T) -> Result<Vec<u8>, Error> {
ic_wire::native::encode_response(self.id(), &value, limits(MAX_REPLY_BYTES))
}
pub fn decode_response<T: Serialize + DeserializeOwned>(
self,
bytes: &[u8],
) -> Result<T, Error> {
ic_wire::native::decode_response(self.id(), bytes, limits(MAX_REPLY_BYTES))
}
}
pub fn encode_init_arguments<T: Serialize>(arguments: T) -> Result<Vec<u8>, Error> {
ic_wire::native::encode_request(ID_BASE + 13, &arguments, limits(MAX_REQUEST_BYTES))
}
pub fn encode_post_upgrade_arguments<T: Serialize>(arguments: T) -> Result<Vec<u8>, Error> {
ic_wire::native::encode_request(ID_BASE + 14, &arguments, limits(MAX_REQUEST_BYTES))
}
#[doc(hidden)]
pub fn decode_init_arguments<T: Serialize + DeserializeOwned>(bytes: &[u8]) -> Result<T, Error> {
ic_wire::native::decode_request(ID_BASE + 13, bytes, limits(MAX_REQUEST_BYTES))
}
#[doc(hidden)]
pub fn decode_post_upgrade_arguments<T: Serialize + DeserializeOwned>(
bytes: &[u8],
) -> Result<T, Error> {
ic_wire::native::decode_request(ID_BASE + 14, bytes, limits(MAX_REQUEST_BYTES))
}
#[cfg(test)]
mod tests;