121 lines
2.8 KiB
Rust
121 lines
2.8 KiB
Rust
use heromodels_core::{Model, BaseModelData, IndexKey};
|
|
use heromodels_derive::model;
|
|
use serde::{Deserialize, Serialize};
|
|
use super::secretbox::SecretBox;
|
|
|
|
/// Represents a per-user key-value store
|
|
#[model]
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
|
|
pub struct UserKVS {
|
|
/// Base model data
|
|
pub base_data: BaseModelData,
|
|
#[index]
|
|
pub userid: u32,
|
|
pub name: String,
|
|
}
|
|
|
|
impl UserKVS {
|
|
/// Create a new user KVS instance
|
|
pub fn new(id: u32) -> Self {
|
|
let mut base_data = BaseModelData::new();
|
|
base_data.update_id(id);
|
|
Self {
|
|
base_data,
|
|
userid: 0,
|
|
name: String::new(),
|
|
}
|
|
}
|
|
|
|
/// Set the user ID (fluent)
|
|
pub fn userid(mut self, userid: u32) -> Self {
|
|
self.userid = userid;
|
|
self
|
|
}
|
|
|
|
/// Set the KVS name (fluent)
|
|
pub fn name(mut self, name: impl ToString) -> Self {
|
|
self.name = name.to_string();
|
|
self
|
|
}
|
|
|
|
/// Build the final user KVS instance
|
|
pub fn build(self) -> Self {
|
|
self
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Represents an item in a user's key-value store
|
|
#[model]
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
|
|
pub struct UserKVSItem {
|
|
/// Base model data
|
|
pub base_data: BaseModelData,
|
|
#[index]
|
|
pub userkvs_id: u32,
|
|
pub key: String,
|
|
pub value: String,
|
|
pub secretbox: Vec<SecretBox>,
|
|
pub timestamp: u64,
|
|
}
|
|
|
|
impl UserKVSItem {
|
|
/// Create a new user KVS item instance
|
|
pub fn new(id: u32) -> Self {
|
|
let mut base_data = BaseModelData::new();
|
|
base_data.update_id(id);
|
|
Self {
|
|
base_data,
|
|
userkvs_id: 0,
|
|
key: String::new(),
|
|
value: String::new(),
|
|
secretbox: Vec::new(),
|
|
timestamp: 0,
|
|
}
|
|
}
|
|
|
|
/// Set the user KVS ID (fluent)
|
|
pub fn userkvs_id(mut self, userkvs_id: u32) -> Self {
|
|
self.userkvs_id = userkvs_id;
|
|
self
|
|
}
|
|
|
|
/// Set the key (fluent)
|
|
pub fn key(mut self, key: impl ToString) -> Self {
|
|
self.key = key.to_string();
|
|
self
|
|
}
|
|
|
|
/// Set the value (fluent)
|
|
pub fn value(mut self, value: impl ToString) -> Self {
|
|
self.value = value.to_string();
|
|
self
|
|
}
|
|
|
|
/// Add a secret box (fluent)
|
|
pub fn add_secretbox(mut self, secretbox: SecretBox) -> Self {
|
|
self.secretbox.push(secretbox);
|
|
self
|
|
}
|
|
|
|
/// Set all secret boxes (fluent)
|
|
pub fn secretbox(mut self, secretbox: Vec<SecretBox>) -> Self {
|
|
self.secretbox = secretbox;
|
|
self
|
|
}
|
|
|
|
/// Set the timestamp (fluent)
|
|
pub fn timestamp(mut self, timestamp: u64) -> Self {
|
|
self.timestamp = timestamp;
|
|
self
|
|
}
|
|
|
|
/// Build the final user KVS item instance
|
|
pub fn build(self) -> Self {
|
|
self
|
|
}
|
|
}
|
|
|
|
|