laravel_mailing_list/
lib.rs1mod campaigns;
32mod commands;
33mod config;
34mod email;
35pub mod events;
36pub mod lists;
37mod local;
38#[cfg(feature = "mailchimp")]
39mod mailchimp;
40pub mod models;
41mod render;
42mod routes;
43#[cfg(feature = "template-engine")]
44pub mod templates;
45pub mod transfer;
46
47use std::collections::BTreeMap;
48use std::sync::Arc;
49
50use laravel::config::{Config, Env};
51use laravel::container::Registrar;
52use laravel::foundation::ServiceProvider;
53
54pub use crate::campaigns::{CampaignDraft, CampaignMail, PrepareCampaign, SendDeliveries, Stats};
55pub use crate::config::MailingListConfig;
56pub use crate::email::Email;
57pub use crate::lists::{Driver, List, ListConfig, ListHandle, Subscribed, Subscription};
58pub use crate::local::ConfirmSubscription;
59pub use crate::routes::{SubscribeForm, confirm, subscribe, unsubscribe, unsubscribe_page};
60
61use crate::lists::Lists;
62
63#[derive(Clone, Debug, Default)]
65pub struct MailingListServiceProvider {
66 lists: BTreeMap<String, ListConfig>,
67 #[cfg(feature = "template-engine")]
68 template_engine: bool,
69}
70
71impl MailingListServiceProvider {
72 #[must_use]
74 pub fn new() -> Self {
75 Self::default()
76 }
77
78 #[must_use]
85 pub fn list(mut self, key: impl Into<String>, list: List) -> Self {
86 let key = key.into();
87 assert!(
88 !key.is_empty()
89 && key.len() <= 64
90 && key.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_'),
91 "the mailing list key `{key}` must be 1 to 64 letters, digits, `-` or `_`"
92 );
93 assert!(
94 !self.lists.contains_key(&key),
95 "the mailing list `{key}` is declared twice"
96 );
97 self.lists.insert(key.clone(), list.into_config(key));
98 self
99 }
100}
101
102impl MailingListServiceProvider {
103 #[cfg(feature = "template-engine")]
107 #[must_use]
108 pub fn with_template_engine(mut self) -> Self {
109 self.template_engine = true;
110 self
111 }
112}
113
114#[cfg(feature = "template-engine")]
116#[derive(Clone, Copy, Debug, Default)]
117pub(crate) struct UseTemplateEngine(pub(crate) bool);
118
119laravel::db::migrations!(fn migrations = "database/migrations");
120
121impl ServiceProvider for MailingListServiceProvider {
122 fn config(&self, config: &mut Config, env: &Env) {
123 config.discover(env!("CARGO_CRATE_NAME"), env);
124 }
125
126 fn register(&self, app: &mut Registrar) {
127 app.discover(env!("CARGO_CRATE_NAME"));
128 app.migrations().add(migrations());
129 app.views()
130 .namespace("mailing-list", laravel::embed!("resources/views"));
131 app.instance(Lists(Arc::new(self.lists.clone())));
132 #[cfg(feature = "template-engine")]
133 app.instance(UseTemplateEngine(self.template_engine));
134 let config = app.config().section::<MailingListConfig>().clone();
135 app.routing().web(move |route| routes::register(route, &config));
136 }
137}