1use bitrpc::bitcode::{Decode, Encode};
2use serde::{Deserialize, Serialize};
3use thiserror::Error;
4
5pub const MAX_WASM_SIZE: usize = 30 * 1024 * 1024;
6
7#[derive(Debug, Error, Serialize, Deserialize, Clone, Encode, Decode)]
9pub enum FunctionError {
10 #[error("Authentication error: {0}")]
11 AuthError(String),
12
13 #[error("Function not found: {0}")]
14 NotFound(String),
15
16 #[error("Permission denied: {0}")]
17 PermissionDenied(String),
18
19 #[error("Invalid input: {0}")]
20 InvalidInput(String),
21
22 #[error("Internal error: {0}")]
23 InternalError(String),
24}
25
26pub type FunctionResult<T> = std::result::Result<T, FunctionError>;
28
29#[derive(
33 Clone, Debug, Serialize, Deserialize, Encode, Decode, bincode::Encode, bincode::Decode,
34)]
35pub struct FunctionInfo {
36 pub name: String,
38 pub owner: String,
40 pub published_at: String,
42 pub usage: String,
44}
45
46#[derive(Clone, Debug, Serialize, Deserialize, Encode, Decode)]
48pub struct FunctionMetricsResponse {
49 pub function_name: String,
51 pub total_time_millis: u64,
53 pub call_count: u64,
55 pub last_called: String,
57}
58
59#[derive(Clone, Debug, Serialize, Deserialize, Encode, Decode)]
61pub struct Metrics {
62 pub total_time: u64,
64 pub total_calls: u64,
66 pub function_metrics: Vec<FunctionMetricsResponse>,
68}
69
70#[bitrpc::service(
72 request = FunctionServiceRequest,
73 response = FunctionServiceResponse,
74 client = FunctionServiceRpcClient
75)]
76pub trait FunctionService {
77 async fn publish(
79 &self,
80 wasm_file: Vec<u8>,
81 name: String,
82 github_auth_token: String,
83 ) -> bitrpc::Result<FunctionResult<String>>;
84 async fn list_functions(
86 &self,
87 github_auth_token: String,
88 ) -> bitrpc::Result<FunctionResult<Vec<FunctionInfo>>>;
89 async fn unpublish(
91 &self,
92 name: String,
93 github_auth_token: String,
94 ) -> bitrpc::Result<FunctionResult<()>>;
95 async fn get_metrics(
97 &self,
98 github_auth_token: String,
99 ) -> bitrpc::Result<FunctionResult<Metrics>>;
100}