Response

The sequence of channel responses the client answers with, what ends it, what a finish with nothing before it means, and what the server does with the answer.

  • The sequence. Exactly one channel response, ok or an error, in the form the proxy’s /vault/delete/{channel} page defines, followed by the channel response finish.
  • A finish with nothing. A channel response finish that no channel response precedes states that the client did not serve the ask. The server relays that to the proxy as the ask not served, and does not ask again.

The channel response is defined by diverge-provider-sdk/src/shared/containers/vault/response/frame.rs:

//! The answer shared by the four operations that either happen or do
//! not.

use std::convert::Infallible;

use super::super::ResponseError;
use crate::encode::{Encode, Writer};

/// The one message that answers a set, a delete, a lock or an unlock
/// — each of those operations re-exports it as its own
/// `response::Frame`.
///
/// ```text
/// [kind: u8][message…]
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Frame<'a> {
    /// Kind `0`. It happened: the key is written or removed, the lock
    /// is held or released.
    Ok,
    /// Kind `1`. It did not, and this says why, for a reader rather
    /// than a program: nothing here is enumerated, because what a
    /// vault can refuse is the caller's policy and not this
    /// specification's.
    Error(&'a str),
}

impl Encode for Frame<'_> {
    /// [`Infallible`]: a kind byte and bytes copied.
    type Error = Infallible;

    fn encode(&self, out: &mut Writer<'_>) -> Result<(), Infallible> {
        match self {
            Frame::Ok => out.extend_from_slice(&[0]),
            Frame::Error(message) => {
                out.extend_from_slice(&[1]);
                out.extend_from_slice(message.as_bytes());
            }
        }
        Ok(())
    }
}

impl<'a> Frame<'a> {
    /// Decode the one message. The message borrows from `bytes`.
    pub fn decode(bytes: &'a [u8]) -> Result<Self, ResponseError> {
        let (kind, rest) = bytes.split_first().ok_or(ResponseError::Empty)?;
        match *kind {
            0 => Ok(Frame::Ok),
            1 => std::str::from_utf8(rest)
                .map(Frame::Error)
                .map_err(|_| ResponseError::MessageUtf8),
            other => Err(ResponseError::UnknownKind(other)),
        }
    }
}