Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

3.7 KB
Raw Blame
//! Reading cells out of a row as the platforms hand them over: booleans as 0/1 numbers, JSON booleans or "1"/"true"
//! strings, bytes as `{ "__b64" }` (inline) or `{ "__blobRef", "__blobKind" }` (an extracted blob) markers.

use std::collections::HashMap;

use serde_json::{json, Value};

use super::manifest::CodecRecord;
use crate::common::encoding::base64_encode;
use crate::vault_model::names::{IS_DELETED_COL, KIND_COL, LOGO_KIND_FAVICON};

/// The key of an inline byte cell: `{ "__b64": <base64> }`.
pub(crate) const INLINE_BYTES_KEY: &str = "__b64";

/// The hash key of an extracted blob cell: `{ "__blobRef": <hash>, "__blobKind": <kind> }`.
pub(crate) const BLOB_REF_KEY: &str = "__blobRef";

/// The kind key of an extracted blob cell.
pub(crate) const BLOB_KIND_KEY: &str = "__blobKind";

/// A cell as a string slice, `None` when absent or not a string.
pub(crate) fn str_col<'a>(row: &'a CodecRecord, column: &str) -> Option<&'a str> {
    row.get(column).and_then(Value::as_str)
}

/// SQLite-tolerant truthiness: boolean true, a non-zero number, or a "1"/"true" string.
pub(crate) fn truthy(value: Option<&Value>) -> bool {
    match value {
        Some(Value::Bool(b)) => *b,
        Some(Value::Number(n)) => n.as_f64().is_some_and(|f| f != 0.0),
        Some(Value::String(s)) => s == "1" || s.eq_ignore_ascii_case("true"),
        _ => false,
    }
}

/// True when the row's `IsDeleted` flag is set.
pub(crate) fn is_deleted(row: &CodecRecord) -> bool {
    truthy(row.get(IS_DELETED_COL))
}

/// The rows of `table`, empty when the table set does not carry it.
pub(crate) fn rows_of<'a>(tables: &'a HashMap<String, Vec<CodecRecord>>, table: &str) -> &'a [CodecRecord] {
    tables.get(table).map(Vec::as_slice).unwrap_or(&[])
}

/// A logo `Kind` in its comparison form: trimmed and lowercased, empty meaning favicon, as every logo was before the
/// column existed.
pub(crate) fn normalize_logo_kind(kind: &str) -> String {
    let trimmed = kind.trim();
    if trimmed.is_empty() { LOGO_KIND_FAVICON.to_string() } else { trimmed.to_lowercase() }
}

/// A logo row's `Kind`, normalized.
pub(crate) fn logo_kind(row: &CodecRecord) -> String {
    normalize_logo_kind(str_col(row, KIND_COL).unwrap_or(""))
}

/// Bytes as the inline `{ "__b64" }` cell every platform binds and reads.
pub(crate) fn inline_bytes(bytes: &[u8]) -> Value {
    json!({ INLINE_BYTES_KEY: base64_encode(bytes) })
}

/// The base64 text of an inline `{ "__b64" }` cell, `None` for any other value.
pub(crate) fn inline_b64(value: &Value) -> Option<&str> {
    value.get(INLINE_BYTES_KEY).and_then(Value::as_str)
}

/// An extracted blob cell: the content hash and kind that stand in for the bytes inside a manifest.
pub(crate) fn blob_ref(hash: &str, kind: &str) -> Value {
    json!({ BLOB_REF_KEY: hash, BLOB_KIND_KEY: kind })
}

/// The `(hash, kind)` of an extracted blob cell, `None` for any other value.
pub(crate) fn blob_ref_of(value: &Value) -> Option<(&str, Option<&str>)> {
    let hash = value.get(BLOB_REF_KEY).and_then(Value::as_str)?;
    Some((hash, value.get(BLOB_KIND_KEY).and_then(Value::as_str)))
}

/// True when a cell holds bytes: a non-empty inline `{ __b64 }`, an extracted `{ __blobRef }`, or any other non-empty
/// string, array or object (the presence marker the prune queries select).
pub(crate) fn has_bytes(value: Option<&Value>) -> bool {
    match value {
        None | Some(Value::Null) => false,
        Some(Value::String(s)) => !s.is_empty(),
        Some(Value::Array(a)) => !a.is_empty(),
        Some(Value::Object(o)) => match o.get(INLINE_BYTES_KEY).and_then(Value::as_str) {
            Some(b64) => !b64.is_empty(),
            None => !o.is_empty(),
        },
        Some(_) => true,
    }
}