diff --git a/notifico-core/src/contact.rs b/notifico-core/src/contact.rs index 7791466..562e792 100644 --- a/notifico-core/src/contact.rs +++ b/notifico-core/src/contact.rs @@ -1,5 +1,6 @@ use crate::error::EngineError; use serde::{Deserialize, Serialize}; +use std::str::FromStr; use utoipa::ToSchema; #[derive(Clone, Debug, Serialize, Deserialize, ToSchema)] @@ -8,21 +9,14 @@ pub struct Contact { pub value: String, } -impl Contact { - pub fn from_url(url: &str) -> Result { - let mut iter = url.split("://"); - let r#type = iter - .next() - .ok_or(EngineError::InvalidContactFormat( - "Invalid URL format".to_string(), - ))? - .to_owned(); - let value = iter - .next() - .ok_or(EngineError::InvalidContactFormat( - "Invalid URL format".to_string(), - ))? - .to_owned(); +impl FromStr for Contact { + type Err = EngineError; + + fn from_str(s: &str) -> Result { + let (r#type, value) = s.split_once(':').ok_or(EngineError::InvalidContactFormat( + "Invalid URL format".to_string(), + ))?; + let (r#type, value) = (r#type.to_owned(), value.to_owned()); Ok(Self { r#type, value }) } } diff --git a/notifico-core/src/credentials.rs b/notifico-core/src/credentials.rs index 691015a..97ac507 100644 --- a/notifico-core/src/credentials.rs +++ b/notifico-core/src/credentials.rs @@ -22,7 +22,7 @@ impl Credential { pub fn transport(&self) -> &str { match self { Credential::Long { r#type, .. } => r#type, - Credential::Short(url) => url.split("://").next().unwrap(), + Credential::Short(url) => url.split(":").next().unwrap(), } } } diff --git a/notificox/src/main.rs b/notificox/src/main.rs index f039c10..b63e406 100644 --- a/notificox/src/main.rs +++ b/notificox/src/main.rs @@ -120,15 +120,7 @@ async fn main() { pipeline }; - let contacts: Vec = { - let mut contacts = vec![]; - - for contact in to { - contacts.push(Contact::from_url(&contact).unwrap()) - } - - contacts - }; + let contacts: Vec = to.iter().map(|s| s.parse().unwrap()).collect(); let recipient = Recipient { id: Uuid::nil(), diff --git a/transports/notifico-gotify/src/credentials.rs b/transports/notifico-gotify/src/credentials.rs index 6feebbd..e28df00 100644 --- a/transports/notifico-gotify/src/credentials.rs +++ b/transports/notifico-gotify/src/credentials.rs @@ -22,7 +22,7 @@ impl TryFrom for GotifyCredentials { .map_err(|_| EngineError::InvalidCredentialFormat)?) } Credential::Short(url) => { - let mut url = url.splitn(2, "://"); + let mut url = url.splitn(2, ":"); let _ = url.next(); let url = url.next().unwrap_or_default(); let url = Url::parse(url).map_err(|_| EngineError::InvalidCredentialFormat)?; diff --git a/transports/notifico-pushover/src/lib.rs b/transports/notifico-pushover/src/lib.rs index 359f0e8..ba1b5f3 100644 --- a/transports/notifico-pushover/src/lib.rs +++ b/transports/notifico-pushover/src/lib.rs @@ -26,10 +26,7 @@ impl TryFrom for PushoverCredentials { .map_err(|_| EngineError::InvalidCredentialFormat)?) } Credential::Short(url) => Ok(Self { - token: url - .strip_prefix("pushover://") - .unwrap_or_default() - .to_owned(), + token: url.strip_prefix("pushover:").unwrap_or_default().to_owned(), }), } } diff --git a/transports/notifico-slack/src/credentials.rs b/transports/notifico-slack/src/credentials.rs index 2184602..6296377 100644 --- a/transports/notifico-slack/src/credentials.rs +++ b/transports/notifico-slack/src/credentials.rs @@ -21,7 +21,7 @@ impl TryFrom for SlackCredentials { .map_err(|_| EngineError::InvalidCredentialFormat)?) } Credential::Short(url) => Ok(Self { - token: url.strip_prefix("slack://").unwrap_or_default().to_owned(), + token: url.strip_prefix("slack:").unwrap_or_default().to_owned(), }), } } diff --git a/transports/notifico-smpp/src/credentials.rs b/transports/notifico-smpp/src/credentials.rs index 3dd3c5a..a55580e 100644 --- a/transports/notifico-smpp/src/credentials.rs +++ b/transports/notifico-smpp/src/credentials.rs @@ -25,6 +25,8 @@ impl TryFrom for SmppServerCredentials { .map_err(|_| EngineError::InvalidCredentialFormat)?) } Credential::Short(url) => { + let url = url.strip_prefix("smpp:").unwrap_or_default(); + let url = String::from("smpp://") + url; let url = Url::parse(&url).map_err(|_| EngineError::InvalidCredentialFormat)?; Ok(Self { host: url.host_str().unwrap_or_default().to_owned(), diff --git a/transports/notifico-smtp/src/credentials.rs b/transports/notifico-smtp/src/credentials.rs index acc030d..39f278b 100644 --- a/transports/notifico-smtp/src/credentials.rs +++ b/transports/notifico-smtp/src/credentials.rs @@ -28,6 +28,8 @@ impl TryFrom for SmtpServerCredentials { .map_err(|_| EngineError::InvalidCredentialFormat)?) } Credential::Short(url) => { + let url = url.strip_prefix("smtp:").unwrap_or_default(); + let url = String::from("smtp://") + url; let url = Url::parse(&url).map_err(|_| EngineError::InvalidCredentialFormat)?; let query: BTreeMap, Cow> = url.query_pairs().collect(); let tls = query diff --git a/transports/notifico-telegram/src/lib.rs b/transports/notifico-telegram/src/lib.rs index 8b858bb..5b73542 100644 --- a/transports/notifico-telegram/src/lib.rs +++ b/transports/notifico-telegram/src/lib.rs @@ -32,10 +32,7 @@ impl TryFrom for TelegramBotCredentials { .map_err(|_| EngineError::InvalidCredentialFormat)?) } Credential::Short(url) => Ok(Self { - token: url - .strip_prefix("telegram://") - .unwrap_or_default() - .to_owned(), + token: url.strip_prefix("telegram:").unwrap_or_default().to_owned(), }), } } diff --git a/transports/notifico-whatsapp/src/credentials.rs b/transports/notifico-whatsapp/src/credentials.rs index cdaf7de..f03d833 100644 --- a/transports/notifico-whatsapp/src/credentials.rs +++ b/transports/notifico-whatsapp/src/credentials.rs @@ -26,7 +26,7 @@ impl TryFrom for WhatsAppCredentials { Credential::Short(url) => { static WABA_REGEX: OnceLock = OnceLock::new(); let regex = WABA_REGEX - .get_or_init(|| Regex::new("^waba://([0-9]+):([0-9a-zA-Z]+)$").unwrap()); + .get_or_init(|| Regex::new("^waba:([0-9]+):([0-9a-zA-Z]+)$").unwrap()); let caps = regex .captures(&url)