use bitrpc::bitcode::{Decode, Encode};
use serde::{Deserialize, Serialize};
use thiserror::Error;
pub const MAX_WASM_SIZE: usize = 30 * 1024 * 1024;
#[derive(Debug, Error, Serialize, Deserialize, Clone, Encode, Decode)]
pub enum FunctionError {
#[error("Authentication error: {0}")]
AuthError(String),
#[error("Function not found: {0}")]
NotFound(String),
#[error("Permission denied: {0}")]
PermissionDenied(String),
#[error("Invalid input: {0}")]
InvalidInput(String),
#[error("Internal error: {0}")]
InternalError(String),
}
pub type FunctionResult<T> = std::result::Result<T, FunctionError>;
#[derive(
Clone, Debug, Serialize, Deserialize, Encode, Decode, bincode::Encode, bincode::Decode,
)]
pub struct FunctionInfo {
pub name: String,
pub owner: String,
pub published_at: String,
pub usage: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, Encode, Decode)]
pub struct FunctionMetricsResponse {
pub function_name: String,
pub total_time_millis: u64,
pub call_count: u64,
pub last_called: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, Encode, Decode)]
pub struct Metrics {
pub total_time: u64,
pub total_calls: u64,
pub function_metrics: Vec<FunctionMetricsResponse>,
}
#[bitrpc::service(
request = FunctionServiceRequest,
response = FunctionServiceResponse,
client = FunctionServiceRpcClient
)]
pub trait FunctionService {
async fn publish(
&self,
wasm_file: Vec<u8>,
name: String,
github_auth_token: String,
) -> bitrpc::Result<FunctionResult<String>>;
async fn list_functions(
&self,
github_auth_token: String,
) -> bitrpc::Result<FunctionResult<Vec<FunctionInfo>>>;
async fn unpublish(
&self,
name: String,
github_auth_token: String,
) -> bitrpc::Result<FunctionResult<()>>;
async fn get_metrics(
&self,
github_auth_token: String,
) -> bitrpc::Result<FunctionResult<Metrics>>;
}