fastly-sys 0.12.0

Fastly Compute ABI Bindings
Documentation
use fastly_shared::FastlyStatus;

use crate::{BodyHandle, RequestHandle};

pub type CacheHandle = u32;
pub type CacheBusyHandle = u32;
pub type CacheReplaceHandle = u32;

pub type CacheObjectLength = u64;
pub type CacheDurationNs = u64;
pub type CacheHitCount = u64;
pub type Boolean = u8;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(C)]
pub struct CacheLookupOptions {
    pub request_headers: RequestHandle,
    pub service: *const u8,
    pub service_len: u32,
}

bitflags::bitflags! {
    #[repr(transparent)]
    pub struct CacheLookupOptionsMask: u32 {
        const _RESERVED = 1 << 0;
        const REQUEST_HEADERS = 1 << 1;
        const SERVICE = 1 << 2;
        const ALWAYS_USE_REQUESTED_RANGE = 1 << 3;
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(C)]
pub struct CacheReplaceOptions {
    pub request_headers: RequestHandle,
    pub replace_strategy: u32,
    pub service: *const u8,
    pub service_len: u32,
}

bitflags::bitflags! {
    #[repr(transparent)]
    pub struct CacheReplaceOptionsMask: u32 {
        const _RESERVED = 1 << 0;
        const REQUEST_HEADERS = 1 << 1;
        const REPLACE_STRATEGY = 1 << 2;
        const SERVICE = 1 << 3;
        const ALWAYS_USE_REQUESTED_RANGE= 1 << 4;
    }
}

#[repr(u32)]
pub enum CacheReplaceStrategy {
    Immediate = 1,
    ImmediateForceMiss = 2,
    Wait = 3,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(C)]
pub struct CacheWriteOptions {
    pub max_age_ns: u64,
    pub request_headers: RequestHandle,
    pub vary_rule_ptr: *const u8,
    pub vary_rule_len: usize,
    pub initial_age_ns: u64,
    pub stale_while_revalidate_ns: u64,
    pub surrogate_keys_ptr: *const u8,
    pub surrogate_keys_len: usize,
    pub length: CacheObjectLength,
    pub user_metadata_ptr: *const u8,
    pub user_metadata_len: usize,
    pub edge_max_age_ns: u64,
    pub service: *const u8,
    pub service_len: u32,
}

bitflags::bitflags! {
    #[repr(transparent)]
    pub struct CacheWriteOptionsMask: u32 {
        const _RESERVED = 1 << 0;
        const REQUEST_HEADERS = 1 << 1;
        const VARY_RULE = 1 << 2;
        const INITIAL_AGE_NS = 1 << 3;
        const STALE_WHILE_REVALIDATE_NS = 1 << 4;
        const SURROGATE_KEYS = 1 << 5;
        const LENGTH = 1 << 6;
        const USER_METADATA = 1 << 7;
        const SENSITIVE_DATA = 1 << 8;
        const EDGE_MAX_AGE_NS = 1 << 9;
        const SERVICE = 1 << 10;
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(C)]
pub struct CacheGetBodyOptions {
    pub from: u64,
    pub to: u64,
}

bitflags::bitflags! {
    #[repr(transparent)]
    pub struct CacheGetBodyOptionsMask: u32 {
        const _RESERVED = 1 << 0;
        const FROM = 1 << 1;
        const TO = 1 << 2;
    }
}

bitflags::bitflags! {
    #[repr(transparent)]
    pub struct CacheLookupState: u32 {
        const FOUND = 1 << 0;
        const USABLE = 1 << 1;
        const STALE = 1 << 2;
        const MUST_INSERT_OR_UPDATE = 1 << 3;
    }
}

#[link(wasm_import_module = "fastly_cache")]
extern "C" {
    #[link_name = "lookup"]
    pub fn lookup(
        cache_key_ptr: *const u8,
        cache_key_len: usize,
        options_mask: CacheLookupOptionsMask,
        options: *const CacheLookupOptions,
        cache_handle_out: *mut CacheHandle,
    ) -> FastlyStatus;

    #[link_name = "insert"]
    pub fn insert(
        cache_key_ptr: *const u8,
        cache_key_len: usize,
        options_mask: CacheWriteOptionsMask,
        options: *const CacheWriteOptions,
        body_handle_out: *mut BodyHandle,
    ) -> FastlyStatus;

    #[link_name = "replace"]
    pub fn replace(
        cache_key_ptr: *const u8,
        cache_key_len: usize,
        options_mask: CacheReplaceOptionsMask,
        options: *const CacheReplaceOptions,
        cache_handle_out: *mut CacheHandle,
    ) -> FastlyStatus;

    #[link_name = "replace_insert"]
    pub fn replace_insert(
        handle: CacheReplaceHandle,
        options_mask: CacheWriteOptionsMask,
        options: *const CacheWriteOptions,
        body_handle_out: *mut BodyHandle,
    ) -> FastlyStatus;

    #[link_name = "replace_get_age_ns"]
    pub fn replace_get_age_ns(
        handle: CacheReplaceHandle,
        duration_out: *mut CacheDurationNs,
    ) -> FastlyStatus;

    #[link_name = "replace_get_body"]
    pub fn replace_get_body(
        handle: CacheReplaceHandle,
        options_mask: CacheGetBodyOptionsMask,
        options: *const CacheGetBodyOptions,
        body_handle_out: *mut BodyHandle,
    ) -> FastlyStatus;

    #[link_name = "replace_get_hits"]
    pub fn replace_get_hits(
        handle: CacheReplaceHandle,
        hits_out: *mut CacheHitCount,
    ) -> FastlyStatus;

    #[link_name = "replace_get_length"]
    pub fn replace_get_length(
        handle: CacheReplaceHandle,
        length_out: *mut CacheObjectLength,
    ) -> FastlyStatus;

    #[link_name = "replace_get_max_age_ns"]
    pub fn replace_get_max_age_ns(
        handle: CacheReplaceHandle,
        duration_out: *mut CacheDurationNs,
    ) -> FastlyStatus;

    #[link_name = "replace_get_stale_while_revalidate_ns"]
    pub fn replace_get_stale_while_revalidate_ns(
        handle: CacheReplaceHandle,
        duration_out: *mut CacheDurationNs,
    ) -> FastlyStatus;

    #[link_name = "replace_get_state"]
    pub fn replace_get_state(
        handle: CacheReplaceHandle,
        cache_lookup_state_out: *mut CacheLookupState,
    ) -> FastlyStatus;

    #[link_name = "replace_get_user_metadata"]
    pub fn replace_get_user_metadata(
        handle: CacheReplaceHandle,
        user_metadata_out_ptr: *mut u8,
        user_metadata_out_len: usize,
        nwritten_out: *mut usize,
    ) -> FastlyStatus;

    #[link_name = "transaction_lookup"]
    pub fn transaction_lookup(
        cache_key_ptr: *const u8,
        cache_key_len: usize,
        options_mask: CacheLookupOptionsMask,
        options: *const CacheLookupOptions,
        cache_handle_out: *mut CacheHandle,
    ) -> FastlyStatus;

    #[link_name = "transaction_lookup_async"]
    pub fn transaction_lookup_async(
        cache_key_ptr: *const u8,
        cache_key_len: usize,
        options_mask: CacheLookupOptionsMask,
        options: *const CacheLookupOptions,
        busy_handle_out: *mut CacheBusyHandle,
    ) -> FastlyStatus;

    #[link_name = "cache_busy_handle_wait"]
    pub fn cache_busy_handle_wait(
        busy_handle: CacheBusyHandle,
        cache_handle_out: *mut CacheHandle,
    ) -> FastlyStatus;

    #[link_name = "transaction_insert"]
    pub fn transaction_insert(
        handle: CacheHandle,
        options_mask: CacheWriteOptionsMask,
        options: *const CacheWriteOptions,
        body_handle_out: *mut BodyHandle,
    ) -> FastlyStatus;

    #[link_name = "transaction_insert_and_stream_back"]
    pub fn transaction_insert_and_stream_back(
        handle: CacheHandle,
        options_mask: CacheWriteOptionsMask,
        options: *const CacheWriteOptions,
        body_handle_out: *mut BodyHandle,
        cache_handle_out: *mut CacheHandle,
    ) -> FastlyStatus;

    #[link_name = "transaction_update"]
    pub fn transaction_update(
        handle: CacheHandle,
        options_mask: CacheWriteOptionsMask,
        options: *const CacheWriteOptions,
    ) -> FastlyStatus;

    #[link_name = "transaction_cancel"]
    pub fn transaction_cancel(handle: CacheHandle) -> FastlyStatus;

    #[link_name = "close"]
    pub fn close(handle: CacheHandle) -> FastlyStatus;

    #[link_name = "close_busy"]
    pub fn close_busy(handle: CacheBusyHandle) -> FastlyStatus;

    #[link_name = "get_state"]
    pub fn get_state(
        handle: CacheHandle,
        cache_lookup_state_out: *mut CacheLookupState,
    ) -> FastlyStatus;

    #[link_name = "get_user_metadata"]
    pub fn get_user_metadata(
        handle: CacheHandle,
        user_metadata_out_ptr: *mut u8,
        user_metadata_out_len: usize,
        nwritten_out: *mut usize,
    ) -> FastlyStatus;

    #[link_name = "get_body"]
    pub fn get_body(
        handle: CacheHandle,
        options_mask: CacheGetBodyOptionsMask,
        options: *const CacheGetBodyOptions,
        body_handle_out: *mut BodyHandle,
    ) -> FastlyStatus;

    #[link_name = "get_length"]
    pub fn get_length(handle: CacheHandle, length_out: *mut CacheObjectLength) -> FastlyStatus;

    #[link_name = "get_max_age_ns"]
    pub fn get_max_age_ns(handle: CacheHandle, duration_out: *mut CacheDurationNs) -> FastlyStatus;

    #[link_name = "get_stale_while_revalidate_ns"]
    pub fn get_stale_while_revalidate_ns(
        handle: CacheHandle,
        duration_out: *mut CacheDurationNs,
    ) -> FastlyStatus;

    #[link_name = "get_age_ns"]
    pub fn get_age_ns(handle: CacheHandle, duration_out: *mut CacheDurationNs) -> FastlyStatus;

    #[link_name = "get_hits"]
    pub fn get_hits(handle: CacheHandle, hits_out: *mut CacheHitCount) -> FastlyStatus;
}