rustf 1.0.0

A simple MVC web framework inspired by Total.js
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
//! Global utilities module for RustF framework
//!
//! This module provides commonly used utility functions for web development,
//! accessible globally through the `U` struct (similar to Total.js Utils).
//!
//! # Usage
//! ```rust
//! use rustf::U;
//!
//! let guid = U::guid();
//! let encoded = U::encode("hello world");
//! let status_text = U::http_status(404);
//! ```

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;

/// Global utilities module - the main entry point for all utility functions
///
/// This module provides access to utility functions for common operations in web development.
/// It follows the Total.js convention of providing utilities through a global `U` namespace.
#[allow(non_snake_case)]
pub mod U {
    use super::*;
    use crate::error::Result;
    use serde_json::Value;
    use std::string::String as StdString;

    /// String utilities namespace
    pub mod String {
        pub use super::super::string::*;
    }

    /// Encoding utilities namespace
    pub mod Encoding {
        pub use super::super::encoding::*;
    }

    /// Parsing utilities namespace
    pub mod Parsing {
        pub use super::super::parsing::*;
    }

    /// Random utilities namespace
    pub mod Random {
        pub use super::super::random::*;
    }

    /// Crypto utilities namespace
    pub mod Crypto {
        pub use super::super::crypto::*;
    }

    /// HTTP utilities namespace
    pub mod Http {
        pub use super::super::http::*;
    }

    /// Object utilities namespace
    pub mod Object {
        pub use super::super::object::*;
    }

    /// Geo utilities namespace
    pub mod Geo {
        pub use super::super::geo::*;
    }

    // Random generation utilities

    /// Generate a new UUID/GUID
    ///
    /// # Example
    /// ```rust,ignore
    /// let id = U::guid();
    /// println!("Generated ID: {}", id);
    /// ```
    pub fn guid() -> StdString {
        random::guid()
    }

    /// Generate a random string of specified length
    ///
    /// # Arguments
    /// * `length` - The length of the random string to generate
    ///
    /// # Example
    /// ```rust,ignore
    /// let token = U::random_string(32);
    /// println!("Random token: {}", token);
    /// ```
    pub fn random_string(length: usize) -> StdString {
        random::string(length)
    }

    /// Generate a random number between min and max (inclusive)
    ///
    /// # Arguments
    /// * `min` - Minimum value (inclusive)
    /// * `max` - Maximum value (inclusive)
    ///
    /// # Example
    /// ```rust,ignore
    /// let num = U::random_number(1, 100);
    /// println!("Random number: {}", num);
    /// ```
    pub fn random_number(min: i64, max: i64) -> i64 {
        random::number(min, max)
    }

    // HTTP utilities

