use core::{
pin::pin,
task::{Context, Poll, Waker},
};
use super::{AsyncBlobEngine, AsyncBlobStore, AsyncLocate, Connected};
use crate::{BlobRecord, Identifiable, Transformer};
fn block_on<F: Future>(future: F) -> F::Output {
let mut future = pin!(future);
let mut cx = Context::from_waker(Waker::noop());
loop {
if let Poll::Ready(value) = future.as_mut().poll(&mut cx) {
return value;
}
}
}
struct Thumbnail {
media: u64,
frame: u32,
bytes: Vec<u8>,
}
#[derive(Debug, PartialEq)]
struct ThumbnailKey {
media: u64,
frame: u32,
}
#[derive(Debug, PartialEq)]
struct ThumbnailLineage {
media: u64,
}
impl Identifiable for Thumbnail {
type Id = u32;
fn id_ref(&self) -> &u32 {
&self.frame
}
}
impl Transformer for Thumbnail {
type Key = ThumbnailKey;
type Lineage = ThumbnailLineage;
type Record = Self;
type Graphql = ();
fn key(&self) -> ThumbnailKey {
ThumbnailKey {
media: self.media,
frame: self.frame,
}
}
}
impl BlobRecord for Thumbnail {
type Bytes = Vec<u8>;
fn bytes(&self) -> &Vec<u8> {
&self.bytes
}
fn extension() -> Option<&'static str> {
Some("png")
}
}
struct Waveform(Vec<u8>);
impl BlobRecord for Waveform {
type Bytes = Vec<u8>;
fn bytes(&self) -> &Vec<u8> {
&self.0
}
}
#[test]
fn a_blob_record_states_its_bytes_and_its_format() {
let thumb = Thumbnail {
media: 7,
frame: 12,
bytes: vec![0x89, 0x50],
};
assert_eq!(thumb.bytes(), &[0x89, 0x50]);
assert_eq!(<Thumbnail as BlobRecord>::extension(), Some("png"));
assert_eq!(<Waveform as BlobRecord>::extension(), None);
fn format_of<T: BlobRecord>() -> Option<&'static str> {
T::extension()
}
assert_eq!(format_of::<Thumbnail>(), Some("png"));
}
#[test]
fn the_payload_face_carries_no_identity() {
fn bytes_of<T: BlobRecord>(record: &T) -> &T::Bytes {
record.bytes()
}
assert_eq!(bytes_of(&Waveform(vec![1, 2, 3])), &[1, 2, 3]);
}
#[test]
fn the_unused_face_answers_the_whole_seam() {
let thumb = Thumbnail {
media: 7,
frame: 12,
bytes: vec![0x89],
};
assert_eq!(
block_on(AsyncBlobEngine::<Thumbnail>::put(
&(),
Connected::new(&(), ()),
&thumb
)),
Ok(())
);
assert_eq!(
block_on(AsyncBlobEngine::<Thumbnail>::get(
&(),
Connected::new(&(), ()),
&thumb.key()
)),
Ok(Vec::new())
);
}
#[test]
fn only_the_read_side_projects_the_key() {
fn store_anything<R, D>(db: &D, record: &R) -> impl Future<Output = Result<(), D::Error>>
where
D: AsyncBlobEngine<R, Connection = (), WriteContext<'static> = ()>,
{
db.put(Connected::new(&(), ()), record)
}
fn read_by_key<R, D>(db: &D, key: &R::Key) -> impl Future<Output = Result<D::Payload, D::Error>>
where
R: Transformer,
D: AsyncBlobEngine<R, Connection = (), ReadContext<'static> = ()>,
{
db.get(Connected::new(&(), ()), key)
}
let thumb = Thumbnail {
media: 1,
frame: 2,
bytes: vec![9],
};
assert_eq!(
block_on(store_anything::<Thumbnail, ()>(&(), &thumb)),
Ok(())
);
assert_eq!(
block_on(read_by_key::<Thumbnail, ()>(&(), &thumb.key())),
Ok(Vec::new())
);
}
struct NullStore {
root: &'static str,
}
impl AsyncBlobStore for NullStore {
type Options = &'static str;
type Error = core::convert::Infallible;
type Handle = ();
async fn open(options: Self::Options) -> Result<Self, Self::Error> {
Ok(Self { root: options })
}
fn blob(&self, _slot: impl Into<smol_bytes::Utf8Bytes>) -> Self::Handle {}
}
#[test]
fn a_store_opens_once_and_mints_per_slot() {
let store = block_on(NullStore::open("/var/blobs")).expect("the null store opens");
assert_eq!(store.root, "/var/blobs");
store.blob("thumbnail");
store.blob(String::from("waveform"));
}
#[test]
fn the_address_capability_is_a_separate_opt_in() {
struct Addressed;
#[derive(Debug, PartialEq)]
struct Unaddressable;
impl core::fmt::Display for Unaddressable {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("the key renders no address")
}
}
impl core::error::Error for Unaddressable {}
impl<R> AsyncBlobEngine<R> for Addressed {
type Options = ();
type Error = core::convert::Infallible;
type Payload = Vec<u8>;
type Connection = ();
type ReadContext<'a> = ();
type WriteContext<'a> = ();
fn put(
&self,
_ctx: Connected<'_, Self::Connection, Self::WriteContext<'_>>,
_record: &R,
) -> impl Future<Output = Result<(), Self::Error>> + Send {
core::future::ready(Ok(()))
}
fn get(
&self,
_ctx: Connected<'_, Self::Connection, Self::ReadContext<'_>>,
_key: &R::Key,
) -> impl Future<Output = Result<Self::Payload, Self::Error>> + Send
where
R: Transformer,
{
core::future::ready(Ok(Vec::new()))
}
fn read(
&self,
_ctx: Connected<'_, Self::Connection, Self::ReadContext<'_>>,
_key: &R::Key,
_suffix: Option<&str>,
) -> impl Future<Output = Result<Option<Self::Payload>, Self::Error>> + Send
where
R: Transformer,
{
core::future::ready(Ok(None))
}
}
#[derive(Debug, PartialEq)]
struct Placed {
url: String,
bucket: &'static str,
}
impl<R> AsyncLocate<R> for Addressed {
type Ref<'a> = String;
type Location = Placed;
type LocateError = Unaddressable;
fn locate<'a>(
&'a self,
_ctx: Connected<'a, Self::Connection, Self::ReadContext<'a>>,
_key: &'a R::Key,
suffix: Option<&str>,
) -> Result<Self::Ref<'a>, Self::LocateError>
where
R: Transformer,
{
Ok(match suffix {
Some(suffix) => format!("thumbnail/7/12.{suffix}"),
None => "thumbnail/7/12".to_owned(),
})
}
fn location<'a>(&'a self, reference: Self::Ref<'a>) -> Result<Placed, Self::LocateError> {
Ok(Placed {
url: format!("s3://cards/{reference}"),
bucket: "cards",
})
}
}
let key = ThumbnailKey {
media: 7,
frame: 12,
};
assert_eq!(
AsyncLocate::<Thumbnail>::locate(&Addressed, Connected::new(&(), ()), &key, Some("png")),
Ok("thumbnail/7/12.png".to_owned())
);
assert_eq!(
AsyncLocate::<Thumbnail>::locate(&Addressed, Connected::new(&(), ()), &key, None),
Ok("thumbnail/7/12".to_owned())
);
assert_eq!(
AsyncLocate::<Thumbnail>::location(&Addressed, "thumbnail/7/12.png".to_owned()),
Ok(Placed {
url: "s3://cards/thumbnail/7/12.png".to_owned(),
bucket: "cards",
})
);
}