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
// ISC License (ISC)
//
// Copyright (c) 2016, Austin Hellyer <hello@austinhellyer.me>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
// RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
// CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// What is a gtLD?
//
// | Generic Top Level Domain (gTLD) is an internet domain name extension with
// | three or more characters. It is one of the categories of the top level
// | domain (TLD) in the Domain Name System (DNS) maintained by the Internet
// | Assigned Numbers Authority.
// |
// | - [icannwiki](http://icannwiki.com/GTLD)

pub mod gtlds;

// Re-export the `all` method since there's no reason to make userland go
// through the `gtlds` mod.
pub use gtlds::all;

/// The type of what a `Gtld` is. Each `Gtld` has a different type, with each
/// having a different purpose.
///
/// Wikipedia has a good summary of the differences of each:
///
/// <https://en.wikipedia.org/wiki/Top-level_domain#Types>
///
/// Derives `Clone`, `Debug`, and `PartialEq`.
#[derive(Clone, Debug, PartialEq)]
pub enum GtldKind {
    /// *ccTLD*: Two-letter domains established for countries or territories.
    CountryCode,
    /// *grTLD*: domains managed under official ICANN accredited registrars.
    GenericRestricted,
    /// *gTLD*: Top-level domains with three or more characters.
    Generic,
    /// *ARPA*: Consists of one domain, the Address and Routing Parameter Area.
    Infrastructure,
    /// Domains that are proposed and sponsored by private agencies or
    /// organizations.
    Sponsored,
    /// Domains installed under .test for testing purposes in the IDN
    /// development process.
    Test,
}

/// Representation of each `Gtld` with its three key pieces of data:
///
/// - *domain*: `str` of the domain name for this `Gtld`, such as `com` or `pw`.
/// - *kind*: `GtldKind` enum value of the type of `Gtld` that this is. For
///           technical purposes, this is renamed from `type` to `kind`.
/// - *organization*: `str` of the name of the organization that represents this
///                   `Gtld`.
///
/// Derives `Clone` and `Debug`.
#[derive(Clone, Debug)]
pub struct Gtld<'a> {
    pub domain: &'a str,
    pub kind: GtldKind,
    pub organization: &'a str,
}

/// Determines whether a domain exists within a `Gtld` defined by the
/// `gtld_data::all()` method.
///
/// # Examples
///
/// Check if a domain exists given a `str`:
///
/// ```rust
/// use gtld_data::domain_exists;
///
/// let exists = domain_exists("com");
/// ```
///
///
/// Check if a domain exists given a `String`:
///
/// ```rust
/// use gtld_data::domain_exists;
///
/// let exists = domain_exists(String::from("net"));
/// ```
pub fn domain_exists<S: Into<String>>(domain: S) -> bool {
    let mut exists = false;

    let domain_str: &str = &domain.into()[..];

    for gtld in all() {
        if gtld.domain == domain_str {
            exists = true;

            break;
        }
    }

    exists
}

/// Retrieves all `Gtld`s defined by the `gtld_data::all()` method given a
/// `GtldKind` enum value. For example, passing a `GtldKind::Generic` will
/// return all `Gtld`s that have a `kind` value of `GtldKind::Generic`.
///
/// # Examples
///
/// Retrieve all `Gtld`s with a `kind` of `GtldKind::Generic`:
///
/// ```rust
/// use gtld_data::{GtldKind, get_by_kind};
///
/// let generics = get_by_kind(GtldKind::Generic);
///
/// assert!(generics.len() > 0);
/// ```
///
/// Alternatively the same can be done for all values of `GtldKind`, for
/// example:
///
/// ```rust
/// use gtld_data::{GtldKind, get_by_kind};
///
/// let sponsored = get_by_kind(GtldKind::Sponsored);
///
/// assert!(sponsored.len() > 0);
/// ```
pub fn get_by_kind<'a>(kind: GtldKind) -> Vec<Gtld<'a>> {
    all().into_iter()
        .filter(|g| g.kind == kind)
        .collect()
}

/// Retrieves all `Gtld`s defined by the `gtld_data::all()` method with an
/// `organization` name equal to that of the `organization` passed.
///
/// # Examples
///
/// Retrieve all `Gtld`s represented by the `str`
/// "VeriSign Global Registry Services":
///
/// ```rust
/// use gtld_data::get_by_organization;
///
/// let organization = "VeriSign Global Registry Services";
/// let gtld_data = get_by_organization(organization);
///
/// assert!(gtld_data.len() > 0);
/// ```
///
/// Alternatively, pass a `String` of the organization name:
///
/// ```rust
/// use gtld_data::get_by_organization;
///
/// let organization = String::from("VeriSign Global Registry Services");
/// let gtld_data = get_by_organization(organization);
///
/// assert!(gtld_data.len() > 0);
/// ```
pub fn get_by_organization<'a, S: Into<String>>(organization: S)
    -> Vec<Gtld<'a>> {
    let org_str: &str = &organization.into()[..];

    all().into_iter()
        .filter(|g| g.organization == org_str)
        .collect()
}