laravel_sluggable/
config.rs1use std::fmt;
5use std::sync::Arc;
6
7use laravel::config::Section;
8
9use crate::slugify::SlugifyOptions;
10
11type MethodFn = dyn Fn(&str, &str) -> String + Send + Sync;
12type SuffixFn = dyn Fn(&str, &str, &[String], &str) -> String + Send + Sync;
13
14#[derive(Clone)]
17pub struct SlugMethod(Arc<MethodFn>);
18
19impl SlugMethod {
20 pub fn new(method: impl Fn(&str, &str) -> String + Send + Sync + 'static) -> Self {
22 Self(Arc::new(method))
23 }
24
25 pub(crate) fn call(&self, source: &str, separator: &str) -> String {
26 (self.0)(source, separator)
27 }
28}
29
30impl fmt::Debug for SlugMethod {
31 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
32 formatter.write_str("SlugMethod")
33 }
34}
35
36#[derive(Clone)]
39pub struct UniqueSuffix(Arc<SuffixFn>);
40
41impl UniqueSuffix {
42 pub fn new(suffix: impl Fn(&str, &str, &[String], &str) -> String + Send + Sync + 'static) -> Self {
44 Self(Arc::new(suffix))
45 }
46
47 pub(crate) fn call(&self, slug: &str, separator: &str, list: &[String], first: &str) -> String {
48 (self.0)(slug, separator, list, first)
49 }
50}
51
52impl fmt::Debug for UniqueSuffix {
53 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
54 formatter.write_str("UniqueSuffix")
55 }
56}
57
58#[derive(Debug, Clone, Section)]
68pub struct SluggableConfig {
69 pub max_length: Option<usize>,
71
72 #[section(default = true)]
74 pub max_length_keep_words: bool,
75
76 pub method: Option<SlugMethod>,
78
79 #[section(default = "-".to_owned())]
81 pub separator: String,
82
83 #[section(default = true)]
85 pub unique: bool,
86
87 pub unique_suffix: Option<UniqueSuffix>,
89
90 #[section(default = "2".to_owned())]
92 pub first_unique_suffix: String,
93
94 pub reserved: Option<Vec<String>>,
96
97 #[section(default = false)]
99 pub on_update: bool,
100
101 #[section(default = SlugifyOptions::default())]
103 pub slug_engine_options: SlugifyOptions,
104}
105
106#[derive(Debug, Clone)]
110pub struct SlugOptions {
111 pub max_length: Option<usize>,
113 pub max_length_keep_words: bool,
115 pub method: Option<SlugMethod>,
117 pub separator: String,
119 pub unique: bool,
121 pub unique_suffix: Option<UniqueSuffix>,
123 pub first_unique_suffix: String,
125 pub reserved: Option<Vec<String>>,
127 pub on_update: bool,
129 pub slug_engine_options: SlugifyOptions,
131}
132
133pub(crate) fn current() -> SluggableConfig {
136 if laravel::context::Context::current().is_none() {
137 return SluggableConfig::default();
138 }
139
140 laravel::config().get::<SluggableConfig>().cloned().unwrap_or_default()
141}