mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-12-29 06:58:50 +00:00
74 lines
2.0 KiB
Rust
74 lines
2.0 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, "<b>hello world</b>");
|
|
/// ```
|
|
pub fn remove_html_nodes(input: &str) -> String {
|
|
input.replace("<", "<")
|
|
.replace(">", ">")
|
|
}
|
|
|
|
/// 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
|
|
}
|
|
|
|
/// Check the validity of a YouTube ID
|
|
///
|
|
/// ```
|
|
/// use comunic_server::utils::string_utils::check_youtube_id;
|
|
///
|
|
/// assert_eq!(check_youtube_id("/ab/"), false);
|
|
/// assert_eq!(check_youtube_id("abxZ96C"), true);
|
|
/// assert_eq!(check_youtube_id("a6C"), false);
|
|
/// ```
|
|
pub fn check_youtube_id(id: &str) -> bool {
|
|
id.len() >= 5
|
|
&& !id.contains("/")
|
|
&& !id.contains("\\")
|
|
&& !id.contains("@")
|
|
&& !id.contains("&")
|
|
&& !id.contains("?")
|
|
&& !id.contains(".")
|
|
&& !id.contains("'")
|
|
&& !id.contains("\"")
|
|
} |