use std::{fmt::Debug, sync::Arc};
use oxinat_derive::uri_builder_alias;
use crate::{UriBuildError, UriBuilder, Version};
uri_builder_alias!(EventsAdminUriBuilder);
ImplEventsAdminUriBuilder! {
(String),
}
#[derive(Clone, Debug, Default, UriBuilder)]
#[match_path(path = "{parent}/events")]
#[match_path(path = "{parent}/projects/{project}/events")]
pub struct EventsUriBuilder<Parent>
where
Parent: EventsAdminUriBuilder,
{
#[param]
project: Option<String>,
#[parent]
parent: Option<Arc<Parent>>
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub enum EventType {
#[default]
All,
ByEvent,
Multiple,
One,
}
macro_rules! is_event_type {
() => {
(|this: &Self| this.event_type == EventType::All)
};
($type:ident) => {
(|this: &Self| this.event_type == EventType::$type)
};
}
#[derive(Clone, Debug, Default, UriBuilder)]
#[match_path(path = "{parent}/action", requires = "is_event_type!(One)")]
#[match_path(path = "{parent}/actions", requires = "is_event_type!(Multiple)")]
#[match_path(path = "{parent}/actionsbyevent", requires = "is_event_type!(ByEvent)")]
#[match_path(path = "{parent}/allactions", requires = "is_event_type!()")]
pub struct ActionsUriBuilder<'a> {
event_type: EventType,
#[parent]
parent: Option<&'a EventsUriBuilder<String>>
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub enum DeliveredType {
CleanUp,
Count,
Summary,
#[default]
None
}
macro_rules! is_deliver_type {
() => {
(|this: &Self| this.deliver_type == DeliveredType::default())
};
($type:ident) => {
(|this: &Self| this.deliver_type == DeliveredType::$type)
};
}
#[derive(Clone, Debug, Default, UriBuilder)]
#[match_path(path = "{parent}/delivered/cleanup", requires = "is_deliver_type!(CleanUp)")]
#[match_path(path = "{parent}/delivered/count", requires = "is_deliver_type!(Count)")]
#[match_path(path = "{parent}/delivered/summary", requires = "is_deliver_type!(Summary)")]
#[match_path(path = "{parent}/delivered/{id}")]
#[match_path(path = "{parent}/delivered")]
pub struct DeliveredUriBuilder<'a> {
deliver_type: DeliveredType,
#[param]
id: Option<String>,
#[parent]
parent: Option<&'a EventsUriBuilder<String>>
}
#[derive(Clone, Debug, Default, UriBuilder)]
#[match_path(path = "{parent}/event", requires = "is_event_type!(One)")]
#[match_path(path = "{parent}/events")]
pub struct EventUriBuilder<'a> {
event_type: EventType,
#[parent]
parent: Option<&'a EventsUriBuilder<String>>
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub enum SubscriptionAction {
Activate,
All,
Deactivate,
Filter,
Validate,
#[default]
None
}
macro_rules! is_subscription_action {
() => {
(|this: &Self| this.action == SubscriptionAction::default())
};
($type:ident) => {
(|this: &Self| this.action == SubscriptionAction::$type)
};
}
#[derive(Clone, Debug, Default, UriBuilder)]
#[match_path(path = "{parent}/subscriptions", requires = "is_subscription_action!(All)")]
#[match_path(path = "{parent}/subscription/filter", requires = "is_subscription_action!(Filter)")]
#[match_path(path = "{parent}/subscription/validate", requires = "is_subscription_action!(Validate)")]
#[match_path(path = "{parent}/subscription/{id}/activate", requires = "is_subscription_action!(Activate)")]
#[match_path(path = "{parent}/subscription/{id}/deactivate", requires = "is_subscription_action!(Deactivate)")]
#[match_path(path = "{parent}/subscription/{id}")]
#[match_path(path = "{parent}/subscription")]
pub struct SubscriptionUriBuilder<'a> {
action: SubscriptionAction,
#[param]
id: Option<String>,
#[parent]
parent: Option<&'a EventsUriBuilder<String>>,
}
impl EventUriBuilder<'_> {
pub fn build_properties(&self) -> crate::BuildResult {
if is_event_type!(One)(self) {
self.build_join("properties")
} else {
Err(UriBuildError::Validation.into())
}
}
}
impl EventsUriBuilder<String> {
pub fn actions(&self) -> ActionsUriBuilder {
ActionsUriBuilder::from_parent(&Arc::new(self))
}
pub fn build_pref(&self) -> crate::BuildResult {
self.build_join("prefs")
}
pub fn delivered(&self) -> DeliveredUriBuilder {
DeliveredUriBuilder::from_parent(&Arc::new(self))
}
pub fn event(&self) -> EventUriBuilder {
EventUriBuilder::from_parent(&Arc::new(self))
}
pub fn subscription(&self) -> SubscriptionUriBuilder {
SubscriptionUriBuilder::from_parent(&Arc::new(self))
}
}
#[derive(Clone, Debug, Default, UriBuilder)]
#[match_path(path = "{parent}/event_tracking")]
#[match_path(path = "{parent}/event_tracking/{key}")]
pub struct EventTrackingUriBuilder<Parent>
where
Parent: EventsAdminUriBuilder,
{
#[param]
key: Option<String>,
#[parent]
parent: Option<Arc<Parent>>,
}
pub trait EventsUri: Version {
#[inline]
fn events(&self) -> EventsUriBuilder<String> {
EventsUriBuilder::from_parent(self.root_uri().into())
}
#[inline]
fn event_tracking(&self) -> EventTrackingUriBuilder<String> {
EventTrackingUriBuilder::from_parent(self.root_uri().into())
}
}