pub mod crypto;
pub mod encoding;
pub mod geo;
pub mod http;
pub mod http_date;
pub mod object;
pub mod pagination;
pub mod parsing;
pub mod random;
pub mod string;
#[allow(non_snake_case)]
pub mod U {
use super::*;
use crate::error::Result;
use serde_json::Value;
use std::string::String as StdString;
pub mod String {
pub use super::super::string::*;
}
pub mod Encoding {
pub use super::super::encoding::*;
}
pub mod Parsing {
pub use super::super::parsing::*;
}
pub mod Random {
pub use super::super::random::*;
}
pub mod Crypto {
pub use super::super::crypto::*;
}
pub mod Http {
pub use super::super::http::*;
}
pub mod Object {
pub use super::super::object::*;
}
pub mod Geo {
pub use super::super::geo::*;
}
pub fn guid() -> StdString {
random::guid()
}
pub fn random_string(length: usize) -> StdString {
random::string(length)
}
pub fn random_number(min: i64, max: i64) -> i64 {
random::number(min, max)
}
pub fn http_status(code: u16) -> &'static str {
http::status_text(code)
}
pub fn etag(content: &str) -> StdString {
http::etag(content)
}
pub fn get_content_type(extension: &str) -> &'static str {
http::content_type(extension)
}
pub fn encode(input: &str) -> StdString {
encoding::encode(input)
}
pub fn decode(input: &str) -> Result<StdString> {
encoding::decode(input)
}
pub fn btoa(input: &str) -> StdString {
encoding::btoa(input)
}
pub fn atob(input: &str) -> Result<StdString> {
encoding::atob(input)
}
pub fn parse_bool(value: &str, default: bool) -> bool {
parsing::bool(value, default)
}
pub fn parse_int(value: &str, default: i64) -> i64 {
parsing::int(value, default)
}
pub fn parse_float(value: &str, default: f64) -> f64 {
parsing::float(value, default)
}
pub fn trim(input: &str) -> StdString {
string::trim(input)
}
pub fn keywords(content: &str, max_count: usize, min_length: usize) -> Vec<StdString> {
string::keywords(content, max_count, min_length)
}
pub fn get<'a>(obj: &'a Value, path: &str) -> Option<&'a Value> {
object::get(obj, path)
}
pub fn set(obj: &mut Value, path: &str, value: Value) -> Result<()> {
object::set(obj, path, value)
}
pub fn distance(lat1: f64, lon1: f64, lat2: f64, lon2: f64) -> f64 {
geo::distance(lat1, lon1, lat2, lon2)
}
pub fn md5(input: &str) -> StdString {
crypto::md5(input)
}
pub fn sha256(input: &str) -> StdString {
crypto::sha256(input)
}
pub fn paginate(
total: i64,
page: u32,
per_page: u32,
url_pattern: StdString,
) -> pagination::Pagination {
pagination::Pagination::new(total, page, per_page, url_pattern)
}
pub fn pagination_from_paged_result<T>(
paged_result: &crate::models::PagedResult<T>,
url_pattern: StdString,
) -> pagination::Pagination {
pagination::pagination_from_paged_result(paged_result, url_pattern)
}
}
pub use U as Utils;
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn test_u_struct_basic_operations() {
let guid = U::guid();
assert!(!guid.is_empty());
let random_str = U::random_string(10);
assert_eq!(random_str.len(), 10);
let status = U::http_status(200);
assert_eq!(status, "OK");
let encoded = U::encode("hello world");
assert!(encoded.contains("%20"));
let bool_val = U::parse_bool("true", false);
assert_eq!(bool_val, true);
}
#[test]
fn test_u_struct_object_operations() {
let data = json!({"user": {"name": "John", "age": 25}});
let name = U::get(&data, "user.name");
assert_eq!(name, Some(&json!("John")));
let mut data = json!({});
U::set(&mut data, "user.name", json!("Jane")).unwrap();
assert_eq!(U::get(&data, "user.name"), Some(&json!("Jane")));
}
#[test]
fn test_extended_utilities_via_modules() {
let slug = string::to_slug("Hello World!");
assert_eq!(slug, "hello-world");
let camel = string::to_camel_case("hello world");
assert_eq!(camel, "helloWorld");
let html = encoding::html_encode("<p>Hello</p>");
assert_eq!(html, "<p>Hello</p>");
let hex = encoding::hex_encode(&[255, 0, 128]);
assert_eq!(hex, "ff0080");
let duration = parsing::parse_duration("1h", 0);
assert_eq!(duration, 3600);
let size = parsing::parse_size("1KB", 0);
assert_eq!(size, 1024);
let hash1 = crypto::hash_string("hello");
let hash2 = crypto::hash_string("hello");
assert_eq!(hash1, hash2);
let uuid = random::generate_guid_with_hyphens();
assert_eq!(uuid.len(), 36);
assert!(uuid.contains('-'));
assert!(http::is_success_status(200));
assert!(!http::is_success_status(404));
let ext = http::get_extension_from_content_type("application/json");
assert_eq!(ext, "json");
let data = json!({"user": {"name": "John"}});
assert!(object::has_nested_property(&data, "user.name"));
assert!(!object::has_nested_property(&data, "user.email"));
let distance_miles = geo::distance_miles(40.7, -74.0, 34.0, -118.2);
assert!(distance_miles > 2000.0);
assert!(geo::in_bounds(40.7, -74.0, 40.0, 41.0, -75.0, -73.0));
}
#[test]
fn test_nested_module_syntax() {
let id = U::guid();
assert!(!id.is_empty());
assert_eq!(id.len(), 32);
let encoded = U::encode("hello world");
assert_eq!(encoded, "hello%20world");
let slug = U::String::to_slug("Hello World!");
assert_eq!(slug, "hello-world");
let camel = U::String::to_camel_case("hello world");
assert_eq!(camel, "helloWorld");
let html = U::Encoding::html_encode("<p>Hello</p>");
assert_eq!(html, "<p>Hello</p>");
let hex = U::Encoding::hex_encode(&[255, 0, 128]);
assert_eq!(hex, "ff0080");
let duration = U::Parsing::parse_duration("1h", 0);
assert_eq!(duration, 3600);
let size = U::Parsing::parse_size("1KB", 0);
assert_eq!(size, 1024);
let hash1 = U::Crypto::hash_string("hello");
let hash2 = U::Crypto::hash_string("hello");
assert_eq!(hash1, hash2);
let md5_hash = U::Crypto::md5("hello world");
assert_eq!(md5_hash, "5eb63bbbe01eeed093cb22bb8f5acdc3");
let sha256_hash = U::Crypto::sha256("hello world");
assert_eq!(
sha256_hash,
"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
);
let md5_direct = U::md5("hello world");
assert_eq!(md5_direct, "5eb63bbbe01eeed093cb22bb8f5acdc3");
let sha256_direct = U::sha256("hello world");
assert_eq!(
sha256_direct,
"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
);
let uuid = U::Random::generate_guid_with_hyphens();
assert_eq!(uuid.len(), 36);
assert!(uuid.contains('-'));
assert!(U::Http::is_success_status(200));
assert!(!U::Http::is_success_status(404));
let ext = U::Http::get_extension_from_content_type("application/json");
assert_eq!(ext, "json");
let data = json!({"user": {"name": "John"}});
assert!(U::Object::has_nested_property(&data, "user.name"));
assert!(!U::Object::has_nested_property(&data, "user.email"));
let distance_miles = U::Geo::distance_miles(40.7, -74.0, 34.0, -118.2);
assert!(distance_miles > 2000.0);
assert!(U::Geo::in_bounds(40.7, -74.0, 40.0, 41.0, -75.0, -73.0));
}
}