AkurAI Build
Menu

Public repository / e19d3bd001a2452c1602304c070ad0d24c7d5860

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

Latest change 705fd90a65de57e8fe9074f8c225719cf6556674 - Checkpoint WIP + rustfmt + AkurAI Build CI/CD pipeline by Ólafur Búi Ólafsson

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

#[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)),
        ])
    }
}