1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-01-03 17:38:50 +00:00
comunicapiv3/src/utils/string_utils.rs

53 lines
1.4 KiB
Rust

//! # String utilities
//!
//! This module contains utilities that can be used accross all the application
use std::str::FromStr;
use actix_web::http::Uri;
/// Escape an HTML string
///
/// Removes the HTML code included inside a string
///
/// ```
/// use comunic_server::utils::string_utils::remove_html_nodes;
///
/// let s1 = "<b>hello world</b>";
/// let res1 = remove_html_nodes(s1);
/// assert_eq!(res1, "&lt;b&gt;hello world&lt;/b&gt;");
/// ```
pub fn remove_html_nodes(input: &str) -> String {
input.replace("<", "&lt;")
.replace(">", "&gt;")
}
/// Check out whether a URL is valid or not
///
/// ```
/// use comunic_server::utils::string_utils::check_url;
///
/// let url1 = "http://communniquons.org/?url=some&arg2=myname#content3";
/// assert_eq!(check_url(url1), true);
///
/// let url2 = "h@ttp://communniquons.org/?url=some&arg2=myname#content3";
/// assert_eq!(check_url(url2), false);
/// ```
pub fn check_url(url: &str) -> bool {
Uri::from_str(url).is_ok()
}
/// Check a string before its insertion
///
/// Legacy function that might be completed / replaced in the future
///
/// ```
/// use comunic_server::utils::string_utils::check_string_before_insert;
///
/// assert_eq!(check_string_before_insert("s"), false);
/// assert_eq!(check_string_before_insert(" s"), false);
/// assert_eq!(check_string_before_insert("Hello world"), true);
/// ```
pub fn check_string_before_insert(s: &str) -> bool {
s.trim().len() > 3
}