rustolio-db 0.1.0

An DB extention for the rustolio HTTP-Server
Documentation
//
// SPDX-License-Identifier: MPL-2.0
//
// Copyright (c) 2026 Tobias Binnewies. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//

use std::hash::Hash;

use rustolio_utils::bytes::encoding::{decode_from_bytes, encode_to_bytes};
use rustolio_utils::bytes::Bytes;
use rustolio_utils::crypto::signature::PublicKey;
use rustolio_utils::prelude::*;

#[derive(Debug, Clone, PartialEq, Eq, Hash, Decode, Encode)]
pub struct Value {
    // Signer must be first to enable partial decoding of a file to check if the signer match
    signer: Option<PublicKey>,
    encoded: Bytes,
}

impl Value {
    pub fn from_value(t: &impl Encode) -> rustolio_utils::Result<Self> {
        Ok(Value {
            signer: None,
            encoded: encode_to_bytes(t).context("Failed to encode value")?,
        })
    }

    pub fn signed_from_value(t: &impl Encode, signer: PublicKey) -> rustolio_utils::Result<Self> {
        Ok(Value {
            signer: Some(signer),
            encoded: encode_to_bytes(t).context("Failed to encode value")?,
        })
    }

    pub fn into_value<T: Decode>(self) -> rustolio_utils::Result<T> {
        decode_from_bytes(self.encoded).context("Failed to decode value")
    }

    pub fn signer(&self) -> Option<PublicKey> {
        self.signer
    }
}