link_bridge/
lib.rs

1//! # link-bridge
2//!
3//! A lightweight Rust library for creating URL redirects with short names that generate
4//! web pages redirecting to longer links on your website.
5//!
6//! This crate provides a simple and efficient way to create HTML redirect pages that
7//! automatically forward users from short, memorable paths to longer URLs on your website.
8//! It's perfect for creating user-friendly shortcuts, maintaining backward compatibility
9//! after URL changes, or implementing a simple URL shortening system.
10//!
11//! ## Features
12//!
13//! - 🚀 **Fast and lightweight** - Minimal dependencies and efficient operation
14//! - 🔧 **Simple API** - Easy-to-use interface for creating redirects
15//! - 🎯 **URL validation** - Ensures paths contain only valid characters
16//! - 📁 **Automatic file management** - Creates directories and HTML files automatically
17//! - 📋 **Registry system** - Prevents duplicate redirects and ensures consistency
18//! - 🌐 **Standards compliant** - Generates proper HTML5 with multiple redirect methods
19//! - 🔒 **Safe** - Built with Rust's memory safety and error handling
20//!
21//! ## Quick Start
22//!
23//! Add this to your `Cargo.toml`:
24//!
25//! ```toml
26//! [dependencies]
27//! link-bridge = "0.2.3"
28//! ```
29//!
30//! ## Basic Usage
31//!
32//! ```rust
33//! use link_bridge::Redirector;
34//! use std::fs;
35//!
36//! // Create a redirector for a URL path
37//! let mut redirector = Redirector::new("api/v1/users").unwrap();
38//!
39//! // Optionally customize the output directory
40//! redirector.set_path("redirects");
41//!
42//! // Generate the redirect HTML file
43//! let redirect_path = redirector.write_redirect().unwrap();
44//!
45//! // Clean up for example
46//! fs::remove_dir_all("redirects").ok();
47//! ```
48//!
49//! This creates an HTML file that automatically redirects visitors from your short URL
50//! to the longer target path using multiple redirect methods for maximum compatibility.
51//!
52//! ## How It Works
53//!
54//! 1. **URL Validation**: Input paths are validated to ensure they contain only safe characters
55//! 2. **Unique Naming**: Short file names are generated using base62 encoding and timestamps
56//! 3. **Registry Check**: System checks if a redirect for this URL path already exists
57//! 4. **HTML Generation**: Complete HTML5 pages are created with multiple redirect methods:
58//!    - Meta refresh tag (universal browser support)
59//!    - JavaScript redirect (faster when JS is enabled)
60//!    - Manual fallback link (accessibility and fail-safe)
61//! 5. **File Management**: Directories are created automatically and files are written to disk
62//! 6. **Registry Update**: The registry is updated to track the new redirect mapping
63//!
64//! ## Generated HTML Structure
65//!
66//! The generated HTML files include:
67//!
68//! ```html
69//! <!DOCTYPE HTML>
70//! <html lang="en-US">
71//! <head>
72//!     <meta charset="UTF-8">
73//!     <meta http-equiv="refresh" content="0; url=/your/target/path/">
74//!     <script type="text/javascript">
75//!         window.location.href = "/your/target/path/";
76//!     </script>
77//!     <title>Page Redirection</title>
78//! </head>
79//! <body>
80//!     If you are not redirected automatically, follow this
81//!     <a href='/your/target/path/'>link</a>.
82//! </body>
83//! </html>
84//! ```
85//!
86//! ## Error Handling
87//!
88//! The library uses the [`RedirectorError`] type for comprehensive error handling:
89//!
90//! ```rust
91//! use link_bridge::{Redirector, RedirectorError};
92//!
93//! match Redirector::new("invalid?path") {
94//!     Ok(redirector) => println!("Success!"),
95//!     Err(RedirectorError::InvalidUrlPath(e)) => {
96//!         println!("Invalid path: {}", e);
97//!     }
98//!     Err(e) => println!("Other error: {}", e),
99//! }
100//! ```
101//!
102
103#![cfg_attr(docsrs, feature(doc_cfg))]
104#![warn(missing_docs)]
105#![cfg_attr(docsrs, feature(rustdoc_missing_doc_code_examples))]
106#![cfg_attr(docsrs, warn(rustdoc::invalid_codeblock_attributes))]
107
108mod redirector;
109
110pub use redirector::Redirector;
111pub use redirector::RedirectorError;