Skip to content

Commit 763624e

Browse files
committed
Add Vss KVStore api.
1 parent 62e1322 commit 763624e

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

rust/api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7+
async-trait = "0.1.77"
78
prost = { version = "0.11.6", default-features = false, features = ["std", "prost-derive"] }
89
bytes = "1.4.0"
910

rust/api/src/error.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use std::error::Error;
2+
use std::fmt::{Display, Formatter};
3+
use std::io;
4+
5+
/// When there is an error while writing to VSS storage, the response contains a relevant error code.
6+
/// A mapping from a VSS server error codes. Refer to [`ErrorResponse`] docs for more
7+
/// information regarding each error code and corresponding use-cases.
8+
#[derive(Debug)]
9+
pub enum VssError {
10+
/// Please refer to [`ErrorCode::NoSuchKeyException`].
11+
NoSuchKeyError(String),
12+
13+
/// Please refer to [`ErrorCode::InvalidRequestException`].
14+
InvalidRequestError(String),
15+
16+
/// Please refer to [`ErrorCode::ConflictException`].
17+
ConflictError(String),
18+
19+
/// Please refer to [`ErrorCode::AuthException`].
20+
AuthError(String),
21+
22+
/// Please refer to [`ErrorCode::InternalServerException`].
23+
InternalServerError(String),
24+
}
25+
26+
impl Display for VssError {
27+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
28+
match self {
29+
VssError::NoSuchKeyError(message) => {
30+
write!(f, "Requested key does not exist: {}", message)
31+
},
32+
VssError::InvalidRequestError(message) => {
33+
write!(f, "Request sent to VSS Storage was invalid: {}", message)
34+
},
35+
VssError::ConflictError(message) => {
36+
write!(f, "Potential version conflict in write operation: {}", message)
37+
},
38+
VssError::AuthError(message) => {
39+
write!(f, "Authentication or Authorization failure: {}", message)
40+
},
41+
VssError::InternalServerError(message) => {
42+
write!(f, "InternalServerError: {}", message)
43+
},
44+
}
45+
}
46+
}
47+
48+
impl Error for VssError {}
49+
50+
impl From<io::Error> for VssError {
51+
fn from(err: io::Error) -> Self {
52+
VssError::InternalServerError(err.to_string())
53+
}
54+
}

rust/api/src/kv_store.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use crate::error::VssError;
2+
use crate::types::{
3+
DeleteObjectRequest, DeleteObjectResponse, GetObjectRequest, GetObjectResponse,
4+
ListKeyVersionsRequest, ListKeyVersionsResponse, PutObjectRequest, PutObjectResponse,
5+
};
6+
use async_trait::async_trait;
7+
8+
pub const GLOBAL_VERSION_KEY: &str = "global_version";
9+
pub const INITIAL_RECORD_VERSION: i32 = 1;
10+
#[async_trait]
11+
pub trait KvStore: Send + Sync {
12+
async fn get(
13+
&self, user_token: String, request: GetObjectRequest,
14+
) -> Result<GetObjectResponse, VssError>;
15+
async fn put(
16+
&self, user_token: String, request: PutObjectRequest,
17+
) -> Result<PutObjectResponse, VssError>;
18+
async fn delete(
19+
&self, user_token: String, request: DeleteObjectRequest,
20+
) -> Result<DeleteObjectResponse, VssError>;
21+
async fn list_key_versions(
22+
&self, user_token: String, request: ListKeyVersionsRequest,
23+
) -> Result<ListKeyVersionsResponse, VssError>;
24+
}

rust/api/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
pub mod error;
2+
pub mod kv_store;
13
pub mod types;

0 commit comments

Comments
 (0)