| 1 | //! A native Rust library for Mozilla's Public Suffix List |
| 2 | |
| 3 | #![no_std] |
| 4 | #![forbid(unsafe_code)] |
| 5 | |
| 6 | mod list; |
| 7 | |
| 8 | #[cfg(feature = "helpers")] |
| 9 | use core::str; |
| 10 | |
| 11 | pub use psl_types::{Domain, Info, List as Psl, Suffix, Type}; |
| 12 | |
| 13 | /// A static public suffix list |
| 14 | pub struct List; |
| 15 | |
| 16 | impl Psl for List { |
| 17 | #[inline] |
| 18 | fn find<'a, T>(&self, labels: T) -> Info |
| 19 | where |
| 20 | T: Iterator<Item = &'a [u8]>, |
| 21 | { |
| 22 | list::lookup(labels) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | /// Get the public suffix of the domain |
| 27 | #[cfg(feature = "helpers")] |
| 28 | #[inline] |
| 29 | pub fn suffix(name: &[u8]) -> Option<Suffix<'_>> { |
| 30 | List.suffix(name) |
| 31 | } |
| 32 | |
| 33 | /// Get the public suffix of the domain |
| 34 | #[cfg(feature = "helpers")] |
| 35 | #[inline] |
| 36 | pub fn suffix_str(name: &str) -> Option<&str> { |
| 37 | let bytes = suffix(name.as_bytes())?.trim().as_bytes(); |
| 38 | str::from_utf8(bytes).ok() |
| 39 | } |
| 40 | |
| 41 | /// Get the registrable domain |
| 42 | #[cfg(feature = "helpers")] |
| 43 | #[inline] |
| 44 | pub fn domain(name: &[u8]) -> Option<Domain<'_>> { |
| 45 | List.domain(name) |
| 46 | } |
| 47 | |
| 48 | /// Get the registrable domain |
| 49 | #[cfg(feature = "helpers")] |
| 50 | #[inline] |
| 51 | pub fn domain_str(name: &str) -> Option<&str> { |
| 52 | let bytes = domain(name.as_bytes())?.trim().as_bytes(); |
| 53 | str::from_utf8(bytes).ok() |
| 54 | } |