package wasi:blobstore;

// Types used by blobstore
interface types {
  use wasi:io/streams@0.2.3.{input-stream, output-stream};

  // name of a container, a collection of objects.
  // The container name may be any valid UTF-8 string.
  type container-name = string;

  // name of an object within a container
  // The object name may be any valid UTF-8 string.
  type object-name = string;

  // TODO: define timestamp to include seconds since
  // Unix epoch and nanoseconds
  // https://github.com/WebAssembly/wasi-blob-store/issues/7
  type timestamp = u64;

  // size of an object, in bytes
  type object-size = u64;

  type error = string;

  // information about a container
  record container-metadata {
    // the container's name
    name: container-name,
    // date and time container was created
    created-at: timestamp,
  }

  // information about an object
  record object-metadata {
    // the object's name
    name: object-name,
    // the object's parent container
    container: container-name,
    // date and time the object was created
    created-at: timestamp,
    // size of the object, in bytes
    size: object-size,
  }

  // identifier for an object that includes its container name
  record object-id {
    container: container-name,
    object: object-name
  }

  /// A data is the data stored in a data blob. The value can be of any type
  /// that can be represented in a byte array. It provides a way to write the value
  /// to the output-stream defined in the `wasi-io` interface.
  // Soon: switch to `resource value { ... }`
  resource outgoing-value {
    new-outgoing-value: static func() -> outgoing-value;
    outgoing-value-write-body: func() -> result<output-stream>;
  }

  /// A incoming-value is a wrapper around a value. It provides a way to read the value
  /// from the input-stream defined in the `wasi-io` interface.
  ///
  /// The incoming-value provides two ways to consume the value:
  /// 1. `incoming-value-consume-sync` consumes the value synchronously and returns the
  ///    value as a list of bytes.
  /// 2. `incoming-value-consume-async` consumes the value asynchronously and returns the
  ///    value as an input-stream.
  // Soon: switch to `resource incoming-value { ... }`
  resource incoming-value {
      incoming-value-consume-sync: func() -> result<incoming-value-sync-body, error>;
      incoming-value-consume-async: func() -> result<incoming-value-async-body, error>;
      size: func() -> u64;
  }

  type incoming-value-async-body = input-stream;
  type incoming-value-sync-body = list<u8>;
}
