use adk_awp::v2::store::StoreError;
#[cfg(feature = "awp-redis")]
mod redis;
#[cfg(any(feature = "awp-sqlite", feature = "awp-postgres"))]
mod sql;
#[cfg(feature = "awp-redis")]
pub use redis::RedisEdgeStores;
#[cfg(feature = "awp-postgres")]
pub use sql::PostgresEdgeStores;
#[cfg(feature = "awp-sqlite")]
pub use sql::SqliteEdgeStores;
pub const DEFAULT_SITE: &str = "default";
fn backend(name: &str, action: &str, error: &dyn std::fmt::Display) -> StoreError {
StoreError::Backend(format!("the {name} edge store could not {action}: {error}"))
}
fn encode<T: serde::Serialize>(name: &str, value: &T) -> Result<String, StoreError> {
serde_json::to_string(value).map_err(|error| backend(name, "encode a record", &error))
}
fn decode<T: serde::de::DeserializeOwned>(name: &str, text: &str) -> Result<T, StoreError> {
serde_json::from_str(text).map_err(|error| backend(name, "read a stored record", &error))
}
#[cfg(any(feature = "awp-sqlite", feature = "awp-postgres"))]
fn wire_name<T: serde::Serialize>(value: &T) -> String {
match serde_json::to_value(value) {
Ok(serde_json::Value::String(name)) => name,
Ok(other) => other.to_string(),
Err(_) => String::new(),
}
}
const CAS_ATTEMPTS: usize = 16;
fn apply_update(
record: &mut adk_awp::v2::store::SubscriptionRecord,
update: adk_awp::v2::store::SubscriptionUpdate,
) {
if let Some(types) = update.types {
record.subscription.types = types;
}
if let Some(url) = update.url {
record.subscription.url = url;
}
if let Some(state) = update.state {
if state == awp_types::SubscriptionState::Active {
record.subscription.failures = 0;
}
record.subscription.state = state;
}
}
fn apply_delivery(record: &mut adk_awp::v2::store::SubscriptionRecord, delivered: bool) {
if delivered {
record.subscription.failures = 0;
} else {
record.subscription.failures = record.subscription.failures.saturating_add(1);
if record.subscription.failures >= awp_types::SubscriptionSuspended::MAX_FAILURES {
record.subscription.state = awp_types::SubscriptionState::Suspended;
}
}
}
fn apply_transition(
mut record: adk_awp::v2::store::HeldCallRecord,
transition: adk_awp::v2::store::HeldCallTransition,
) -> Result<adk_awp::v2::store::HeldCallRecord, StoreError> {
let id = record.call.id.clone();
if !adk_awp::v2::store::held_call_transition_allowed(record.call.state, transition.to) {
return Err(StoreError::Conflict(format!(
"held call {id} cannot move from {:?} to {:?}",
record.call.state, transition.to
)));
}
if transition.to == awp_types::HeldCallState::Completed
&& transition.result.is_none()
&& transition.error.is_none()
{
return Err(StoreError::Conflict(format!(
"held call {id} completes with a result or an error"
)));
}
record.call.state = transition.to;
if transition.result.is_some() {
record.call.result = transition.result;
}
if transition.error.is_some() {
record.call.error = transition.error;
}
Ok(record)
}
#[cfg(feature = "awp-redis")]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct IdempotencyEntry {
digest: String,
began_us: i64,
#[serde(default, skip_serializing_if = "Option::is_none")]
outcome: Option<adk_awp::v2::store::RecordedOutcome>,
}