1use crate::error::CoreError;
3use crate::retry::RetryPolicy;
4use crate::webhook_service::BoxedWebhookSender;
5use rustc_hash::FxHashMap;
6use serde::{Deserialize, Serialize};
7use std::sync::Arc;
8use std::time::Duration;
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct ServiceInfo {
13 pub name: String,
15 pub version: String,
17 pub description: String,
19 pub webhook_url_format: String,
21 pub supports_embeds: bool,
23 pub max_content_length: usize,
25 pub features: Vec<String>,
27 pub rate_limit_retry_after: Option<Duration>,
29 pub documentation_url: Option<String>,
31 pub icon_url: Option<String>,
33 pub platforms: Vec<Platform>,
35}
36
37#[derive(Debug, Clone, Serialize, Deserialize)]
39pub enum Platform {
40 Discord,
42 Slack,
44 Teams,
46 Mattermost,
48 Generic,
50 Custom(String),
52}
53
54#[derive(Debug, Clone)]
56pub struct ServiceConfig {
57 pub url: String,
59 pub timeout: Duration,
61 pub retry_policy: RetryPolicy,
63 pub custom_headers: FxHashMap<String, String>,
65 pub additional_config: FxHashMap<String, serde_json::Value>,
67}
68
69impl ServiceConfig {
70 #[must_use]
72 pub fn new(url: impl Into<String>) -> Self {
73 Self {
74 url: url.into(),
75 timeout: Duration::from_secs(30),
76 retry_policy: RetryPolicy::default(),
77 custom_headers: FxHashMap::default(),
78 additional_config: FxHashMap::default(),
79 }
80 }
81
82 #[must_use]
84 pub const fn with_timeout(mut self, timeout: Duration) -> Self {
85 self.timeout = timeout;
86 self
87 }
88
89 #[must_use]
91 pub const fn with_retry_policy(mut self, policy: RetryPolicy) -> Self {
92 self.retry_policy = policy;
93 self
94 }
95
96 #[must_use]
98 pub fn with_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
99 self.custom_headers.insert(key.into(), value.into());
100 self
101 }
102
103 #[must_use]
109 pub fn with_config(
110 mut self,
111 key: impl Into<String>,
112 value: impl Serialize,
113 ) -> Result<Self, CoreError> {
114 self.additional_config
115 .insert(key.into(), serde_json::to_value(value)?);
116 Ok(self)
117 }
118}
119
120pub trait ServiceBuilder: Send + Sync {
125 fn build(
132 &self,
133 config: ServiceConfig,
134 ) -> Result<BoxedWebhookSender, Box<dyn std::error::Error + Send + Sync>>;
135
136 fn info(&self) -> Arc<ServiceInfo>;
138
139 #[must_use]
144 fn validate_url(&self, _url: &str) -> bool {
145 true
146 }
147}