Skip to content

Commit

Permalink
Mapper::MovePart, j.mp, bitly.com, out.reddit.com
Browse files Browse the repository at this point in the history
  • Loading branch information
Scripter17 committed Aug 2, 2024
1 parent 29ac2d8 commit 1f95210
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 18 deletions.
32 changes: 17 additions & 15 deletions default-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"nerd.whatever.social", "z.opnxng.com"
],
"shortlink-hosts": [
"a.co", "abc7.la", "adobe.ly", "aje.io", "amzn.asia", "amzn.to", "apple.co", "bbc.in", "bit.ly", "bitly.com", "bityl.co", "blizz.ly",
"bloom.bg", "boxd.it", "buff.ly", "cbsn.ws", "cfl.re", "chn.ge", "chng.it", "cnb.cx", "cnn.it", "cos.lv", "cutt.ly", "db.tt",
"dcdr.me", "dis.gd", "dlvr.it", "e.emailalerts.cnn.com", "etsy.me", "fal.cn", "fb.me", "flip.it", "forms.gle", "g.co", "glo.bo",
"go.forbes.com", "go.nasa.gov", "gofund.me", "goo.gl", "goo.su", "gum.co", "hulu.tv", "ift.tt", "iwe.one", "k00.fr", "katy.to",
"kck.st", "l.leparisien.fr", "link.animaapp.com", "linkr.it", "lnk.to", "loom.ly", "mzl.la", "n.pr", "nas.cr", "ninten.do",
"a.co", "abc7.la", "adobe.ly", "aje.io", "amzn.asia", "amzn.to", "apple.co", "bbc.in", "bit.ly", "bitly.com", "bitly.com", "bityl.co",
"blizz.ly", "bloom.bg", "boxd.it", "buff.ly", "cbsn.ws", "cfl.re", "chn.ge", "chng.it", "cnb.cx", "cnn.it", "cos.lv", "cutt.ly",
"db.tt", "dcdr.me", "dis.gd", "dlvr.it", "e.emailalerts.cnn.com", "etsy.me", "fal.cn", "fb.me", "flip.it", "forms.gle", "g.co",
"glo.bo", "go.forbes.com", "go.nasa.gov", "gofund.me", "goo.gl", "goo.su", "gum.co", "hulu.tv", "ift.tt", "iwe.one", "j.mp", "k00.fr",
"katy.to", "kck.st", "l.leparisien.fr", "link.animaapp.com", "linkr.it", "lnk.to", "loom.ly", "mzl.la", "n.pr", "nas.cr", "ninten.do",
"ntdo.co.uk", "ny.ti", "nyer.cm", "nyti.ms", "nyto.ms", "on.forbes.com", "on.ft.com", "on.msnbc.com", "onl.bz", "onl.la", "onl.sc",
"operagx.gg", "orlo.uk", "ow.ly", "pin.it", "pixiv.me", "play.st", "py.pl", "rb.gy", "rblx.co", "rdbl.co", "redd.it", "reurl.cc",
"reut.rs", "rzr.to", "s.goodsmile.link", "shorturl.at", "spoti.fi", "spr.ly", "sqex.to", "t.co", "t.ly", "thecutio", "thr.cm",
Expand Down Expand Up @@ -85,7 +85,7 @@
"before": "http://bfy.tw/9ZSk",
"after": "https://google.com/search?q=How+does+the+US+government+work"
},
{"before": "https://https//abc.tumblr.com.tumblr.com", "after": "https://abc.tumblr.com"},
{"before": "https://https//abc.tumblr.com.tumblr.com", "after": "https://tumblr.com/abc/"},

