domain_changer/
lib.rs

1#![doc = include_str!("../README.md")]
2
3//     Rust library that helps you change the domain of the link to another domain
4//      Copyright (C) 2022  TheAwiteb <awiteb@hotmail.com>
5//
6// This program is free software: you can redistribute it and/or modify it under
7// the terms of the GNU Affero General Public License as published by the Free
8// Software Foundation, version 3 of the License
9//
10// This program is distributed in the hope that it will be useful, but WITHOUT
11// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12// FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
13// details.
14//
15// You should have received a copy of the GNU Affero General Public License along
16// with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18pub mod types;
19use types::{Config, Domain};
20
21/// Parse [`String`] and return new [`String`] with new domains if any
22///
23/// # Example
24/// ```rust
25/// use domain_changer::parse_string;
26/// use domain_changer::types::Config;
27///
28/// let text: String = "Welcome to my youtube channel: https://www.youtube.com/channel/UCeRbJsc8cl7xBwT3jIxaAdg And my twitter is: twitter.com/Awiteb".to_string();
29/// let config: Config = Config::default();
30/// assert_eq!(parse_string(&config, text),
31///     "Welcome to my youtube channel: https://piped.kavin.rocks/channel/UCeRbJsc8cl7xBwT3jIxaAdg And my twitter is: https://nitter.net/Awiteb".to_string()
32///     );
33/// ```
34pub fn parse_string(config: &Config, text: String) -> String {
35    if !text.is_empty() {
36        text.split_ascii_whitespace()
37            .map(|word| {
38                for domain in config.domains.iter() {
39                    if let Some(mut url) = domain.contain(word, true) {
40                        if let Some(new_host) = domain.new.host_str() {
41                            // Error of `set_host` is `ParseError`, and we got the host
42                            // from the Url instance, ensuring that there is no problem
43                            url.set_host(Some(new_host)).unwrap();
44                            return url.as_str().to_string();
45                        }
46                    }
47                }
48                word.to_string()
49            })
50            .collect::<Vec<String>>()
51            .join(" ")
52    } else {
53        text
54    }
55}
56
57/// Returns all [`old`] domains in text if it is in [`config.domains`]
58///
59/// # Example
60/// ```rust
61/// use domain_changer::extract_old_domains;
62/// use domain_changer::types::{Config, Domain};
63///
64/// let config: Config = Config::default();
65/// let text: String = String::from(
66///     "Hi i hate youtube.com and https://twitter.com what about you?"
67/// );
68/// assert_eq!(
69///     extract_old_domains(&config, text),
70///     vec![
71///         &Domain::try_from(("https://youtube.com/", "https://piped.kavin.rocks/")).unwrap(),
72///         &Domain::try_from(("https://twitter.com/", "https://nitter.net/")).unwrap()
73///     ]
74/// );
75/// let empty_vec: Vec<&Domain> = Vec::new();
76/// assert_eq!(
77///     extract_old_domains(&config, String::from("Hello, World!")),
78///     empty_vec
79/// );
80/// ```
81///
82/// [`config.domains`]: struct.Config.html#structfield.domains
83/// [`old`]: struct.Domain.html#structfield.old
84pub fn extract_old_domains(config: &Config, text: String) -> Vec<&Domain> {
85    text.split_ascii_whitespace()
86        .filter_map(|word| config.contain(word, true))
87        .collect()
88}
89
90#[cfg(test)]
91mod tests {
92    use crate::{parse_string, types::Config};
93
94    #[test]
95    fn parse_string_test() {
96        let config: Config = Config::default();
97
98        assert_eq!(parse_string(&config, String::new()), String::new());
99        assert_eq!(
100            parse_string(&config, "Hello, world".to_owned()),
101            "Hello, world".to_owned()
102        );
103        assert_eq!(
104            parse_string(&config, "Hello https://randomdooomain.com".to_owned()),
105            "Hello https://randomdooomain.com".to_owned()
106        );
107        assert_eq!(
108            parse_string(&config, "hi, youtube.com/something".to_owned()),
109            "hi, https://piped.kavin.rocks/something".to_owned()
110        );
111        assert_eq!(
112            parse_string(&config, "hi, youtube.com".to_owned()),
113            "hi, https://piped.kavin.rocks/".to_owned()
114        )
115    }
116}