    /// Get HTTP status text for a status code
    ///
    /// # Arguments
    /// * `code` - HTTP status code
    ///
    /// # Example
    /// ```rust,ignore
    /// let status = U::http_status(404);
    /// assert_eq!(status, "Not Found");
    /// ```
    pub fn http_status(code: u16) -> &'static str {
        http::status_text(code)
    }

    /// Generate an ETag for content
    ///
    /// # Arguments
    /// * `content` - The content to generate ETag for
    ///
    /// # Example
    /// ```rust,ignore
    /// let etag = U::etag("Hello, World!");
    /// println!("ETag: {}", etag);
    /// ```
    pub fn etag(content: &str) -> StdString {
        http::etag(content)
    }

    /// Get MIME content type for file extension
    ///
    /// # Arguments
    /// * `extension` - File extension (with or without dot)
    ///
    /// # Example
    /// ```rust,ignore
    /// let mime = U::get_content_type("html");
    /// assert_eq!(mime, "text/html");
    /// ```
    pub fn get_content_type(extension: &str) -> &'static str {
        http::content_type(extension)
    }

    // Encoding/Decoding utilities

    /// URL encode a string
    ///
    /// # Arguments
    /// * `input` - String to encode
    ///
    /// # Example
    /// ```rust,ignore
    /// let encoded = U::encode("hello world");
    /// assert_eq!(encoded, "hello%20world");
    /// ```
    pub fn encode(input: &str) -> StdString {
        encoding::encode(input)
    }

    /// URL decode a string
    ///
    /// # Arguments
    /// * `input` - String to decode
    ///
    /// # Example
    /// ```rust,ignore
    /// let decoded = U::decode("hello%20world").unwrap();
    /// assert_eq!(decoded, "hello world");
    /// ```
    pub fn decode(input: &str) -> Result<StdString> {
        encoding::decode(input)
    }

    /// Base64 encode a string
    ///
    /// # Arguments
    /// * `input` - String to encode
    ///
    /// # Example
    /// ```rust,ignore
    /// let encoded = U::btoa("hello");
    /// println!("Base64: {}", encoded);
    /// ```
    pub fn btoa(input: &str) -> StdString {
        encoding::btoa(input)
    }

    /// Base64 decode a string
    ///
    /// # Arguments
    /// * `input` - Base64 string to decode
    ///
    /// # Example
    /// ```rust,ignore
    /// let decoded = U::atob("aGVsbG8=").unwrap();
    /// assert_eq!(decoded, "hello");
    /// ```
    pub fn atob(input: &str) -> Result<StdString> {
        encoding::atob(input)
    }

    // Parsing utilities

    /// Parse a string to boolean with default value
    ///
    /// # Arguments
    /// * `value` - String to parse
    /// * `default` - Default value if parsing fails
    ///
    /// # Example
    /// ```rust,ignore
    /// let result = U::parse_bool("true", false);
    /// assert_eq!(result, true);
    /// ```
    pub fn parse_bool(value: &str, default: bool) -> bool {
        parsing::bool(value, default)
    }

    /// Parse a string to integer with default value
    ///
    /// # Arguments
    /// * `value` - String to parse
    /// * `default` - Default value if parsing fails
    ///
    /// # Example
    /// ```rust,ignore
    /// let result = U::parse_int("123", 0);
    /// assert_eq!(result, 123);
    /// ```
    pub fn parse_int(value: &str, default: i64) -> i64 {
        parsing::int(value, default)
    }

    /// Parse a string to float with default value
    ///
    /// # Arguments
    /// * `value` - String to parse
    /// * `default` - Default value if parsing fails
    ///
    /// # Example
    /// ```rust,ignore
    /// let result = U::parse_float("123.45", 0.0);
    /// assert_eq!(result, 123.45);
    /// ```
    pub fn parse_float(value: &str, default: f64) -> f64 {
        parsing::float(value, default)
    }

    // String utilities

    /// Trim whitespace and clean up a string
    ///
    /// # Arguments
    /// * `input` - String to trim
    ///
    /// # Example
    /// ```rust,ignore
    /// let cleaned = U::trim("  hello world  ");
    /// assert_eq!(cleaned, "hello world");
    /// ```
    pub fn trim(input: &str) -> StdString {
        string::trim(input)
    }

    /// Extract keywords from text for search indexing
    ///
    /// # Arguments
    /// * `content` - Text content to process
    /// * `max_count` - Maximum number of keywords to return
    /// * `min_length` - Minimum keyword length
    ///
    /// # Example
    /// ```rust,ignore
    /// let keywords = U::keywords("This is a sample text", 10, 3);
    /// println!("Keywords: {:?}", keywords);
    /// ```
    pub fn keywords(content: &str, max_count: usize, min_length: usize) -> Vec<StdString> {
        string::keywords(content, max_count, min_length)
    }

    // Object manipulation utilities

    /// Get a nested property from a JSON object safely
    ///
    /// # Arguments
    /// * `obj` - JSON object to search in
    /// * `path` - Dot-separated path (e.g., "user.profile.name")
    ///
    /// # Example
    /// ```rust,ignore
    /// let data = json!({"user": {"name": "John"}});
    /// let name = U::get(&data, "user.name");
    /// ```
    pub fn get<'a>(obj: &'a Value, path: &str) -> Option<&'a Value> {
        object::get(obj, path)
    }

    /// Set a nested property in a JSON object
    ///
    /// # Arguments
    /// * `obj` - Mutable JSON object to modify
    /// * `path` - Dot-separated path (e.g., "user.profile.name")
    /// * `value` - Value to set
    ///
    /// # Example
    /// ```rust,ignore
    /// let mut data = json!({});
    /// U::set(&mut data, "user.name", json!("John"));
    /// ```
    pub fn set(obj: &mut Value, path: &str, value: Value) -> Result<()> {
        object::set(obj, path, value)
    }

    // Geographic utilities

    /// Calculate distance between two geographic points in kilometers
    ///
    /// # Arguments
    /// * `lat1` - Latitude of first point
    /// * `lon1` - Longitude of first point
    /// * `lat2` - Latitude of second point
    /// * `lon2` - Longitude of second point
    ///
    /// # Example
    /// ```rust,ignore
    /// let distance = U::distance(40.7128, -74.0060, 34.0522, -118.2437);
    /// println!("Distance: {:.2} km", distance);
    /// ```
    pub fn distance(lat1: f64, lon1: f64, lat2: f64, lon2: f64) -> f64 {
        geo::distance(lat1, lon1, lat2, lon2)
    }

    // Cryptographic hash utilities (direct access to most common ones)

    /// Generate MD5 hash of input string
    ///
    /// **Security Warning**: MD5 is cryptographically broken and should not be used
    /// for security purposes. Use SHA256 or higher for security applications.
    ///
    /// # Arguments
    /// * `input` - String to hash
    ///
    /// # Example
    /// ```rust,ignore
    /// let hash = U::md5("hello world");
    /// assert_eq!(hash, "5eb63bbbe01eeed093cb22bb8f5acdc3");
    /// ```
    pub fn md5(input: &str) -> StdString {
        crypto::md5(input)
    }

    /// Generate SHA256 hash of input string
    ///
    /// This is a secure hash function suitable for security applications.
    ///
    /// # Arguments
    /// * `input` - String to hash
    ///
    /// # Example
    /// ```rust,ignore
    /// let hash = U::sha256("hello world");
    /// assert_eq!(hash, "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9");
    /// ```
    pub fn sha256(input: &str) -> StdString {
        crypto::sha256(input)
    }

    // Pagination utilities

    /// Create a pagination object for building pagination UI
    ///
    /// This creates a pagination helper that calculates page numbers, URLs,
    /// and provides all necessary data for rendering pagination controls.
    ///
    /// # Arguments
    /// * `total` - Total number of items
    /// * `page` - Current page (1-based)
    /// * `per_page` - Items per page
    /// * `url_pattern` - URL pattern with {0} placeholder for page number
    ///
    /// # Example
    /// ```rust,ignore
    /// let total_users = Users::count().await?;
    /// let page = ctx.query_int_or("page", 1) as u32;
    /// let per_page = 20;
    ///
    /// let pagination = U::paginate(total_users, page, per_page, "/users?page={0}");
    ///
    /// // Use in template data
    /// ctx.view("users/list", json!({
    ///     "pagination": pagination.to_json()
    /// }))
    /// ```
    ///
    /// # Template Usage
    /// ```html
    /// @{if pagination.isPrev}
    ///     <a href="@{pagination.prev.url}">Previous</a>
    /// @{fi}
    ///
    /// @{foreach page in pagination.range}
    ///     @{if page.selected}
    ///         <span>@{page.page}</span>
    ///     @{else}
    ///         <a href="@{page.url}">@{page.page}</a>
    ///     @{fi}
    /// @{end}
    ///
    /// @{if pagination.isNext}
    ///     <a href="@{pagination.next.url}">Next</a>
    /// @{fi}
    /// ```
    pub fn paginate(
        total: i64,
        page: u32,
        per_page: u32,
        url_pattern: StdString,
    ) -> pagination::Pagination {
        pagination::Pagination::new(total, page, per_page, url_pattern)
    }

    /// Convert a `PagedResult` from database queries to a `Pagination` for template rendering
    ///
    /// This is a convenience wrapper around `pagination::pagination_from_paged_result()` that
    /// separates database concerns from template rendering concerns.
    ///
    /// # Arguments
    /// * `paged_result` - The paginated result from a database query (e.g., from `.get_paginated()`)
    /// * `url_pattern` - URL pattern with `{0}` placeholder for page number
    ///
    /// # Returns
    /// A `Pagination` object ready for template rendering
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use rustf::prelude::*;
    ///
    /// let result = Users::query()?
    ///     .where_eq("is_active", true)
    ///     .get_paginated(2, 20)
    ///     .await?;
    ///
    /// // Convert to Pagination for templates
    /// let pagination = U::pagination_from_paged_result(&result, "/users?page={0}");
    ///
    /// ctx.view("users/list", json!({
    ///     "users": result.rows,
    ///     "pagination": pagination.to_json()
    /// }))
    /// ```
    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)
    }
}

