url-cleaner-engine 0.11.0

The engine behind URL Cleaner.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! A common API for getting and setting various parts of [`BetterUrl`]s.

use std::borrow::Cow;
use std::str::FromStr;

use thiserror::Error;
use serde::{Serialize, Deserialize};
#[expect(unused_imports, reason = "Used in a doc comment.")]
use url::Url;

use crate::types::*;
use crate::glue::*;
use crate::util::*;

/// A common API for getting and setting various parts of [`BetterUrl`]s.
///
/// For most parts, setting a URL's part to a value then getting that same part returns the same value.
///
/// Exceptions include setting part segments to values containing the split, `After`/`Before`/`Next` variants always returning [`None`], and probably some other things. I'll fix this doc later.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Suitability)]
#[serde(deny_unknown_fields)]
pub enum UrlPart {
    /// Print debug information about the contained [`Self`].
    #[suitable(never)]
    Debug(Box<Self>),



    /// The whole URL.
    /// # Examples
    /// ```
    /// use url_cleaner_engine::types::*;
    ///
    /// let mut url = BetterUrl::parse("https://example.com").unwrap();
    /// assert_eq!(UrlPart::Whole.get(&url), Some("https://example.com/".into()));
    ///
    /// UrlPart::Whole.set(&mut url, Some("https://example2.com")).unwrap();
    /// assert_eq!(UrlPart::Whole.get(&url), Some("https://example2.com/".into()));
    /// assert_eq!(url.as_str(), "https://example2.com/");
    /// ```
    Whole,



    /// [`Url::scheme`] and [`BetterUrl::set_scheme`].
    Scheme,
    /// [`Url::username`] and [`BetterUrl::set_username`].
    Username,
    /// [`Url::password`] and [`BetterUrl::set_password`].
    Password,



    /// [`Url::host`] and [`BetterUrl::set_host`].
    Host,
    /// [`BetterUrl::normalized_host`].
    /// # Errors
    /// Trying to set this part returns [`SetUrlPartError::CannotSetNormalizedHost`].
    NormalizedHost,



    /// [`BetterUrl::domain_segment`] and [`BetterUrl::set_domain_segment`].
    DomainSegment(isize),
    /// [`BetterUrl::subdomain_segment`] and [`BetterUrl::set_subdomain_segment`].
    SubdomainSegment(isize),
    /// [`BetterUrl::domain_suffix_segment`] and [`BetterUrl::set_domain_suffix_segment`].
    DomainSuffixSegment(isize),



    /// [`BetterUrl::domain`] and [`BetterUrl::set_domain`].
    Domain,
    /// [`BetterUrl::subdomain`] and [`BetterUrl::set_subdomain`].
    Subdomain,
    /// [`BetterUrl::reg_domain`] and [`BetterUrl::set_reg_domain`].
    RegDomain,
    /// [`BetterUrl::not_domain_suffix`] and [`BetterUrl::set_not_domain_suffix`].
    NotDomainSuffix,
    /// [`BetterUrl::domain_middle`] and [`BetterUrl::set_domain_middle`].
    DomainMiddle,
    /// [`BetterUrl::domain_suffix`] and [`BetterUrl::set_domain_suffix`].
    DomainSuffix,



    /// [`Url::port`] and [`BetterUrl::set_port`], but using strings.
    /// # Examples
    /// ```
    /// use url_cleaner_engine::types::*;
    ///
    /// let mut url = BetterUrl::parse("https://example.com").unwrap();
    ///
    /// assert_eq!(UrlPart::Port.get(&url), None);
    ///
    /// UrlPart::Port.set(&mut url, Some("443")).unwrap();
    /// assert_eq!(UrlPart::Port.get(&url), None);
    ///
    /// UrlPart::Port.set(&mut url, Some("80")).unwrap();
    /// assert_eq!(UrlPart::Port.get(&url), Some("80".into()));
    /// ```
    Port,



