link-bridge 0.2.5

A lightweight Rust library for creating URL redirects with short names that generate web pages redirecting to longer links on your website.
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
//! URL redirection system for generating short links and HTML redirect pages.
//!
//! This module provides the core functionality for creating URL redirects by:
//! - Validating and normalizing URL paths
//! - Generating unique short file names using base62 encoding
//! - Creating HTML redirect pages with meta refresh and JavaScript fallbacks
//! - Writing redirect files to the filesystem
//! - Managing a registry system to prevent duplicate redirects
//!
//! # Example Usage
//!
//! ```rust
//! use link_bridge::Redirector;
//! use std::fs;
//!
//! // Create a redirector for a URL path
//! let mut redirector = Redirector::new("api/v1/users").unwrap();
//!
//! // Optionally set a custom output directory
//! redirector.set_path("doc_test_output");
//!
//! // Write the redirect HTML file
//! let redirect_path = redirector.write_redirect().unwrap();
//!
//! // Clean up test files
//! fs::remove_dir_all("doc_test_output").ok();
//! ```

mod url_path;

use std::collections::HashMap;
use std::ffi::OsString;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::{fmt, fs};
use thiserror::Error;

use chrono::Utc;

use crate::redirector::url_path::UrlPath;

/// Errors that can occur during redirect operations.
#[derive(Debug, Error)]
pub enum RedirectorError {
    /// An I/O error occurred while creating or writing redirect files.
    ///
    /// This includes errors like permission denied, disk full, or invalid file paths.
    #[error("Failed to create redirect file")]
    FileCreationError(#[from] std::io::Error),

    /// The short link has not been generated (should not occur in normal usage).
    ///
    /// This error is included for completeness but should not happen since
    /// short links are automatically generated during `Redirector::new()`.
    #[error("Short link not found")]
    ShortLinkNotFound,

    /// The provided URL path is invalid.
    ///
    /// This occurs when the path contains invalid characters like query parameters (?),
    /// semicolons (;), or other forbidden characters.
    #[error("Invalid URL path: {0}")]
    InvalidUrlPath(#[from] url_path::UrlPathError),

    /// An error occurred while reading or writing the redirect registry.
    ///
    /// This occurs when the `registry.json` file cannot be read, parsed, or written.
    /// Common causes include corrupted JSON, permission issues, or filesystem errors.
    #[error("Failed to read redirect registry")]
    FailedToReadRegistry(#[from] serde_json::Error),
}

/// Manages URL redirection by generating short links and HTML redirect pages.
///
/// The `Redirector` creates HTML files that automatically redirect users to longer URLs
/// on your website. It handles the entire process from URL validation to file generation.
///
/// # Key Features
///
/// - **URL Validation**: Ensures paths contain only valid characters
/// - **Unique Naming**: Generates unique file names using base62 encoding and timestamps
/// - **HTML Generation**: Creates complete HTML pages with meta refresh and JavaScript fallbacks
/// - **File Management**: Handles directory creation and file writing operations
/// - **Registry System**: Maintains a JSON registry to track existing redirects and prevent duplicates
///
/// # Short Link Generation
///
/// Short file names are generated using:
/// - Current timestamp in milliseconds
/// - Sum of UTF-16 code units from the URL path
/// - Base62 encoding for compact, URL-safe names
/// - `.html` extension for web server compatibility
///
/// # Registry System
///
/// The redirector maintains a `registry.json` file in each output directory that tracks:
/// - Mapping from URL paths to their corresponding redirect files
/// - Prevents duplicate files for the same URL path
/// - Ensures consistent redirect behaviour across multiple calls
/// - Automatically created and updated when redirects are written
///
/// # HTML Output
///
/// Generated HTML files include:
/// - Meta refresh tag for immediate redirection
/// - JavaScript fallback for better compatibility
/// - User-friendly link for manual navigation
/// - Proper HTML5 structure and encoding
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Redirector {
    /// The validated and normalized URL path to redirect to.
    long_path: UrlPath,
    /// The generated short file name (including .html extension).
    short_file_name: OsString,
    /// The directory path where redirect HTML files will be stored.
    path: PathBuf,
}

impl Redirector {
    /// Creates a new `Redirector` instance for the specified URL path.
    ///
    /// Validates the provided path and automatically generates a unique short file name.
    /// The redirector is initialized with a default output directory of "s".
    ///
    /// # Arguments
    ///
    /// * `long_path` - The URL path to create a redirect for (e.g., "api/v1/users")
    ///
    /// # Returns
    ///
    /// * `Ok(Redirector)` - A configured redirector ready to generate redirect files
    /// * `Err(RedirectorError::InvalidUrlPath)` - If the path contains invalid characters
    ///
    /// # Examples
    ///
    /// ```rust
    /// use link_bridge::Redirector;
    ///
    /// // Valid paths
    /// let redirector1 = Redirector::new("api/v1").unwrap();
    /// let redirector2 = Redirector::new("/docs/getting-started/").unwrap();
    /// let redirector3 = Redirector::new("user-profile").unwrap();
    ///
    /// // Invalid paths (will return errors)
    /// assert!(Redirector::new("api?param=value").is_err()); // Query parameters
    /// assert!(Redirector::new("api;session=123").is_err());  // Semicolons
    /// assert!(Redirector::new("").is_err());                 // Empty string
    /// ```
    pub fn new<S: ToString>(long_path: S) -> Result<Self, RedirectorError> {
        let long_path = UrlPath::new(long_path.to_string())?;

        let short_file_name = Redirector::generate_short_file_name(&long_path);

        Ok(Redirector {
            long_path,
            short_file_name,
            path: PathBuf::from("s"),
        })
    }

    /// Generates a unique short file name based on timestamp and URL path content.
    ///
    /// Creates a unique identifier by combining the current timestamp with the URL path's
    /// UTF-16 character values, then encoding the result using base62 for a compact,
    /// URL-safe file name.
    ///
    /// # Algorithm
    ///
    /// 1. Get current timestamp in milliseconds
    /// 2. Sum all UTF-16 code units from the URL path
    /// 3. Add timestamp and UTF-16 sum together
    /// 4. Encode the result using base62 (0-9, A-Z, a-z)
    /// 5. Append ".html" extension
    ///
    /// # Returns
    ///
    /// An `OsString` containing the generated file name with `.html` extension.
    fn generate_short_file_name(long_path: &UrlPath) -> OsString {
        let name = base62::encode(
            Utc::now().timestamp_millis() as u64
                + long_path.encode_utf16().iter().sum::<u16>() as u64,
        );
        OsString::from(format!("{name}.html"))
    }

    /// Reports the short file name of the redirect HTML file.
    ///
    /// # Returns
    ///
    /// An `OsString` containing the generated file name with `.html` extension.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use link_bridge::Redirector;
    ///
    /// let redirector = Redirector::new("api/v1").unwrap();
    /// let name = redirector.short_file_name();
    /// assert!(name.to_string_lossy().ends_with(".html"));
    /// ```
    pub fn short_file_name(&self) -> OsString {
        self.short_file_name.clone()
    }

    /// Sets the output directory where redirect HTML files will be stored.
    ///
    /// By default, redirector uses "s" as the output directory. Use this method
    /// to specify a custom directory path. The directory will be created automatically
    /// when `write_redirect()` is called if it doesn't exist.
    ///
    /// # Arguments
    ///
    /// * `path` - A path-like value (String, &str, PathBuf, etc.) specifying the directory
    ///
    /// # Examples
    ///
    /// ```rust
    /// use link_bridge::Redirector;
    ///
    /// let mut redirector = Redirector::new("api/v1").unwrap();
    ///
    /// // Set various types of paths
    /// redirector.set_path("redirects");           // &str
    /// redirector.set_path("output/html".to_string()); // String
    /// redirector.set_path(std::path::PathBuf::from("custom/path")); // PathBuf
    /// ```
    pub fn set_path<P: Into<PathBuf>>(&mut self, path: P) {
        self.path = path.into();
    }

    /// Writes the redirect HTML file to the filesystem with registry support.
    ///
    /// Creates the output directory (if it doesn't exist) and generates a complete
    /// HTML redirect page that automatically redirects users to the target URL.
    /// The file name is the automatically generated short name with `.html` extension.
    ///
    /// # Registry System
    ///
    /// This method maintains a registry (`registry.json`) in the output directory to track
    /// existing redirects. If a redirect for the same URL path already exists, it returns
    /// the path to the existing file instead of creating a duplicate. This ensures:
    /// - No duplicate files for the same URL path
    /// - Consistent redirect behaviour across multiple calls
    /// - Efficient reuse of existing redirects
    ///
    /// # File Structure
    ///
    /// The generated HTML includes:
    /// - DOCTYPE and proper HTML5 structure
    /// - Meta charset and refresh tags for immediate redirection
    /// - JavaScript fallback for better browser compatibility
    /// - User-friendly fallback link for manual navigation
    ///
    /// # Returns
    ///
    /// * `Ok(String)` - The path to the created redirect file if successful
    /// * `Err(RedirectorError::FileCreationError)` - If file operations fail
    ///
    /// # Errors
    ///
    /// This method can return the following errors:
    ///
    /// ## `FileCreationError`
    /// - Permission denied (insufficient write permissions)
    /// - Disk full or insufficient space
    /// - Invalid characters in the file path
    /// - Parent directory cannot be created
    ///
    /// ## `FailedToReadRegistry`
    /// - Corrupted or invalid JSON in `registry.json`
    /// - Permission denied when reading/writing registry file
    /// - Registry file locked by another process
    ///
    /// # Examples
    ///
    /// ## Basic Usage
    ///
    /// ```rust
    /// use link_bridge::Redirector;
    /// use std::fs;
    ///
    /// let mut redirector = Redirector::new("api/v1/users").unwrap();
    /// redirector.set_path("doc_test_redirects");
    ///
    /// // First call creates a new redirect file and registry entry
    /// let redirect_path = redirector.write_redirect().unwrap();
    /// println!("Created redirect at: {}", redirect_path);
    ///
    /// // Clean up after the test
    /// fs::remove_dir_all("doc_test_redirects").ok();
    /// ```
    ///
    /// ## Registry behaviour
    ///
    /// ```rust
    /// use link_bridge::Redirector;
    /// use std::fs;
    ///
    /// let mut redirector1 = Redirector::new("api/v1/users").unwrap();
    /// redirector1.set_path("doc_test_registry");
    ///
    /// let mut redirector2 = Redirector::new("api/v1/users").unwrap();
    /// redirector2.set_path("doc_test_registry");
    ///
    /// // First call creates the file
    /// let path1 = redirector1.write_redirect().unwrap();
    ///
    /// // Second call returns the same path (no duplicate file created)
    /// let path2 = redirector2.write_redirect().unwrap();
    /// assert_eq!(path1, path2);
    ///
    /// // Clean up
    /// fs::remove_dir_all("doc_test_registry").ok();
    /// ```
    pub fn write_redirect(&self) -> Result<String, RedirectorError> {
        // create store directory if it doesn't exist
        if !Path::new(&self.path).exists() {
            fs::create_dir_all(&self.path)?;
        }
        const REDIRECT_REGISTRY: &str = "registry.json";
        let mut registry: HashMap<String, String> = HashMap::new();
        if Path::new(&self.path).join(REDIRECT_REGISTRY).exists() {
            registry = serde_json::from_reader::<_, HashMap<String, String>>(File::open(
                self.path.join(REDIRECT_REGISTRY),
            )?)?;
        }

        let file_path = self.path.join(&self.short_file_name);

        if let Some(existing_path) = registry.get(&self.long_path.to_string()) {
            // A link already exists for this path, return the existing file path
            Ok(existing_path.clone())
        } else {
            let mut file = File::create(&file_path)?;

            file.write_all(self.to_string().as_bytes())?;
            file.sync_all()?;

            registry.insert(
                self.long_path.to_string(),
                file_path.to_string_lossy().to_string(),
            );

            serde_json::to_writer_pretty(
                File::create(self.path.join(REDIRECT_REGISTRY))?,
                &registry,
            )?;

            Ok(file_path.to_string_lossy().to_string())
        }
    }
}

impl fmt::Display for Redirector {
    /// Generates the complete HTML redirect page content.
    ///
    /// Creates a standard HTML5 page that redirects to the target URL using
    /// multiple methods for maximum compatibility:
    /// - Meta refresh tag (works in all browsers)
    /// - JavaScript redirect (faster, works when JS is enabled)
    /// - Fallback link (for manual navigation if automatic redirect fails)
    ///
    /// The HTML follows web standards and includes proper accessibility features.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let target = self.long_path.to_string();
        write!(
            f,
            r#"
    <!DOCTYPE HTML>
    <html lang="en-US">

    <head>
        <meta charset="UTF-8">
        <meta http-equiv="refresh" content="0; url={target}">
        <script type="text/javascript">
            window.location.href = "{target}";
        </script>
        <title>Page Redirection</title>
    </head>

    <body>
        <!-- Note: don't tell people to `click` the link, just tell them that it is a link. -->
        If you are not redirected automatically, follow this <a href='{target}'>link to page</a>.
    </body>

    </html>
    "#
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::thread;
    use std::time::Duration;

    #[test]
    fn test_new_redirector() {
        let long_link = "/some/path";
        let redirector = Redirector::new(long_link).unwrap();

        assert_eq!(
            redirector.long_path,
            UrlPath::new(long_link.to_string()).unwrap()
        );
        assert!(!redirector.short_file_name.is_empty());
        assert_eq!(redirector.path, PathBuf::from("s"));
    }

    #[test]
    fn test_generate_short_link_unique() {
        let redirector1 = Redirector::new("/some/path").unwrap();
        thread::sleep(Duration::from_millis(1));
        let redirector2 = Redirector::new("/some/path").unwrap();

        assert_ne!(redirector1.short_file_name, redirector2.short_file_name);
    }

    #[test]
    fn test_set_path() {
        let mut redirector = Redirector::new("/some/path/").unwrap();

        redirector.set_path("custom_path");
        assert_eq!(redirector.path, PathBuf::from("custom_path"));

        redirector.set_path("another/path".to_string());
        assert_eq!(redirector.path, PathBuf::from("another/path"));
    }

    #[test]
    fn test_display_renders_html() {
        let redirector = Redirector::new("some/path").unwrap();
        let output = format!("{redirector}");

        assert!(output.contains("<!DOCTYPE HTML>"));
        assert!(output.contains("/some/path/"));
        assert!(output.contains("meta http-equiv=\"refresh\""));
        assert!(output.contains("window.location.href"));
    }

    #[test]
    fn test_display_with_complex_path() {
        let redirector = Redirector::new("api/v2/users").unwrap();

        let output = format!("{redirector}");

        assert!(output.contains("<!DOCTYPE HTML>"));
        assert!(output.contains("/api/v2/users/"));
        assert!(output.contains("meta http-equiv=\"refresh\""));
        assert!(output.contains("window.location.href"));
    }

    #[test]
    fn test_write_redirect_with_valid_path() {
        let test_dir = format!(
            "test_write_redirect_with_valid_path_{}",
            Utc::now().timestamp_nanos_opt().unwrap_or(0)
        );
        let mut redirector = Redirector::new("some/path").unwrap();
        redirector.set_path(&test_dir);

        let result = redirector.write_redirect();

        // Should succeed since short link is generated in new()
        assert!(result.is_ok());

        // Clean up
        fs::remove_dir_all(&test_dir).ok();
    }

    #[test]
    fn test_write_redirect_success() {
        let test_dir = format!(
            "test_write_redirect_success_{}",
            Utc::now().timestamp_nanos_opt().unwrap_or(0)
        );
        let mut redirector = Redirector::new("some/path").unwrap();
        redirector.set_path(&test_dir);

        let result = redirector.write_redirect();
        assert!(result.is_ok());

        let file_path = result.unwrap();

        assert!(Path::new(&file_path).exists());

        let content = fs::read_to_string(&file_path).unwrap();
        assert!(content.contains("<!DOCTYPE HTML>"));
        assert!(content.contains("meta http-equiv=\"refresh\""));
        assert!(content.contains("window.location.href"));
        assert!(content.contains("If you are not redirected automatically"));
        assert!(content.contains("/some/path/"));

        // Clean up
        fs::remove_dir_all(&test_dir).unwrap();
    }

    #[test]
    fn test_write_redirect_creates_directory() {
        let test_dir = format!(
            "test_write_redirect_creates_directory_{}",
            Utc::now().timestamp_nanos_opt().unwrap_or(0)
        );
        let subdir_path = format!("{test_dir}/subdir");
        let mut redirector = Redirector::new("some/path").unwrap();
        redirector.set_path(&subdir_path);

        assert!(!Path::new(&test_dir).exists());

        let result = redirector.write_redirect();
        assert!(result.is_ok());

        assert!(Path::new(&subdir_path).exists());

        let file_path = result.unwrap();
        assert!(Path::new(&file_path).exists());

        // Clean up
        fs::remove_dir_all(&test_dir).unwrap();
    }

    #[test]
    fn test_redirector_clone() {
        let mut redirector = Redirector::new("some/path").unwrap();
        redirector.set_path("custom");

        let cloned = redirector.clone();

        assert_eq!(redirector, cloned);
        assert_eq!(redirector.long_path, cloned.long_path);
        assert_eq!(redirector.short_file_name, cloned.short_file_name);
        assert_eq!(redirector.path, cloned.path);
    }

    #[test]
    fn test_redirector_default() {
        let redirector = Redirector::default();

        assert_eq!(redirector.long_path, UrlPath::default());
        assert_eq!(redirector.path, PathBuf::new());
        assert!(redirector.short_file_name.is_empty());
    }

    #[test]
    fn test_write_redirect_returns_correct_path() {
        let test_dir = format!(
            "test_write_redirect_returns_correct_path_{}",
            Utc::now().timestamp_nanos_opt().unwrap_or(0)
        );
        let mut redirector = Redirector::new("some/path").unwrap();
        redirector.set_path(&test_dir);

        let result = redirector.write_redirect();
        assert!(result.is_ok());

        let returned_path = result.unwrap();
        let expected_path = redirector.path.join(&redirector.short_file_name);

        assert_eq!(returned_path, expected_path.to_string_lossy());
        assert!(Path::new(&returned_path).exists());

        // Clean up
        fs::remove_dir_all(&test_dir).unwrap();
    }

    #[test]
    fn test_write_redirect_registry_functionality() {
        let test_dir = format!(
            "test_write_redirect_registry_functionality_{}",
            Utc::now().timestamp_nanos_opt().unwrap_or(0)
        );
        let mut redirector1 = Redirector::new("some/path").unwrap();
        redirector1.set_path(&test_dir);

        let mut redirector2 = Redirector::new("some/path").unwrap();
        redirector2.set_path(&test_dir);

        // First call should create a new file
        let result1 = redirector1.write_redirect();
        assert!(result1.is_ok());
        let path1 = result1.unwrap();

        // Second call with same path should return the existing file path
        let result2 = redirector2.write_redirect();
        assert!(result2.is_ok());
        let path2 = result2.unwrap();

        // Should return the same path
        assert_eq!(path1, path2);

        // Verify registry file exists
        let registry_path = PathBuf::from(&test_dir).join("registry.json");
        assert!(registry_path.exists());

        // Clean up
        fs::remove_dir_all(&test_dir).unwrap();
    }

    #[test]
    fn test_write_redirect_different_paths_different_files() {
        let test_dir = format!(
            "test_write_redirect_different_paths_different_files_{}",
            Utc::now().timestamp_nanos_opt().unwrap_or(0)
        );
        let mut redirector1 = Redirector::new("some/path").unwrap();
        redirector1.set_path(&test_dir);

        let mut redirector2 = Redirector::new("other/path").unwrap();
        redirector2.set_path(&test_dir);

        let result1 = redirector1.write_redirect();
        assert!(result1.is_ok());
        let path1 = result1.unwrap();

        let result2 = redirector2.write_redirect();
        assert!(result2.is_ok());
        let path2 = result2.unwrap();

        // Should create different files for different paths
        assert_ne!(path1, path2);
        assert!(Path::new(&path1).exists());
        assert!(Path::new(&path2).exists());

        // Clean up
        fs::remove_dir_all(&test_dir).unwrap();
    }

    #[test]
    fn test_new_redirector_error_handling() {
        // Test invalid path - single segment should be okay now
        let result = Redirector::new("api");
        assert!(result.is_ok());

        // Test empty path
        let result = Redirector::new("");
        assert!(result.is_err());

        // Test invalid characters
        let result = Redirector::new("api?param=value");
        assert!(result.is_err());
    }

    #[test]
    fn test_generate_short_link_different_paths() {
        let redirector1 = Redirector::new("api/v1").unwrap();
        let redirector2 = Redirector::new("api/v2").unwrap();

        // Different paths should generate different short links
        assert_ne!(redirector1.short_file_name, redirector2.short_file_name);
    }

    #[test]
    fn test_short_file_name_format() {
        let redirector = Redirector::new("some/path").unwrap();
        let file_name = redirector.short_file_name.to_string_lossy();

        // Should end with .html
        assert!(file_name.ends_with(".html"));
        // Should not be empty
        assert!(!file_name.is_empty());
    }

    #[test]
    fn test_debug_and_partialeq_traits() {
        let redirector1 = Redirector::new("some/path").unwrap();
        let redirector2 = redirector1.clone();

        // Test PartialEq
        assert_eq!(redirector1, redirector2);

        // Test Debug
        let debug_output = format!("{redirector1:?}");
        assert!(debug_output.contains("Redirector"));
    }
}