/// Alias for the global utilities module (for compatibility)
pub use U as Utils;

// Extended utilities are now available via the nested modules within U::
// This provides the exact U::ModuleName::function() syntax that was requested!
//
// Available extended modules:
// - U::String::to_slug(), U::String::to_camel_case(), U::String::title_case(), etc.
// - U::Encoding::html_encode(), U::Encoding::hex_encode(), U::Encoding::base64_url_encode(), etc.
// - U::Parsing::parse_duration(), U::Parsing::parse_size(), U::Parsing::parse_percentage(), etc.
// - U::Random::generate_guid_with_hyphens(), U::Random::generate_secure_token(), etc.
// - U::Crypto::hash_string(), U::Crypto::md5(), U::Crypto::sha1(), U::Crypto::sha256(), U::Crypto::sha512(), etc.
// - U::Http::is_success_status(), U::Http::get_extension_from_content_type(), etc.
// - U::Object::deep_merge(), U::Object::has_nested_property(), U::Object::flatten_object(), etc.
// - U::Geo::distance_miles(), U::Geo::in_bounds(), etc.
//
// Usage examples:
// ```rust
// use rustf::utils::U;
//
// // Basic utilities (direct functions)
// let id = U::guid();
// let encoded = U::encode("hello world");
//
// // Hash functions (direct access to most common)
// let md5_hash = U::md5("hello world");        // Warning: MD5 not secure
// let sha256_hash = U::sha256("hello world");  // Secure hash function
//
// // Extended utilities (nested modules)
// let slug = U::String::to_slug("Hello World!");
// let html = U::Encoding::html_encode("<script>alert('xss')</script>");
// let duration = U::Parsing::parse_duration("1h", 0);
//
// // All hash functions via Crypto namespace
// let sha1_hash = U::Crypto::sha1("data");       // Warning: SHA1 not secure
// let sha512_hash = U::Crypto::sha512("data");   // Very secure hash function
// let hash_from_bytes = U::Crypto::md5_bytes(&[1, 2, 3, 4, 5]);
// ```

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_u_struct_basic_operations() {
        // Test basic utilities are accessible
        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() {
        // Test string utilities via module access
        let slug = string::to_slug("Hello World!");
        assert_eq!(slug, "hello-world");

        let camel = string::to_camel_case("hello world");
        assert_eq!(camel, "helloWorld");

        // Test encoding utilities
        let html = encoding::html_encode("<p>Hello</p>");
        assert_eq!(html, "&lt;p&gt;Hello&lt;&#x2F;p&gt;");

        let hex = encoding::hex_encode(&[255, 0, 128]);
        assert_eq!(hex, "ff0080");

        // Test parsing utilities
        let duration = parsing::parse_duration("1h", 0);
        assert_eq!(duration, 3600);

        let size = parsing::parse_size("1KB", 0);
        assert_eq!(size, 1024);

        // Test crypto utilities
        let hash1 = crypto::hash_string("hello");
        let hash2 = crypto::hash_string("hello");
        assert_eq!(hash1, hash2);

        // Test random utilities
        let uuid = random::generate_guid_with_hyphens();
        assert_eq!(uuid.len(), 36);
        assert!(uuid.contains('-'));

        // Test HTTP utilities
        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");

        // Test object utilities
        let data = json!({"user": {"name": "John"}});
        assert!(object::has_nested_property(&data, "user.name"));
        assert!(!object::has_nested_property(&data, "user.email"));

        // Test geo utilities
        let distance_miles = geo::distance_miles(40.7, -74.0, 34.0, -118.2);
        assert!(distance_miles > 2000.0); // NY to LA is > 2000 miles

        assert!(geo::in_bounds(40.7, -74.0, 40.0, 41.0, -75.0, -73.0));
    }

    #[test]
    fn test_nested_module_syntax() {
        // Test the new U::ModuleName::function() syntax - exactly what you requested!

        // Basic utilities - direct access through U::
        let id = U::guid();
        assert!(!id.is_empty());
        assert_eq!(id.len(), 32); // UUID without hyphens

        let encoded = U::encode("hello world");
        assert_eq!(encoded, "hello%20world");

        // Extended utilities - nested module access through U::ModuleName::
        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");

        // Test encoding utilities via U::Encoding::
        let html = U::Encoding::html_encode("<p>Hello</p>");
        assert_eq!(html, "&lt;p&gt;Hello&lt;&#x2F;p&gt;");

        let hex = U::Encoding::hex_encode(&[255, 0, 128]);
        assert_eq!(hex, "ff0080");

        // Test parsing utilities via U::Parsing::
        let duration = U::Parsing::parse_duration("1h", 0);
        assert_eq!(duration, 3600);

        let size = U::Parsing::parse_size("1KB", 0);
        assert_eq!(size, 1024);

        // Test crypto utilities via U::Crypto::
        let hash1 = U::Crypto::hash_string("hello");
        let hash2 = U::Crypto::hash_string("hello");
        assert_eq!(hash1, hash2);

        // Test new hash functions via U::Crypto::
        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"
        );

        // Test direct access hash functions
        let md5_direct = U::md5("hello world");
        assert_eq!(md5_direct, "5eb63bbbe01eeed093cb22bb8f5acdc3");

        let sha256_direct = U::sha256("hello world");
        assert_eq!(
            sha256_direct,
            "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
        );

        // Test random utilities via U::Random::
        let uuid = U::Random::generate_guid_with_hyphens();
        assert_eq!(uuid.len(), 36);
        assert!(uuid.contains('-'));

        // Test HTTP utilities via U::Http::
        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");

        // Test object utilities via U::Object::
        let data = json!({"user": {"name": "John"}});
        assert!(U::Object::has_nested_property(&data, "user.name"));
        assert!(!U::Object::has_nested_property(&data, "user.email"));

        // Test geo utilities via U::Geo::
        let distance_miles = U::Geo::distance_miles(40.7, -74.0, 34.0, -118.2);
        assert!(distance_miles > 2000.0); // NY to LA is > 2000 miles

        assert!(U::Geo::in_bounds(40.7, -74.0, 40.0, 41.0, -75.0, -73.0));
    }
}