tinystore 0.1.1

A small storage runtime for applications on SQLite: SQL, key-value state, durable jobs, files, search, logs and metrics in one directory
Documentation
//! A message is a map from field numbers to values. A request is read whole or
//! refused: a field this server does not know is `unimplemented`, since an
//! answer to a request read in part answers another question. A refusal is a
//! `Failure`, the message a stream that failed ends with.

use super::protocol::Failure;
use crate::{Error, ErrorKind};

impl Failure {
    pub(crate) fn invalid(message: impl Into<String>) -> Failure {
        Failure { code: "invalid".to_owned(), message: message.into(), what: None }
    }

    /// A method or a field this server does not have, naming it and the
    /// server's version, so the client knows which server to upgrade.
    pub(crate) fn unimplemented(missing: impl Into<String>) -> Failure {
        let missing = missing.into();
        let message = format!("{missing}: not in tinystore {}", env!("CARGO_PKG_VERSION"));
        Failure { code: "unimplemented".to_owned(), message, what: None }.naming("field", missing)
    }

    pub(crate) fn internal(message: impl Into<String>) -> Failure {
        Failure { code: "internal".to_owned(), message: message.into(), what: None }
    }

    pub(crate) fn cancelled(message: impl Into<String>) -> Failure {
        Failure { code: "cancelled".to_owned(), message: message.into(), what: None }
    }

    pub(crate) fn permission(message: impl Into<String>) -> Failure {
        Failure { code: "permission".to_owned(), message: message.into(), what: None }
    }

    pub(crate) fn unavailable(message: impl Into<String>) -> Failure {
        Failure { code: "unavailable".to_owned(), message: message.into(), what: None }
    }

    /// The same failure with one more name of the item it is about.
    pub(crate) fn naming(mut self, name: &str, value: impl Into<String>) -> Failure {
        self.what.get_or_insert_default().insert(name.to_owned(), value.into());
        self
    }
}

impl From<Error> for Failure {
    /// The error's code, its text, and its facts by their names in `what`.
    fn from(error: Error) -> Failure {
        let failure = Failure { code: code_of(error.kind()).to_owned(), message: error.to_string(), what: None };
        error.facts().iter().fold(failure, |failure, (name, value)| failure.naming(name, value.clone()))
    }
}

/// An error kind's code, the same in every SDK's error classes.
pub(crate) fn code_of(kind: ErrorKind) -> &'static str {
    match kind {
        ErrorKind::Invalid => "invalid",
        ErrorKind::NotFound => "not_found",
        ErrorKind::Conflict => "conflict",
        ErrorKind::TooOld => "too_old",
        ErrorKind::TooNew => "too_new",
        ErrorKind::Limit => "limit",
        ErrorKind::Closed => "closed",
        ErrorKind::Corrupt => "corrupt",
        ErrorKind::InUse => "in_use",
        ErrorKind::Suspended => "suspended",
        ErrorKind::Unavailable => "unavailable",
        ErrorKind::Cancelled => "cancelled",
        ErrorKind::OutcomeUnknown => "outcome_unknown",
        ErrorKind::CommittedLocally => "committed_locally",
        ErrorKind::Lost => "lost",
        // The wire has no code of its own for a disk's error: the server's fault.
        ErrorKind::Io | ErrorKind::Internal => "internal",
    }
}