    /// [`Url::path`] and [`BetterUrl::set_path`].
    Path,
    /// [`BetterUrl::path_segment`] and [`BetterUrl::set_path_segment`].
    PathSegment(isize),
    /// [`BetterUrl::path_segment`] and [`BetterUrl::set_raw_path_segment`].
    RawPathSegment(isize),
    /// [`BetterUrl::path_segments_str`] and [`BetterUrl::set_path_segments_str`]
    PathSegments,
    /// [`BetterUrl::first_n_path_segments`] and [`BetterUrl::set_first_n_path_segments`].
    FirstNPathSegments(usize),
    /// [`BetterUrl::path_segments_after_first_n`] and [`BetterUrl::set_path_segments_after_first_n`]
    PathSegmentsAfterFirstN(usize),
    /// [`BetterUrl::last_n_path_segments`] and [`BetterUrl::set_last_n_path_segments`].
    LastNPathSegments(usize),
    /// [`BetterUrl::path_segments_before_last_n`] and [`BetterUrl::set_path_segments_before_last_n`].
    PathSegmentsBeforeLastN(usize),



    /// [`Url::query`] and [`BetterUrl::set_query`].
    Query,
    /// [`BetterUrl::query_param`] and [`BetterUrl::set_query_param`]
    QueryParam(QueryParamSelector),
    /// [`BetterUrl::raw_query_param`] and [`BetterUrl::set_raw_query_param`]
    RawQueryParam(QueryParamSelector),



    /// [`Url::fragment`] and [`BetterUrl::set_fragment`].
    Fragment,



    /// Uses [`BetterUrlPosition`]s to get multiple adjacent parts at the same time.
    /// # Errors
    /// Currently cannot set a UrlPart::PositionRange because it's complicated.
    /// # Examples
    /// ```
    /// use url_cleaner_engine::types::*;
    /// use url_cleaner_engine::glue::BetterUrlPosition;
    ///
    /// // Note that the `#1` at the end is the fragment, so just getting the query gives the wrong answer.
    /// let url = BetterUrl::parse("https://href.li/?https://example.com/?abc=123&def=456#1").unwrap();
    /// assert_eq!(
    ///     UrlPart::PositionRange {
    ///         start: BetterUrlPosition::BeforeQuery,
    ///         end: BetterUrlPosition::AfterFragment
    ///     }.get(&url),
    ///     Some("https://example.com/?abc=123&def=456#1".into())
    /// );
    /// ```
    PositionRange {
        /// The start of the range to get/set.
        start: BetterUrlPosition,
        /// The end of the range to get/set.
        end: BetterUrlPosition
    }
}

/// Allows getting and setting specific instances of a query parameter.
///
/// For example, it allows getting and setting the second `a` in `https://example.com?a=1&a=2`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Suitability)]
#[serde(remote = "Self")]
pub struct QueryParamSelector {
    /// The name of the query parameter to get.
    pub name: String,
    /// The index of matching query parameters to get.
    ///
    /// Defaults to `0`.
    #[serde(default, skip_serializing_if = "is_default")]
    pub index: usize
}

string_or_struct_magic!(QueryParamSelector);

impl FromStr for QueryParamSelector {
    type Err = std::convert::Infallible;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(s.into())
    }
}

impl From<&str> for QueryParamSelector {
    fn from(value: &str) -> Self {
        value.to_string().into()
    }
}

impl From<String> for QueryParamSelector {
    fn from(value: String) -> Self {
        Self {
            name: value,
            index: Default::default()
        }
    }
}

