Debian Codegraph @

examples/tldextract.rs

raw symbols SCIP
1 use psl_typesrust-analyzer cargo psl-types 2.0.11 crate/::Listrust-analyzer cargo psl-types 2.0.11 List#;
2
3 pub trait TldExtractrust-analyzer cargo psl 2.1.223 TldExtract# {
4 fn extractrust-analyzer cargo psl 2.1.223 TldExtract#extract().<'a>(&self, host: &'a str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Partsrust-analyzer cargo psl 2.1.223 Parts#<'a>>;
5 }
6
7 impl<T: Listrust-analyzer cargo psl-types 2.0.11 List#> TldExtractrust-analyzer cargo psl 2.1.223 TldExtract# for T {
8 fn extractrust-analyzer cargo psl 2.1.223 impl#[T][TldExtract]extract().<'a>(&self, host: &'a str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Partsrust-analyzer cargo psl 2.1.223 Parts#<'a>> {
9 let host_len = host.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().();
10 let suffix_len = self.suffixrust-analyzer cargo psl-types 2.0.11 List#suffix().(host.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()..as_bytesrust-analyzer cargo psl-types 2.0.11 impl#[`Suffix<'a>`]as_bytes().().lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().();
11 let suffix = {
12 let offset = host_len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().suffix_len;
13 &host[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().offset..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().
14 };
15 let suffix_plus_dot = suffix_len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1;
16 let (subdomain, domain) = if host_len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().suffix_plus_dot {
17 match host.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]get().(..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#host_len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().suffix_plus_dot) {
18 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(prefix) => match prefix.rfindrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]rfind().('.') {
19 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(offset) => (prefix.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]get().(..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#offset), prefix.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]get().(offset rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#)),
20 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => (Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(prefix)),
21 },
22 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => (Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#),
23 }
24 } else {
25 (Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)
26 };
27 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Partsrust-analyzer cargo psl 2.1.223 Parts# {
28 suffixrust-analyzer cargo psl 2.1.223 Parts#suffix.,
29 domainrust-analyzer cargo psl 2.1.223 Parts#domain.,
30 subdomainrust-analyzer cargo psl 2.1.223 Parts#subdomain.,
31 })
32 }
33 }
34
35 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)]
36 pub struct Partsrust-analyzer cargo psl 2.1.223 Parts#<'a> {
37 pub suffixrust-analyzer cargo psl 2.1.223 Parts#suffix.: &'a str,
38 pub domainrust-analyzer cargo psl 2.1.223 Parts#domain.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&'a str>,
39 pub subdomainrust-analyzer cargo psl 2.1.223 Parts#subdomain.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&'a str>,
40 }
41
42 // This example is inspired by https://github.com/john-kurkowski/tldextract
43 // Unlike that project, we don't try to parse URLs though. That can easily
44 // be done by using the `url` crate and feeding the output of `Url::domain`
45 // to `TldExtract::extract`.
46 fn mainrust-analyzer cargo psl 2.1.223 main().() {
47 use pslrust-analyzer cargo psl 2.1.223 crate/::Listrust-analyzer cargo psl 2.1.223 List#;
48 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/;
49
50 let domain = match envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::argsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/args().().nthrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#nth().(1) {
51 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(name) => name,
52 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => {
53 eprintlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/eprintln!!("Usage: {} <domain name>", envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::argsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/args().().nthrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#nth().(0).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().());
54 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(1);
55 }
56 };
57
58 match Listrust-analyzer cargo psl 2.1.223 List#.extractrust-analyzer cargo psl 2.1.223 impl#[T][TldExtract]extract().(&domain) {
59 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(info) => printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(
60 "{} {} {}",
61 info.subdomainrust-analyzer cargo psl 2.1.223 Parts#subdomain..unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().("(None)"),
62 info.domainrust-analyzer cargo psl 2.1.223 Parts#domain..unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().("(None)"),
63 info.suffixrust-analyzer cargo psl 2.1.223 Parts#suffix.
64 ),
65 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => eprintlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/eprintln!!("`{}` is not domain name", domain),
66 }
67 }