faasta_interface/
lib.rs

1use bitrpc::bitcode::{Decode, Encode};
2use serde::{Deserialize, Serialize};
3use thiserror::Error;
4
5pub const MAX_WASM_SIZE: usize = 30 * 1024 * 1024;
6
7// Define a custom error type that can be serialized
8#[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
26// Type alias for Result with our custom error
27pub type FunctionResult<T> = std::result::Result<T, FunctionError>;
28
29// Define the data structures for our service
30
31/// Represents a published function
32#[derive(
33    Clone, Debug, Serialize, Deserialize, Encode, Decode, bincode::Encode, bincode::Decode,
34)]
35pub struct FunctionInfo {
36    /// Name of the function
37    pub name: String,
38    /// Owner's GitHub username
39    pub owner: String,
40    /// When the function was published
41    pub published_at: String,
42    /// Usage information
43    pub usage: String,
44}
45
46/// Function metrics information
47#[derive(Clone, Debug, Serialize, Deserialize, Encode, Decode)]
48pub struct FunctionMetricsResponse {
49    /// Name of the function
50    pub function_name: String,
51    /// Total execution time in milliseconds
52    pub total_time_millis: u64,
53    /// Number of times the function was called
54    pub call_count: u64,
55    /// Last time the function was called (ISO 8601 format)
56    pub last_called: String,
57}
58
59/// Overall metrics information
60#[derive(Clone, Debug, Serialize, Deserialize, Encode, Decode)]
61pub struct Metrics {
62    /// Total execution time across all functions in milliseconds
63    pub total_time: u64,
64    /// Total number of function calls
65    pub total_calls: u64,
66    /// Metrics for individual functions
67    pub function_metrics: Vec<FunctionMetricsResponse>,
68}
69
70/// Service interface for managing functions via bitrpc.
71#[bitrpc::service(
72    request = FunctionServiceRequest,
73    response = FunctionServiceResponse,
74    client = FunctionServiceRpcClient
75)]
76pub trait FunctionService {
77    /// Publish a new function
78    async fn publish(
79        &self,
80        wasm_file: Vec<u8>,
81        name: String,
82        github_auth_token: String,
83    ) -> bitrpc::Result<FunctionResult<String>>;
84    /// List all functions for the authenticated user
85    async fn list_functions(
86        &self,
87        github_auth_token: String,
88    ) -> bitrpc::Result<FunctionResult<Vec<FunctionInfo>>>;
89    /// Unpublish a function
90    async fn unpublish(
91        &self,
92        name: String,
93        github_auth_token: String,
94    ) -> bitrpc::Result<FunctionResult<()>>;
95    /// Get metrics for all functions
96    async fn get_metrics(
97        &self,
98        github_auth_token: String,
99    ) -> bitrpc::Result<FunctionResult<Metrics>>;
100}