use std::sync::Arc;
use std::time::Duration;
use crate::internal_core::ValueKeyring;
use crate::internal_core::value::{Compression, Encryption, JsonValue, Value};
use crate::internal_core::{
AlpnPolicy, Certificate, ClientIdentity, ClientRootKey, ClientTimeouts, ConnectionState,
DataProtection, DataProtectionKey, DeleteOutcome, Endpoint, GetOutcome, KeyFormat, KeyType,
NamespaceDescriptor, NamespacePolicy, Result, RetryPolicy, ServerTrust, SetOptions, SetOutcome,
TypedKey,
};
#[cfg(feature = "quic-compio")]
use crate::internal_core::{LocalRawClient, LocalRawClientBuilder};
#[cfg(feature = "quic-quinn")]
use crate::internal_core::{RawClient, RawClientBuilder};
#[cfg(feature = "tls-tcp")]
use crate::internal_core::{TlsTcpRawClient, TlsTcpRawClientBuilder};
use crate::internal_value::Value as StructuredValue;
struct ProtectionSettings {
compression: Compression,
encryption: Encryption,
encryption_explicit: bool,
key: Option<DataProtectionKey>,
keyring: Option<ValueKeyring>,
key_spec: KeyType,
key_format: KeyFormat,
}
impl ProtectionSettings {
fn new(key: DataProtectionKey) -> Self {
Self::with_optional_key(Some(key))
}
fn unprotected() -> Self {
Self::with_optional_key(None)
}
fn with_optional_key(key: Option<DataProtectionKey>) -> Self {
Self {
compression: Compression::default(),
encryption: Encryption::Robust,
encryption_explicit: false,
key,
keyring: None,
key_spec: KeyType::Bytes,
key_format: KeyFormat::NamespaceHash,
}
}
fn finish_with_budget(
self,
budget: crate::internal_core::RequestBudget,
) -> Result<Arc<DataProtection>> {
let protection = match self.key {
Some(key) => match self.keyring {
Some(keyring) => DataProtection::with_keyring_and_key_spec_and_format(
key,
keyring,
self.key_spec,
self.key_format,
self.compression,
self.encryption,
),
None => DataProtection::with_profile_and_key_type_and_format(
key,
self.key_spec,
self.key_format,
self.compression,
self.encryption,
),
},
None => {
if self.encryption_explicit && self.encryption != Encryption::Unprotected {
return Err(crate::internal_core::Error::configuration(
"encryption",
"an encryption profile requires client_root_key",
));
}
DataProtection::with_profile_and_key_type_and_format(
ClientRootKey::zero(),
self.key_spec,
self.key_format,
self.compression,
Encryption::Unprotected,
)
}
}?;
Ok(Arc::new(protection.with_budget(budget)))
}
}
macro_rules! protected_builder_methods {
($builder:ident) => {
impl $builder {
pub fn server_trust(mut self, trust: ServerTrust) -> Self {
self.raw = self.raw.server_trust(trust);
self
}
pub fn trust_certificate(mut self, certificate: Certificate) -> Self {
self.raw = self.raw.trust_certificate(certificate);
self
}
pub fn client_identity(mut self, identity: ClientIdentity) -> Self {
self.raw = self.raw.client_identity(identity);
self
}
pub fn alpn_policy(mut self, policy: AlpnPolicy) -> Self {
self.raw = self.raw.alpn_policy(policy);
self
}
pub fn timeouts(mut self, timeouts: ClientTimeouts) -> Self {
self.raw = self.raw.timeouts(timeouts);
self
}
pub fn retry_policy(mut self, retry: RetryPolicy) -> Self {
self.raw = self.raw.retry_policy(retry);
self
}
pub fn max_in_flight(mut self, maximum: usize) -> Self {
self.raw = self.raw.max_in_flight(maximum);
self
}
pub fn max_in_flight_bytes(mut self, maximum: usize) -> Self {
self.raw = self.raw.max_in_flight_bytes(maximum);
self
}
pub fn namespace_id(mut self, namespace_id: u64) -> Self {
self.raw = self.raw.namespace_id(namespace_id);
self
}
pub fn namespace_name(mut self, namespace_name: impl AsRef<[u8]>) -> Self {
self.raw = self.raw.namespace_name(namespace_name);
self
}
pub fn namespace_policy(mut self, policy: NamespacePolicy) -> Self {
self.raw = self.raw.namespace_policy(policy);
self
}
pub fn compression(mut self, compression: Compression) -> Self {
self.protection.compression = compression;
self
}
pub fn encryption(mut self, encryption: Encryption) -> Self {
self.protection.encryption = encryption;
self.protection.encryption_explicit = true;
self
}
#[deprecated(note = "TypedKey is inferred per operation in v1")]
pub fn key_spec(mut self, key_spec: KeyType) -> Self {
self.protection.key_spec = key_spec;
self
}
pub fn value_keyring(mut self, keyring: ValueKeyring) -> Self {
self.protection.keyring = Some(keyring);
self
}
pub fn key_format(mut self, key_format: KeyFormat) -> Self {
self.protection.key_format = key_format;
self
}
}
};
}
macro_rules! protected_client_methods {
($raw:ty) => {
pub fn raw(&self) -> &$raw {
&self.raw
}
pub async fn ping(&self) -> Result<Duration> {
self.raw.ping().await
}
pub fn namespace_id(&self) -> Option<u64> {
self.raw.namespace_id()
}
pub async fn namespace_open(
&self,
name: impl AsRef<[u8]>,
create_if_missing: bool,
policy: Option<NamespacePolicy>,
) -> Result<NamespaceDescriptor> {
self.raw
.namespace_open(name, create_if_missing, policy)
.await
}
pub async fn namespace_update_policy(
&self,
namespace_id: u64,
expected_revision: u64,
policy: NamespacePolicy,
) -> Result<NamespaceDescriptor> {
self.raw
.namespace_update_policy(namespace_id, expected_revision, policy)
.await
}
pub async fn namespace_delete(
&self,
namespace_id: u64,
expected_revision: u64,
) -> Result<()> {
self.raw
.namespace_delete(namespace_id, expected_revision)
.await
}
pub async fn get(&self, key: impl Into<TypedKey>) -> Result<GetOutcome<Vec<u8>>> {
match self.get_value(key).await? {
GetOutcome::Found(Value::Raw(value)) => Ok(GetOutcome::Found(value)),
GetOutcome::Found(Value::Json(_)) => {
Err(crate::internal_core::value::Error::ExpectedRawValue.into())
}
GetOutcome::NotFound => Ok(GetOutcome::NotFound),
}
}
pub async fn get_value(&self, key: impl Into<TypedKey>) -> Result<GetOutcome<Value>> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self.protection.item_id_in_namespace(namespace_id, key)?;
self.get_value_at_item_id(namespace_id, item_id).await
}
pub async fn get_json(&self, key: impl Into<TypedKey>) -> Result<GetOutcome<JsonValue>> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self.protection.item_id_in_namespace(namespace_id, key)?;
self.get_json_at_item_id(namespace_id, item_id).await
}
pub async fn get_structured(
&self,
key: impl Into<TypedKey>,
) -> Result<GetOutcome<StructuredValue>> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self.protection.item_id_in_namespace(namespace_id, key)?;
self.get_structured_at_item_id(namespace_id, item_id).await
}
pub async fn get_canonical_key(
&self,
canonical_key: impl AsRef<[u8]>,
) -> Result<GetOutcome<Value>> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self
.protection
.item_id_from_canonical_key(namespace_id, canonical_key.as_ref())?;
self.get_value_at_item_id(namespace_id, item_id).await
}
pub async fn get_json_canonical_key(
&self,
canonical_key: impl AsRef<[u8]>,
) -> Result<GetOutcome<JsonValue>> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self
.protection
.item_id_from_canonical_key(namespace_id, canonical_key.as_ref())?;
self.get_json_at_item_id(namespace_id, item_id).await
}
pub async fn get_structured_canonical_key(
&self,
canonical_key: impl AsRef<[u8]>,
) -> Result<GetOutcome<StructuredValue>> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self
.protection
.item_id_from_canonical_key(namespace_id, canonical_key.as_ref())?;
self.get_structured_at_item_id(namespace_id, item_id).await
}
pub async fn get_structured_canonical_key_cbor(
&self,
canonical_key: impl AsRef<[u8]>,
) -> Result<GetOutcome<Vec<u8>>> {
match self.get_structured_canonical_key(canonical_key).await? {
GetOutcome::Found(value) => self
.protection
.encode_structured_cbor(&value)
.map(GetOutcome::Found),
GetOutcome::NotFound => Ok(GetOutcome::NotFound),
}
}
#[cfg(feature = "ffi")]
pub(crate) async fn get_canonical_key_unchecked(
&self,
canonical_key: impl AsRef<[u8]>,
) -> Result<GetOutcome<Value>> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self
.protection
.item_id_from_canonical_key_unchecked(namespace_id, canonical_key.as_ref())?;
self.get_value_at_item_id(namespace_id, item_id).await
}
#[cfg(feature = "ffi")]
pub(crate) async fn get_json_canonical_key_unchecked(
&self,
canonical_key: impl AsRef<[u8]>,
) -> Result<GetOutcome<JsonValue>> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self
.protection
.item_id_from_canonical_key_unchecked(namespace_id, canonical_key.as_ref())?;
self.get_json_at_item_id(namespace_id, item_id).await
}
#[cfg(feature = "ffi")]
pub(crate) async fn get_structured_canonical_key_unchecked(
&self,
canonical_key: impl AsRef<[u8]>,
) -> Result<GetOutcome<StructuredValue>> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self
.protection
.item_id_from_canonical_key_unchecked(namespace_id, canonical_key.as_ref())?;
self.get_structured_at_item_id(namespace_id, item_id).await
}
#[cfg(feature = "ffi")]
pub(crate) async fn get_structured_canonical_key_cbor_unchecked(
&self,
canonical_key: impl AsRef<[u8]>,
) -> Result<GetOutcome<Vec<u8>>> {
match self
.get_structured_canonical_key_unchecked(canonical_key)
.await?
{
GetOutcome::Found(value) => self
.protection
.encode_structured_cbor(&value)
.map(GetOutcome::Found),
GetOutcome::NotFound => Ok(GetOutcome::NotFound),
}
}
async fn get_value_at_item_id(
&self,
namespace_id: u64,
item_id: crate::internal_core::ItemId,
) -> Result<GetOutcome<Value>> {
match self
.raw
.get_in_namespace_with_permit(namespace_id, item_id)
.await?
{
GetOutcome::Found(value) => self
.protection
.decode_in_namespace(namespace_id, item_id, value)
.map(GetOutcome::Found),
GetOutcome::NotFound => Ok(GetOutcome::NotFound),
}
}
async fn get_json_at_item_id(
&self,
namespace_id: u64,
item_id: crate::internal_core::ItemId,
) -> Result<GetOutcome<JsonValue>> {
match self
.raw
.get_in_namespace_with_permit(namespace_id, item_id)
.await?
{
GetOutcome::Found(value) => self
.protection
.decode_json_in_namespace(namespace_id, item_id, value)
.map(GetOutcome::Found),
GetOutcome::NotFound => Ok(GetOutcome::NotFound),
}
}
async fn get_structured_at_item_id(
&self,
namespace_id: u64,
item_id: crate::internal_core::ItemId,
) -> Result<GetOutcome<StructuredValue>> {
match self
.raw
.get_in_namespace_with_permit(namespace_id, item_id)
.await?
{
GetOutcome::Found(value) => self
.protection
.open_structured_in_namespace(namespace_id, item_id, value)
.map(GetOutcome::Found),
GetOutcome::NotFound => Ok(GetOutcome::NotFound),
}
}
pub async fn get_json_exact_item_id(
&self,
namespace_id: u64,
item_id: crate::internal_core::ItemId,
) -> Result<GetOutcome<JsonValue>> {
self.get_json_at_item_id(namespace_id, item_id).await
}
pub async fn get_structured_exact_item_id(
&self,
namespace_id: u64,
item_id: crate::internal_core::ItemId,
) -> Result<GetOutcome<StructuredValue>> {
self.get_structured_at_item_id(namespace_id, item_id).await
}
pub async fn get_structured_exact_item_id_cbor(
&self,
namespace_id: u64,
item_id: crate::internal_core::ItemId,
) -> Result<GetOutcome<Vec<u8>>> {
match self
.get_structured_exact_item_id(namespace_id, item_id)
.await?
{
GetOutcome::Found(value) => self
.protection
.encode_structured_cbor(&value)
.map(GetOutcome::Found),
GetOutcome::NotFound => Ok(GetOutcome::NotFound),
}
}
pub async fn set(
&self,
key: impl Into<TypedKey>,
plaintext: Vec<u8>,
options: SetOptions,
) -> Result<SetOutcome> {
self.set_value(key, Value::Raw(plaintext), options).await
}
pub async fn set_value(
&self,
key: impl Into<TypedKey>,
value: Value,
options: SetOptions,
) -> Result<SetOutcome> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self.protection.item_id_in_namespace(namespace_id, key)?;
let value = self
.protection
.encode_in_namespace(namespace_id, item_id, value)?;
self.raw
.set_in_namespace(namespace_id, item_id, value, options)
.await
}
pub async fn set_json(
&self,
key: impl Into<TypedKey>,
value: JsonValue,
options: SetOptions,
) -> Result<SetOutcome> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self.protection.item_id_in_namespace(namespace_id, key)?;
let value = self
.protection
.encode_json_in_namespace(namespace_id, item_id, value)?;
self.raw
.set_in_namespace(namespace_id, item_id, value, options)
.await
}
pub async fn set_structured(
&self,
key: impl Into<TypedKey>,
value: StructuredValue,
options: SetOptions,
) -> Result<SetOutcome> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self.protection.item_id_in_namespace(namespace_id, key)?;
let value =
self.protection
.seal_structured_in_namespace(namespace_id, item_id, &value)?;
self.raw
.set_in_namespace(namespace_id, item_id, value, options)
.await
}
pub async fn set_json_exact_item_id(
&self,
namespace_id: u64,
item_id: crate::internal_core::ItemId,
value: JsonValue,
options: SetOptions,
) -> Result<SetOutcome> {
let value = self
.protection
.encode_json_in_namespace(namespace_id, item_id, value)?;
self.raw
.set_in_namespace(namespace_id, item_id, value, options)
.await
}
pub async fn set_structured_exact_item_id(
&self,
namespace_id: u64,
item_id: crate::internal_core::ItemId,
value: StructuredValue,
options: SetOptions,
) -> Result<SetOutcome> {
let value =
self.protection
.seal_structured_in_namespace(namespace_id, item_id, &value)?;
self.raw
.set_in_namespace(namespace_id, item_id, value, options)
.await
}
pub async fn set_structured_exact_item_id_cbor(
&self,
namespace_id: u64,
item_id: crate::internal_core::ItemId,
value: impl AsRef<[u8]>,
options: SetOptions,
) -> Result<SetOutcome> {
let value = self.protection.seal_structured_cbor_in_namespace(
namespace_id,
item_id,
value.as_ref(),
)?;
self.raw
.set_in_namespace(namespace_id, item_id, value, options)
.await
}
pub async fn set_v0(
&self,
key: impl Into<TypedKey>,
bytes: Vec<u8>,
options: SetOptions,
) -> Result<SetOutcome> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self.protection.item_id_in_namespace(namespace_id, key)?;
let value = self.protection.pass_through_v0(bytes)?;
self.raw
.set_in_namespace(namespace_id, item_id, value, options)
.await
}
pub async fn get_v0(&self, key: impl Into<TypedKey>) -> Result<GetOutcome<Vec<u8>>> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self.protection.item_id_in_namespace(namespace_id, key)?;
match self
.raw
.get_in_namespace_with_permit(namespace_id, item_id)
.await?
{
GetOutcome::Found(value) => self.protection.open_v0(value).map(GetOutcome::Found),
GetOutcome::NotFound => Ok(GetOutcome::NotFound),
}
}
pub async fn set_v0_exact_item_id(
&self,
namespace_id: u64,
item_id: crate::internal_core::ItemId,
bytes: Vec<u8>,
options: SetOptions,
) -> Result<SetOutcome> {
let value = self.protection.pass_through_v0(bytes)?;
self.raw
.set_in_namespace(namespace_id, item_id, value, options)
.await
}
pub async fn get_v0_exact_item_id(
&self,
namespace_id: u64,
item_id: crate::internal_core::ItemId,
) -> Result<GetOutcome<Vec<u8>>> {
match self
.raw
.get_in_namespace_with_permit(namespace_id, item_id)
.await?
{
GetOutcome::Found(value) => self.protection.open_v0(value).map(GetOutcome::Found),
GetOutcome::NotFound => Ok(GetOutcome::NotFound),
}
}
pub async fn get_v0_canonical_key(
&self,
canonical_key: impl AsRef<[u8]>,
) -> Result<GetOutcome<Vec<u8>>> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self
.protection
.item_id_from_canonical_key(namespace_id, canonical_key.as_ref())?;
self.get_v0_exact_item_id(namespace_id, item_id).await
}
pub async fn set_canonical_key(
&self,
canonical_key: impl AsRef<[u8]>,
value: Value,
options: SetOptions,
) -> Result<SetOutcome> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self
.protection
.item_id_from_canonical_key(namespace_id, canonical_key.as_ref())?;
let value = self
.protection
.encode_in_namespace(namespace_id, item_id, value)?;
self.raw
.set_in_namespace(namespace_id, item_id, value, options)
.await
}
pub async fn set_v0_canonical_key(
&self,
canonical_key: impl AsRef<[u8]>,
bytes: Vec<u8>,
options: SetOptions,
) -> Result<SetOutcome> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self
.protection
.item_id_from_canonical_key(namespace_id, canonical_key.as_ref())?;
self.set_v0_exact_item_id(namespace_id, item_id, bytes, options)
.await
}
pub async fn set_json_canonical_key(
&self,
canonical_key: impl AsRef<[u8]>,
value: JsonValue,
options: SetOptions,
) -> Result<SetOutcome> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self
.protection
.item_id_from_canonical_key(namespace_id, canonical_key.as_ref())?;
let value = self
.protection
.encode_json_in_namespace(namespace_id, item_id, value)?;
self.raw
.set_in_namespace(namespace_id, item_id, value, options)
.await
}
pub async fn set_structured_canonical_key(
&self,
canonical_key: impl AsRef<[u8]>,
value: StructuredValue,
options: SetOptions,
) -> Result<SetOutcome> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self
.protection
.item_id_from_canonical_key(namespace_id, canonical_key.as_ref())?;
let value =
self.protection
.seal_structured_in_namespace(namespace_id, item_id, &value)?;
self.raw
.set_in_namespace(namespace_id, item_id, value, options)
.await
}
pub async fn set_structured_canonical_key_cbor(
&self,
canonical_key: impl AsRef<[u8]>,
value: impl AsRef<[u8]>,
options: SetOptions,
) -> Result<SetOutcome> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self
.protection
.item_id_from_canonical_key(namespace_id, canonical_key.as_ref())?;
let value = self.protection.seal_structured_cbor_in_namespace(
namespace_id,
item_id,
value.as_ref(),
)?;
self.raw
.set_in_namespace(namespace_id, item_id, value, options)
.await
}
#[cfg(feature = "ffi")]
pub(crate) async fn set_canonical_key_unchecked(
&self,
canonical_key: impl AsRef<[u8]>,
value: Value,
options: SetOptions,
) -> Result<SetOutcome> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self
.protection
.item_id_from_canonical_key_unchecked(namespace_id, canonical_key.as_ref())?;
let value = self
.protection
.encode_in_namespace(namespace_id, item_id, value)?;
self.raw
.set_in_namespace(namespace_id, item_id, value, options)
.await
}
#[cfg(feature = "ffi")]
pub(crate) async fn set_json_canonical_key_unchecked(
&self,
canonical_key: impl AsRef<[u8]>,
value: JsonValue,
options: SetOptions,
) -> Result<SetOutcome> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self
.protection
.item_id_from_canonical_key_unchecked(namespace_id, canonical_key.as_ref())?;
let value = self
.protection
.encode_json_in_namespace(namespace_id, item_id, value)?;
self.raw
.set_in_namespace(namespace_id, item_id, value, options)
.await
}
#[cfg(feature = "ffi")]
pub(crate) async fn get_v0_canonical_key_unchecked(
&self,
canonical_key: impl AsRef<[u8]>,
) -> Result<GetOutcome<Vec<u8>>> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self
.protection
.item_id_from_canonical_key_unchecked(namespace_id, canonical_key.as_ref())?;
self.get_v0_exact_item_id(namespace_id, item_id).await
}
#[cfg(feature = "ffi")]
pub(crate) async fn set_v0_canonical_key_unchecked(
&self,
canonical_key: impl AsRef<[u8]>,
bytes: Vec<u8>,
options: SetOptions,
) -> Result<SetOutcome> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self
.protection
.item_id_from_canonical_key_unchecked(namespace_id, canonical_key.as_ref())?;
self.set_v0_exact_item_id(namespace_id, item_id, bytes, options)
.await
}
#[cfg(feature = "ffi")]
pub(crate) async fn set_structured_canonical_key_cbor_unchecked(
&self,
canonical_key: impl AsRef<[u8]>,
value: impl AsRef<[u8]>,
options: SetOptions,
) -> Result<SetOutcome> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self
.protection
.item_id_from_canonical_key_unchecked(namespace_id, canonical_key.as_ref())?;
let value = self.protection.seal_structured_cbor_in_namespace(
namespace_id,
item_id,
value.as_ref(),
)?;
self.raw
.set_in_namespace(namespace_id, item_id, value, options)
.await
}
pub async fn delete(&self, key: impl Into<TypedKey>) -> Result<DeleteOutcome> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self.protection.item_id_in_namespace(namespace_id, key)?;
self.raw.delete_in_namespace(namespace_id, item_id).await
}
pub async fn delete_canonical_key(
&self,
canonical_key: impl AsRef<[u8]>,
) -> Result<DeleteOutcome> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self
.protection
.item_id_from_canonical_key(namespace_id, canonical_key.as_ref())?;
self.raw.delete_in_namespace(namespace_id, item_id).await
}
#[cfg(feature = "ffi")]
pub(crate) async fn delete_canonical_key_unchecked(
&self,
canonical_key: impl AsRef<[u8]>,
) -> Result<DeleteOutcome> {
let namespace_id = self.raw.ensure_namespace_id().await?;
let item_id = self
.protection
.item_id_from_canonical_key_unchecked(namespace_id, canonical_key.as_ref())?;
self.raw.delete_in_namespace(namespace_id, item_id).await
}
pub async fn experimental_stats(&self) -> Result<String> {
self.raw.experimental_stats().await
}
pub async fn experimental_sync(&self) -> Result<()> {
self.raw.experimental_sync().await
}
pub fn connection_state(&self) -> ConnectionState {
self.raw.connection_state()
}
pub async fn reconnect(&self) -> Result<()> {
self.raw.reconnect().await
}
pub async fn close(&self) -> Result<()> {
self.raw.close().await
}
pub fn request_budget(&self) -> crate::internal_core::RequestBudget {
self.protection.request_budget()
}
pub fn value_limits(&self) -> crate::internal_core::value::ValueLimits {
self.protection.value_limits()
}
};
}
#[cfg(feature = "quic-quinn")]
#[derive(Clone)]
pub struct ProtectedClient {
raw: RawClient,
protection: Arc<DataProtection>,
}
#[cfg(feature = "quic-quinn")]
pub struct ProtectedClientBuilder {
raw: RawClientBuilder,
protection: ProtectionSettings,
}
#[cfg(feature = "quic-quinn")]
protected_builder_methods!(ProtectedClientBuilder);
#[cfg(feature = "quic-quinn")]
impl ProtectedClientBuilder {
pub async fn connect(self) -> Result<ProtectedClient> {
let raw = self.raw.connect().await?;
let protection = self.protection.finish_with_budget(raw.request_budget())?;
Ok(ProtectedClient { raw, protection })
}
}
#[cfg(feature = "quic-quinn")]
impl ProtectedClient {
pub async fn connect(endpoint: &str, key: DataProtectionKey) -> Result<Self> {
Self::builder(endpoint.parse()?, key).connect().await
}
pub fn builder(endpoint: Endpoint, key: DataProtectionKey) -> ProtectedClientBuilder {
ProtectedClientBuilder {
raw: RawClient::builder(endpoint),
protection: ProtectionSettings::new(key),
}
}
pub fn builder_with_keyring(
endpoint: Endpoint,
item_id_root: ClientRootKey,
keyring: ValueKeyring,
) -> ProtectedClientBuilder {
ProtectedClientBuilder {
raw: RawClient::builder(endpoint),
protection: ProtectionSettings {
key: Some(item_id_root),
keyring: Some(keyring),
..ProtectionSettings::unprotected()
},
}
}
pub fn builder_unprotected(endpoint: Endpoint) -> ProtectedClientBuilder {
ProtectedClientBuilder {
raw: RawClient::builder(endpoint),
protection: ProtectionSettings::unprotected(),
}
}
protected_client_methods!(RawClient);
}
#[cfg(feature = "tls-tcp")]
#[derive(Clone)]
pub struct TlsTcpProtectedClient {
raw: TlsTcpRawClient,
protection: Arc<DataProtection>,
}
#[cfg(feature = "tls-tcp")]
pub struct TlsTcpProtectedClientBuilder {
raw: TlsTcpRawClientBuilder,
protection: ProtectionSettings,
}
#[cfg(feature = "tls-tcp")]
protected_builder_methods!(TlsTcpProtectedClientBuilder);
#[cfg(feature = "tls-tcp")]
impl TlsTcpProtectedClientBuilder {
pub async fn connect(self) -> Result<TlsTcpProtectedClient> {
let raw = self.raw.connect().await?;
let protection = self.protection.finish_with_budget(raw.request_budget())?;
Ok(TlsTcpProtectedClient { raw, protection })
}
}
#[cfg(feature = "tls-tcp")]
impl TlsTcpProtectedClient {
pub async fn connect(endpoint: &str, key: DataProtectionKey) -> Result<Self> {
Self::builder(endpoint.parse()?, key).connect().await
}
pub fn builder(endpoint: Endpoint, key: DataProtectionKey) -> TlsTcpProtectedClientBuilder {
TlsTcpProtectedClientBuilder {
raw: TlsTcpRawClient::builder(endpoint),
protection: ProtectionSettings::new(key),
}
}
pub fn builder_unprotected(endpoint: Endpoint) -> TlsTcpProtectedClientBuilder {
TlsTcpProtectedClientBuilder {
raw: TlsTcpRawClient::builder(endpoint),
protection: ProtectionSettings::unprotected(),
}
}
protected_client_methods!(TlsTcpRawClient);
}
#[cfg(feature = "quic-compio")]
#[derive(Clone)]
pub struct LocalProtectedClient {
raw: LocalRawClient,
protection: Arc<DataProtection>,
}
#[cfg(feature = "quic-compio")]
pub struct LocalProtectedClientBuilder {
raw: LocalRawClientBuilder,
protection: ProtectionSettings,
}
#[cfg(feature = "quic-compio")]
protected_builder_methods!(LocalProtectedClientBuilder);
#[cfg(feature = "quic-compio")]
impl LocalProtectedClientBuilder {
pub async fn connect(self) -> Result<LocalProtectedClient> {
let raw = self.raw.connect().await?;
let protection = self.protection.finish_with_budget(raw.request_budget())?;
Ok(LocalProtectedClient { raw, protection })
}
}
#[cfg(feature = "quic-compio")]
impl LocalProtectedClient {
pub async fn connect(endpoint: &str, key: DataProtectionKey) -> Result<Self> {
Self::builder(endpoint.parse()?, key).connect().await
}
pub fn builder(endpoint: Endpoint, key: DataProtectionKey) -> LocalProtectedClientBuilder {
LocalProtectedClientBuilder {
raw: LocalRawClient::builder(endpoint),
protection: ProtectionSettings::new(key),
}
}
pub fn builder_with_keyring(
endpoint: Endpoint,
item_id_root: ClientRootKey,
keyring: ValueKeyring,
) -> LocalProtectedClientBuilder {
LocalProtectedClientBuilder {
raw: LocalRawClient::builder(endpoint),
protection: ProtectionSettings {
key: Some(item_id_root),
keyring: Some(keyring),
..ProtectionSettings::unprotected()
},
}
}
pub fn builder_unprotected(endpoint: Endpoint) -> LocalProtectedClientBuilder {
LocalProtectedClientBuilder {
raw: LocalRawClient::builder(endpoint),
protection: ProtectionSettings::unprotected(),
}
}
protected_client_methods!(LocalRawClient);
}