AkurAI Build
Menu

Public repository / 0530b09e3daf6df75da3076c9559324ed6fbbc16

AkurAI-PassVault / crates/passvault-core/src/entities/mod.rs

Latest change 653879bcf622c47638f5aab413ebab46937365ab - Initial: AkurAI-PassVault AES-256-GCM encrypted vault with MCP API by Claude Code

use akurai_json::Value;
use crate::error::Error;
use crate::types::Timestamp;

#[derive(Debug, Clone)]
pub struct VaultEntry {
    pub id: i64,
    pub name: String,
    pub username: String,
    pub password: String,
    pub uri: String,
    pub notes: String,
    pub folder_id: Option<i64>,
    pub created_at: Timestamp,
    pub updated_at: Timestamp,
}

impl VaultEntry {
    pub fn from_value(v: &Value) -> Result<Self, Error> {
        Ok(VaultEntry {
            id: v.get("id").and_then(|v| v.as_i64()).ok_or(Error::Validation("missing id".into()))?,
            name: v.get("name").and_then(|v| v.as_str()).unwrap_or("").to_string(),
            username: v.get("username").and_then(|v| v.as_str()).unwrap_or("").to_string(),
            password: v.get("password").and_then(|v| v.as_str()).unwrap_or("").to_string(),
            uri: v.get("uri").and_then(|v| v.as_str()).unwrap_or("").to_string(),
            notes: v.get("notes").and_then(|v| v.as_str()).unwrap_or("").to_string(),
            folder_id: v.get("folderId").and_then(|v| v.as_i64()),
            created_at: v.get("createdAt").and_then(|v| v.as_i64()).unwrap_or(0),
            updated_at: v.get("updatedAt").and_then(|v| v.as_i64()).unwrap_or(0),
        })
    }

    pub fn to_value(&self) -> Value {
        Value::Object(vec![
            ("id".into(), Value::Int(self.id)),
            ("name".into(), Value::Str(self.name.clone())),
            ("username".into(), Value::Str(self.username.clone())),
            ("password".into(), Value::Str(self.password.clone())),
            ("uri".into(), Value::Str(self.uri.clone())),
            ("notes".into(), Value::Str(self.notes.clone())),
            ("folderId".into(), self.folder_id.map_or(Value::Null, Value::Int)),
            ("createdAt".into(), Value::Int(self.created_at)),
            ("updatedAt".into(), Value::Int(self.updated_at)),
        ])
    }

    pub fn summary_value(&self) -> Value {
        Value::Object(vec![
            ("id".into(), Value::Int(self.id)),
            ("name".into(), Value::Str(self.name.clone())),
            ("username".into(), Value::Str(self.username.clone())),
            ("uri".into(), Value::Str(self.uri.clone())),
            ("folderId".into(), self.folder_id.map_or(Value::Null, Value::Int)),
        ])
    }
}

#[derive(Debug, Clone)]
pub struct Folder {
    pub id: i64,
    pub name: String,
    pub created_at: Timestamp,
}

impl Folder {
    pub fn from_value(v: &Value) -> Result<Self, Error> {
        Ok(Folder {
            id: v.get("id").and_then(|v| v.as_i64()).ok_or(Error::Validation("missing id".into()))?,
            name: v.get("name").and_then(|v| v.as_str()).unwrap_or("").to_string(),
            created_at: v.get("createdAt").and_then(|v| v.as_i64()).unwrap_or(0),
        })
    }

    pub fn to_value(&self) -> Value {
        Value::Object(vec![
            ("id".into(), Value::Int(self.id)),
            ("name".into(), Value::Str(self.name.clone())),
            ("createdAt".into(), Value::Int(self.created_at)),
        ])
    }
}