{"before": "https://https//example.com/abc", "after": "https://example.com/abc"},
{"before": "https://https//example1.com/example2.com/abc", "after": "https://example2.com/abc"},
Expand Down Expand Up @@ -162,8 +162,9 @@
"allmylinks.com", "api.linkr.bio", "api.pinterest.com", "bfy.tw", "buymeacoff.ee", "buymeacoffee.com", "cach.me", "cash.app",
"deviantart.com", "duckduckgo.com", "duckduckgo.com", "duckduckgogg42xjoc72x3sjasowoarfbgcmvfimaftt6twagswzczad.onion", "gf.me",
"gofundme.com", "gofundme.com", "google.com", "gprivate.com", "href.li", "instagr.am", "instagram.com", "l.instagram.com",
"l.threads.com", "lmddgtfy.net", "lnk.bio", "pawoo.net", "pixiv.net", "preview.tinyurl.com", "sketchfab.com", "sketchfab.com",
"skfb.ly", "smarturl.it", "steamcommunity.com", "subscribestar.adult", "t.umblr.com", "tinyurl.com", "toyhou.se", "youtube.com"
"l.threads.com", "lmddgtfy.net", "lnk.bio", "out.reddit.com", "pawoo.net", "pixiv.net", "preview.tinyurl.com", "sketchfab.com",
"sketchfab.com", "skfb.ly", "smarturl.it", "steamcommunity.com", "subscribestar.adult", "t.umblr.com", "tinyurl.com", "toyhou.se",
"youtube.com"
]},
{"InSet": "lmgtfy-hosts"},
{"LengthIs": 4},
Expand Down Expand Up @@ -463,7 +464,8 @@
}}
}}
]}
}}
}},
"out.reddit.com": {"GetUrlFromQueryParam": "url"}
}
}
},
Expand Down Expand Up @@ -873,10 +875,7 @@
{"Not": {"PartIs": {"part": "Subdomain", "value": "www"}}},
{"Not": {"PartIs": {"part": "Subdomain", "value": ""}}}
]},
"mapper": {"All": [
{"CopyPart": {"from": "Subdomain", "to": {"BeforePathSegment": 0}}},
{"SetPart": {"part": "Subdomain", "value": null}}
]}
"mapper": {"MovePart": {"from": "Subdomain", "to": {"BeforePathSegment": 0}}}
}},
"tumblr.com": {"Rules": [
{
Expand All @@ -893,6 +892,10 @@
{
"condition": {"PartIs": {"part": {"PathSegment": 0}, "value": "login_required"}},
"mapper": {"SetPart": {"part": {"PathSegment": 0}, "value": null}}
},
{
"condition": {"PartMatches": {"part": "Subdomain", "matcher": {"Not": {"IsOneOf": ["at", "www"]}}}},
"mapper": {"MovePart": {"from": "Subdomain", "to": {"BeforePathSegment": 0}}}
}
]}
}
Expand Down Expand Up @@ -1103,8 +1106,7 @@
]},
"mapper": {"All": [
{"SetHost": "fandom.com"},
{"CopyPart": {"from": {"PathSegment": 0}, "to": "Subdomain"}},
{"SetPart": {"part": {"PathSegment": 0}, "value": null}}
{"MovePart": {"from": {"PathSegment": 0}, "to": "Subdomain"}}
]}
},
{
Expand Down
46 changes: 43 additions & 3 deletions src/types/rules/mappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,46 @@ pub enum Mapper {
},
/// Copies the part specified by `from` to the part specified by `to`.
/// # Errors
/// If the part specified by `from` is None and the part specified by `to` cannot be `None` (see [`Mapper::SetPart`]), returns the error [`UrlPartSetError::PartCannotBeNone`].
/// If the part specified by `from` is [`None`] and the part specified by `to` cannot be `None` (see [`Mapper::SetPart`]), returns the error [`UrlPartSetError::PartCannotBeNone`].
CopyPart {
/// The part to get the value from.
from: UrlPart,
/// The part to set to `from`'s value.
to: UrlPart
},
},
/// Effectively [`Self::CopyPart`] then [`Self::SetPart`] `from` to [`None`].
/// # Errors
/// If the part specified by `from` is [`None`] and the part specified by `to` cannot be `None` (see [`Mapper::SetPart`]), returns the error [`UrlPartSetError::PartCannotBeNone`].
///
/// If the call to `from`'s [`UrlPart::set`] returns an erorr, that error is returned.
/// # Examples
/// ```
/// # use url_cleaner::types::*;
/// # use url::Url;
/// let mut url = Url::parse("https://abc.example.com").unwrap();
/// let params = Default::default();
/// #[cfg(feature = "cache")]
/// let cache_handler = std::path::PathBuf::from("test-cache.sqlite").as_path().try_into().unwrap();
/// let mut job_state = url_cleaner::types::JobState {
/// url: &mut url,
/// params: &params,
/// vars: Default::default(),
/// #[cfg(feature = "cache")]
/// cache_handler: &cache_handler
/// };
///
/// Mapper::MovePart{from: UrlPart::Subdomain, to: UrlPart::BeforePathSegment(0)}.apply(&mut job_state).unwrap();
/// assert_eq!(job_state.url.as_str(), "https://example.com/abc/");
///
/// Mapper::MovePart{from: UrlPart::Scheme, to: UrlPart::BeforePathSegment(0)}.apply(&mut job_state).unwrap_err();
/// assert_eq!(job_state.url.as_str(), "https://example.com/abc/");
/// ```
MovePart {
/// The part to get the value from then set to [`None`].
from: UrlPart,
/// The part to set to `from`'s value.
to: UrlPart
},

// Miscellaneous.

Expand Down Expand Up @@ -686,6 +719,13 @@ impl Mapper {
Self::SetPart{part, value} => part.set(job_state.url, get_option_string!(value, job_state).as_deref())?, // The deref is needed for borrow checking reasons.
Self::ModifyPart{part, modification} => part.modify(modification, job_state)?,
Self::CopyPart{from, to} => to.set(job_state.url, from.get(job_state.url).map(|x| x.into_owned()).as_deref())?,
Self::MovePart{from, to} => {
let mut temp_url = job_state.url.clone();
let temp_url_ref = &mut temp_url;
to.set(temp_url_ref, from.get(temp_url_ref).map(|x| x.into_owned()).as_deref())?;
from.set(&mut temp_url, None)?;
*job_state.url = temp_url;
},

// Miscellaneous.

Expand Down Expand Up @@ -786,7 +826,7 @@ impl Mapper {
Self::RemoveQueryParams(_) | Self::AllowQueryParams(_) |
Self::RemoveQueryParamsMatching(_) | Self::AllowQueryParamsMatching(_) |
Self::GetUrlFromQueryParam(_) | Self::GetPathFromQueryParam(_) |
Self::SetHost(_) => true,
Self::SetHost(_) | Self::MovePart {..} => true,
#[cfg(feature = "http")]
Self::ExpandShortLink {..} => true
}
Expand Down

0 comments on commit 1f95210

Please sign in to comment.