impl UrlPart {
    /// Gets the value.
    pub fn get<'a>(&self, url: &'a BetterUrl) -> Option<Cow<'a, str>> {
        debug!(UrlPart::get, self, url);
        Some(match self {
            Self::Debug(part) => {
                let ret = part.get(url);
                eprintln!("=== UrlPart::Debug ===\nUrlPart: {part:?}\nValue: {ret:?}");
                ret?
            },

            Self::Whole => Cow::Borrowed(url.as_str()),

            Self::Scheme   => Cow::Borrowed(url.scheme()),
            Self::Username => Cow::Borrowed(url.username()),
            Self::Password => Cow::Borrowed(url.password()?),

            Self::Host           => Cow::Borrowed(url.host_str()?),
            Self::NormalizedHost => Cow::Borrowed(url.normalized_host()?),

            Self::DomainSegment      (index) => Cow::Borrowed(url.domain_segment       (*index)?),
            Self::SubdomainSegment   (index) => Cow::Borrowed(url.subdomain_segment    (*index)?),
            Self::DomainSuffixSegment(index) => Cow::Borrowed(url.domain_suffix_segment(*index)?),

            Self::Domain          => Cow::Borrowed(url.domain           ()?),
            Self::Subdomain       => Cow::Borrowed(url.subdomain        ()?),
            Self::RegDomain       => Cow::Borrowed(url.reg_domain       ()?),
            Self::NotDomainSuffix => Cow::Borrowed(url.not_domain_suffix()?),
            Self::DomainMiddle    => Cow::Borrowed(url.domain_middle    ()?),
            Self::DomainSuffix    => Cow::Borrowed(url.domain_suffix    ()?),

            Self::Port => Cow::Owned(url.port()?.to_string()),

            Self::Path => Cow::Borrowed(url.path()),
            Self::PathSegment(index) => Cow::Borrowed(url.path_segment(*index)??),
            Self::RawPathSegment(index) => Cow::Borrowed(url.path_segment(*index)??),
            Self::PathSegments => Cow::Borrowed(url.path_segments_str()?),
            Self::FirstNPathSegments(n) => Cow::Borrowed(url.first_n_path_segments(*n)??),
            Self::PathSegmentsAfterFirstN(n) => Cow::Borrowed(url.path_segments_after_first_n(*n)??),
            Self::LastNPathSegments(n) => Cow::Borrowed(url.last_n_path_segments(*n)??),
            Self::PathSegmentsBeforeLastN(n) => Cow::Borrowed(url.path_segments_before_last_n(*n)??),

            Self::Query => Cow::Borrowed(url.query()?),
            Self::QueryParam   (QueryParamSelector {name, index}) => url.query_param(name, *index)???,
            Self::RawQueryParam(QueryParamSelector {name, index}) => Cow::Borrowed(url.raw_query_param(name, *index)???),

            Self::Fragment => Cow::Borrowed(url.fragment()?),

            Self::PositionRange {start, end} => Cow::Borrowed(&url[start.0..end.0])
        })
    }

    /// Sets the value.
    /// # Errors
    /// See each variant of [`Self`] for when each variant returns an error.
    pub fn set(&self, url: &mut BetterUrl, to: Option<&str>) -> Result<(), SetUrlPartError> {
        debug!(UrlPart::set, self, url, to);
        match (self, to) {
            (Self::Debug(part), _) => {
                let old = part.get(url).to_owned();
                eprintln!("=== UrlPart::Debug ===\nUrlPart: {part:?}\nOld value: {old:?}\nNew value: {to:?}");
                part.set(url, to)?;
            },

            (Self::Whole   , Some(to)) => *url=BetterUrl::parse(to)?,
            (Self::Whole   , None    ) => Err(SetUrlPartError::WholeCannotBeNone)?,

            (Self::Scheme  , Some(to)) => url.set_scheme(to)?,
            (Self::Scheme  , None    ) => Err(SetUrlPartError::SchemeCannotBeNone)?,

            (Self::Username, Some(to)) => url.set_username(to)?,
            (Self::Username, None    ) => Err(SetUrlPartError::UsernameCannotBeNone)?,

            (Self::Password, _       ) => url.set_password(to)?,

            (Self::Host , _) => url.set_host(to)?,
            (Self::NormalizedHost, _) => Err(SetUrlPartError::CannotSetNormalizedHost)?,

            (Self::DomainSegment      (n), _) => url.set_domain_segment       (*n, to)?,
            (Self::SubdomainSegment   (n), _) => url.set_subdomain_segment    (*n, to)?,
            (Self::DomainSuffixSegment(n), _) => url.set_domain_suffix_segment(*n, to)?,

            (Self::Domain         , _) => url.set_domain           (to)?,
            (Self::Subdomain      , _) => url.set_subdomain        (to)?,
            (Self::RegDomain      , _) => url.set_reg_domain       (to)?,
            (Self::NotDomainSuffix, _) => url.set_not_domain_suffix(to)?,
            (Self::DomainMiddle   , _) => url.set_domain_middle    (to)?,
            (Self::DomainSuffix   , _) => url.set_domain_suffix    (to)?,

            (Self::Port, _) => url.set_port(to.map(|x| x.parse().map_err(|_| SetUrlPartError::InvalidPort)).transpose()?)?,

            (Self::Path, Some(to)) => url.set_path(to),
            (Self::Path, None    ) => Err(SetUrlPartError::PathCannotBeNone)?,
            (Self::PathSegment(n), _) => url.set_path_segment(*n, to)?,
            (Self::RawPathSegment(n), _) => url.set_raw_path_segment(*n, to)?,
            (Self::PathSegments, Some(to)) => url.set_path_segments_str(to)?,
            (Self::PathSegments, None) => Err(SetUrlPartError::CannotSetPathSegmentsToNone)?,
            (Self::FirstNPathSegments(n), _) => url.set_first_n_path_segments(*n, to)?,
            (Self::PathSegmentsAfterFirstN(n), _) => url.set_path_segments_after_first_n(*n, to)?,
            (Self::LastNPathSegments(n), _) => url.set_last_n_path_segments(*n, to)?,
            (Self::PathSegmentsBeforeLastN(n), _) => url.set_path_segments_before_last_n(*n, to)?,

            (Self::Query, _) => url.set_query(to),
            (Self::QueryParam   (QueryParamSelector {name, index}), _) => url.set_query_param(name, *index, to.map(Some))?,
            (Self::RawQueryParam(QueryParamSelector {name, index}), _) => url.set_raw_query_param(name, *index, to.map(Some))?,

            (Self::Fragment, _) => url.set_fragment(to),

            (Self::PositionRange {..}, _) => Err(SetUrlPartError::CannotSetPositionRange)?
        }
        Ok(())
    }
}

