# Request

> The payload is the tag byte 18 followed by the name of the image as a varint length and UTF-8 bytes, in the postcard wire format.

Canonical: https://protocol.diverge.network/2.3.0/endpoints/images-delete/request/
Specification revision: 2.3.0

The payload of the request is the tag byte followed by the request in
the postcard wire format, as the postcard reading of
[Notation](/2.3.0/endpoints/#notation) provides:

```text
[18][name_len: varint][name: utf8 …]
```

- **`name`.** The name of the image, UTF-8, preceded by its byte
  length as a varint. It is the `name` of an image in the client's
  [images::list](/2.3.0/endpoints/images-list/) listing.
- **Bytes after the request.** A server ignores every byte that
  follows the `name` field. A request whose payload extends past the
  request is not malformed.
- **Malformed.** A payload with no byte, a payload whose first byte
  is not `18`, and a payload whose bytes after the tag do not decode
  as the request are malformed. [Endpoints](/2.3.0/endpoints/) states how a
  server answers a request it cannot read.

The request is defined by `diverge-sdk-rs/src/provider/endpoints/images/delete/client/request/frame.rs`:

```rust
//! What a client's request frame carries for an image deletion.

use serde::{Deserialize, Serialize};

use crate::wire::decode::Decode;
use crate::wire::encode::{Encode, Writer};

/// Destroy an image the caller built.
///
/// # A name, and the same access model as everything else
///
/// The one field is an
/// [`Image::name`](crate::provider::endpoints::images::list::server::response::Image::name)
/// from a listing. A caller cannot delete an image it did not build,
/// and cannot probe for what exists by deleting and reading the
/// error — the provider resolves a name against the caller that
/// built it, and nothing else.
///
/// # It is not undoable and there is no confirmation
///
/// One frame destroys the image. Confirmation belongs where a person
/// is, which is above this. A caller that wants the image again
/// builds it again.
///
/// # An image in use is refused
///
/// An image a running container of the caller runs is never deleted:
/// a provider answers
/// [`InUse`](crate::provider::endpoints::images::delete::server::response::Frame::InUse)
/// and changes nothing. A caller that wants it gone stops the
/// container first and asks again.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
pub struct Frame {
    /// Which image, by the name its build gave it.
    ///
    /// Names come from
    /// [`Image::name`](crate::provider::endpoints::images::list::server::response::Image::name)
    /// and mean nothing outside the caller that chose them.
    pub name: String,
}

/// This frame's tag among the scope-opening requests.
///
/// One byte at the front of the payload, which is what tells a reader
/// which request it holds. The frame layer does not discriminate them
/// — [`ClientFrame::Request`](crate::wire::frame::client::ClientFrame::Request)
/// is one type carrying bytes — so the distinction has to be in the
/// bytes, and each request owns the value that names it.
///
/// See the table in [`endpoints`](crate::provider::endpoints) for the whole
/// allocation. The values are chosen across modules that do not know
/// about each other, so the table is the only place they can be seen
/// at once.
const TAG: u8 = 18;

/// Postcard, matching [`volumes`](crate::provider::endpoints::volumes) and the
/// rest of this family's store.
impl Encode for Frame {
    /// Postcard's own failure. The tag cannot fail.
    type Error = postcard::Error;

    fn encode(&self, out: &mut Writer<'_>) -> Result<(), Self::Error> {
        out.extend_from_slice(&[TAG]);
        postcard::to_io(self, &mut *out)?;
        Ok(())
    }
}

impl Decode<'_> for Frame {
    /// Three ways to fail, and only one of them is postcard's.
    type Error = FrameError;

    fn decode(bytes: &[u8]) -> Result<Self, Self::Error> {
        let (tag, rest) = bytes.split_first().ok_or(FrameError::Empty)?;
        if *tag != TAG {
            return Err(FrameError::UnexpectedTag(*tag));
        }
        postcard::from_bytes(rest).map_err(FrameError::Body)
    }
}

/// An image deletion request that could not be read.
#[derive(Debug)]
pub enum FrameError {
    /// No bytes at all, so not even a tag.
    Empty,
    /// A tag naming some other request.
    ///
    /// A reader that dispatched on the tag will not see this. One that
    /// assumed which request it held, and was wrong, will — which is
    /// the point of checking a tag rather than skipping it.
    UnexpectedTag(u8),
    /// The request did not parse.
    Body(postcard::Error),
}

impl std::fmt::Display for FrameError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FrameError::Empty => f.write_str("image deletion request frame is empty"),
            FrameError::UnexpectedTag(tag) => {
                write!(f, "expected image deletion request tag {TAG}, found {tag}")
            }
            FrameError::Body(error) => write!(f, "image deletion request did not parse: {error}"),
        }
    }
}

impl std::error::Error for FrameError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            FrameError::Body(error) => Some(error),
            FrameError::Empty | FrameError::UnexpectedTag(_) => None,
        }
    }
}
```
