url_cleaner_engine/tutorial/cleaner/
string_source.rs

1//! # [`StringSource`]
2//!
3//! When setting a URL part, you sometimes want to choose the value based on another part of the URL.
4//!
5//! Almost all components that take string inputs actually take string sources. This is usually invisible because JSON string literals deserialize to [`StringSource::String`].
6//!
7//! For example, deviantart used to put the username in the subdomain (`https://username.deviantart.com`) but now puts them in the first path segment (`https://deviantart.com/username`).
8//!
9//! To get the subdomain, [`StringSource::Part`] is used with [`UrlPart::Subdomain`]. Written in JSON as `{"Part": "Subdomain"}`.
10//!
11//! To then remove the subdomain, [`Action::SetSubdomain`] is given [`StringSource::None`], written in JSON as `null`.
12//!
13//! ```Json
14//! {"If": {
15//!   "if": {"All": [
16//!     {"RegDomainIs": "deviantart.com"},
17//!     {"Not": {"SubdomainIsOneOf": [null, "www"]}}
18//!   ]},
19//!   "then": {"All": [
20//!     {"InsertPathSegmentAt": {
21//!       "index": 0,
22//!       "value": {"Part": "Subdomain"}
23//!     }},
24//!     {"SetSubdomain": null}
25//!   ]}
26//! }}
27//! ```
28//!
29//! ## Complex construction
30//!
31//! String sources can be very complex. A common example of this is [`StringSource::Modified`].
32//!
33//! For example, some redirect URLs encode the redirect in Base64 in a path segment. To clean these, you must first get that path segment then Base64 decode it (which, in URL Cleaner Engine, defaults to the URL safe alphabet).
34//!
35//! ```Json
36//! {"Modified": {
37//!   "value": {"Part": {"PathSegment": 1}},
38//!   "modification": "Base64Decode"
39//! }}
40//! ```
41//!
42//! This might seem slightly inside out (it's `x.map(f)` instead of `f(x)`) but it was the easiest way to design this.
43//!
44//! For more information on what [`StringSource::Modified`] can do, see the tutorial on [string modifications](string_modification).
45//!
46//! Additionally, there's also [`StringSource::HttpRequest`] to do HTTP requests, [`StringSource::IfFlag`], [`StringSource::Map`], [`StringSource::ParamsMap`], [`StringSource::NamedPartitioning`], and so on for control flow, [`StringSource::Join`] for joining several other string sources, and so on.
47
48pub(crate) use super::*;