/// The enum of errors [`UrlPart::set`] can return.
#[derive(Debug, Error)]
pub enum SetUrlPartError {
    /// Returned when a [`url::ParseError`] is encountered.
    #[error(transparent)] UrlParseError(#[from] url::ParseError),
    /// Returned when attempting to set [`UrlPart::Whole`] to [`None`].
    #[error("Attempted to set a whole URL to None.")]
    WholeCannotBeNone,

    // Pre-host stuff.

    /// Returned when attempting to set a URL's scheme to [`None`].
    #[error("Attempted to set a URL's scheme to None.")]
    SchemeCannotBeNone,
    /// Returned when a [`SetSchemeError`] is encountered.
    #[error(transparent)]
    SetSchemeError(#[from] SetSchemeError),
    /// Returned when attempting to set a URL's username to [`None`].
    #[error("Attempted to set a URL's username to None.")]
    UsernameCannotBeNone,
    /// Returned when a [`SetUsernameError`] is encountered.
    #[error(transparent)]
    SetUsernameError(#[from] SetUsernameError),
    /// Returned when a [`SetPasswordError`] is encountered.
    #[error(transparent)]
    SetPasswordError(#[from] SetPasswordError),

    // Host stuff.

    /// Returned when a [`SetHostError`] is encountered.
    #[error(transparent)] SetHostError(#[from] SetHostError),
    /// Returned when a [`SetIpHostError`] is encountered.
    #[error(transparent)] SetIpHostError(#[from] SetIpHostError),
    /// Returned when a [`SetSubdomainError)`] is encountered.
    #[error(transparent)] SetSubdomainError(#[from] SetSubdomainError),
    /// Returned when a [`SetDomainError)`] is encountered.
    #[error(transparent)] SetDomainError(#[from] SetDomainError),
    /// Returned when a [`SetNotDomainSuffixError)`] is encountered.
    #[error(transparent)] SetNotDomainSuffixError(#[from] SetNotDomainSuffixError),
    /// Returned when a [`SetDomainMiddleError)`] is encountered.
    #[error(transparent)] SetDomainMiddleError(#[from] SetDomainMiddleError),
    /// Returned when a [`SetRegDomainError)`] is encountered.
    #[error(transparent)] SetRegDomainError(#[from] SetRegDomainError),
    /// Returned when a [`SetDomainSuffixError)`] is encountered.
    #[error(transparent)] SetDomainSuffixError(#[from] SetDomainSuffixError),
    /// Returned when a [`SetDomainSegmentError)`] is encountered.
    #[error(transparent)] SetDomainSegmentError(#[from] SetDomainSegmentError),
    /// Returned when a [`SetSubdomainSegmentError)`] is encountered.
    #[error(transparent)] SetSubdomainSegmentError(#[from] SetSubdomainSegmentError),
    /// Returned when a [`SetDomainSuffixSegmentError)`] is encountered.
    #[error(transparent)] SetDomainSuffixSegmentError(#[from] SetDomainSuffixSegmentError),

    // Post-host stuff.

    /// Returned when attempting to set a port to a value that isn't a number between 0 and 65535 (inclusive).
    #[error("Attempted to set a port to a value that isn't a number between 0 and 65535 (inclusive).")]
    InvalidPort,
    /// Returned when a [`SetPortError`] is encountered.
    #[error(transparent)]
    SetPortError(#[from] SetPortError),

    /// Returned when a [`SetPathSegmentError`] is encountered.
    #[error(transparent)]
    SetPathSegmentError(#[from] SetPathSegmentError),
    /// Returned when a [`SetPathSegmentsStrError`] is encountered.
    #[error(transparent)]
    SetPathSegmentsStrError(#[from] SetPathSegmentsStrError),
    /// Returned when attempting to set a URL's path to [`None`].
    #[error("Attempted to set the URL's path to None.")]
    PathCannotBeNone,
    /// Returned when attempting to set [`UrlPart::PathSegments`] to [`None`].
    ///
    /// URLs with no path segments still have a path, therefore the operation is incoherent.
    #[error("Cannot set path segments to None, even for URLs with no path segments because they still have a path.")]
    CannotSetPathSegmentsToNone,
    /// Returned when a [`SetFirstNPathSegmentsError`] is encountered.
    #[error(transparent)]
    SetFirstNPathSegmentsError(#[from] SetFirstNPathSegmentsError),
    /// Returned when a [`SetPathSegmentsAfterFirstNError`] is encountered.
    #[error(transparent)]
    SetPathSegmentsAfterFirstNError(#[from] SetPathSegmentsAfterFirstNError),
    /// Returned when a [`SetLastNPathSegmentsError`] is encountered.
    #[error(transparent)]
    SetLastNPathSegmentsError(#[from] SetLastNPathSegmentsError),
    /// Returned when a [`SetPathSegmentsBeforeLastNError`] is encountered.
    #[error(transparent)]
    SetPathSegmentsBeforeLastNError(#[from] SetPathSegmentsBeforeLastNError),

    /// Returned when a [`SetQueryParamError)`] is encountered.
    #[error(transparent)]
    SetQueryParamError(#[from] SetQueryParamError),

    /// Returned when attempting to set a [`UrlPart::NormalizedHost`].
    #[error("Attempted to set a UrlPart::NormalizedHost.")]
    CannotSetNormalizedHost,
    /// Currently cannot set a UrlPart::PositionRange because it's complicated.
    #[error("Currently cannot set a UrlPart::PositionRange because it's complicated.")]
    CannotSetPositionRange
}