#![deprecated(since = "0.8.6", note = "renamed to `handle::config_store`")]
use crate::abi;
use bytes::BytesMut;
use fastly_shared::FastlyStatus;
#[derive(Debug, Eq, Hash, PartialEq)]
#[repr(transparent)]
#[deprecated(since = "0.8.6", note = "renamed to `ConfigStoreHandle`")]
pub struct DictionaryHandle {
handle: u32,
}
impl DictionaryHandle {
#[deprecated(
since = "0.8.6",
note = "use the constant in `ConfigStoreHandle` instead"
)]
pub const INVALID: Self = DictionaryHandle {
handle: fastly_shared::INVALID_DICTIONARY_HANDLE,
};
#[deprecated(
since = "0.8.6",
note = "use the method on `ConfigStoreHandle` instead"
)]
pub fn open(name: &str) -> Result<Self, OpenError> {
use OpenError::*;
let mut handle = Self::INVALID;
unsafe { abi::fastly_dictionary::open(name.as_ptr(), name.len(), handle.as_u32_mut()) }
.result()
.map(|_| handle)
.map_err(|status| match status {
FastlyStatus::NONE => NameEmpty,
FastlyStatus::UNSUPPORTED => NameTooLong,
FastlyStatus::INVAL => NameInvalid,
FastlyStatus::BADF => DictionaryDoesNotExist,
_ => panic!("fastly_dictionary::open returned an unrecognized result"),
})
}
#[deprecated(
since = "0.8.6",
note = "use the method on `ConfigStoreHandle` instead"
)]
pub fn get(&self, key: &str, max_len: usize) -> Result<Option<String>, LookupError> {
if self.is_invalid() {
panic!("cannot lookup value with invalid dictionary handle");
}
let mut buf = BytesMut::with_capacity(max_len);
let mut nwritten = 0;
let status = unsafe {
abi::fastly_dictionary::get(
self.as_u32(),
key.as_ptr(),
key.len(),
buf.as_mut_ptr(),
buf.capacity(),
&mut nwritten,
)
};
match status.result().map(|_| nwritten) {
Ok(nwritten) => {
assert!(
nwritten <= buf.capacity(),
"fastly_dictionary::get wrote too many bytes"
);
unsafe {
buf.set_len(nwritten);
}
Ok(Some(
String::from_utf8(buf.to_vec()).expect("host returns valid UTF-8"),
))
}
Err(FastlyStatus::NONE) => Ok(None),
Err(FastlyStatus::ERROR) => Err(LookupError::Other),
Err(FastlyStatus::BADF) => Err(LookupError::DictionaryInvalid),
Err(FastlyStatus::INVAL) => Err(LookupError::KeyInvalid),
Err(FastlyStatus::UNSUPPORTED) => Err(LookupError::KeyTooLong),
Err(FastlyStatus::BUFLEN) => Err(LookupError::ValueTooLong),
Err(FastlyStatus::LIMITEXCEEDED) => Err(LookupError::TooManyLookups),
Err(_) => panic!("fastly_dictionary::get returned an unrecognized result"),
}
}
#[deprecated(
since = "0.8.6",
note = "use the method on `ConfigStoreHandle` instead"
)]
pub fn contains(&self, key: &str) -> Result<bool, LookupError> {
use LookupError::*;
match self.get(key, 0) {
Ok(Some(_)) | Err(ValueTooLong) => Ok(true),
Ok(None) => Ok(false),
Err(e) => Err(e),
}
}
#[deprecated(
since = "0.8.6",
note = "use the method on `ConfigStoreHandle` instead"
)]
pub fn is_valid(&self) -> bool {
!self.is_invalid()
}
#[deprecated(
since = "0.8.6",
note = "use the method on `ConfigStoreHandle` instead"
)]
pub fn is_invalid(&self) -> bool {
self.handle == Self::INVALID.handle
}
#[deprecated(
since = "0.8.6",
note = "use the method on `ConfigStoreHandle` instead"
)]
pub(crate) fn as_u32(&self) -> u32 {
self.handle
}
#[deprecated(
since = "0.8.6",
note = "use the method on `ConfigStoreHandle` instead"
)]
pub(crate) fn as_u32_mut(&mut self) -> &mut u32 {
&mut self.handle
}
}
#[derive(Debug, thiserror::Error)]
#[deprecated(since = "0.8.6", note = "use `config_store::OpenError` instead")]
#[non_exhaustive]
pub enum OpenError {
#[error("dictionary could not be found")]
DictionaryDoesNotExist,
#[error("dictionary names cannot be empty")]
NameEmpty,
#[error("dictionary name too long")]
NameTooLong,
#[error("invalid dictionary name")]
NameInvalid,
}
#[derive(Debug, thiserror::Error)]
#[deprecated(since = "0.8.6", note = "use `config_store::LookupError` instead")]
#[non_exhaustive]
pub enum LookupError {
#[error("invalid dictionary")]
DictionaryInvalid,
#[error("invalid dictionary key")]
KeyInvalid,
#[error("dictionary keys must be shorter than 256 characters")]
KeyTooLong,
#[error("dictionary value was longer than the given buffer")]
ValueTooLong,
#[error("Too many lookups have been performed")]
TooManyLookups,
#[error("dictionary lookup failed")]
Other,
}