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//! - 🌐 **Standards compliant** - Generates proper HTML5 with multiple redirect methods
18//! - 🔒 **Safe** - Built with Rust's memory safety and error handling
19//!
20//! ## Quick Start
21//!
22//! Add this to your `Cargo.toml`:
23//!
24//! ```toml
25//! [dependencies]
26//! link-bridge = "0.1"
27//! ```
28//!
29//! ## Basic Usage
30//!
31//! ```rust
32//! use link_bridge::Redirector;
33//! use std::fs;
34//!
35//! // Create a redirector for a URL path
36//! let mut redirector = Redirector::new("api/v1/users").unwrap();
37//!
38//! // Optionally customize the output directory
39//! redirector.set_path("redirects");
40//!
41//! // Generate the redirect HTML file
42//! redirector.write_redirects().unwrap();
43//!
44//! // Clean up for example
45//! fs::remove_dir_all("redirects").ok();
46//! ```
47//!
48//! This creates an HTML file that automatically redirects visitors from your short URL
49//! to the longer target path using multiple redirect methods for maximum compatibility.
50//!
51//! ## How It Works
52//!
53//! 1. **URL Validation**: Input paths are validated to ensure they contain only safe characters
54//! 2. **Unique Naming**: Short file names are generated using base62 encoding and timestamps
55//! 3. **HTML Generation**: Complete HTML5 pages are created with multiple redirect methods:
56//!    - Meta refresh tag (universal browser support)
57//!    - JavaScript redirect (faster when JS is enabled)
58//!    - Manual fallback link (accessibility and fail-safe)
59//! 4. **File Management**: Directories are created automatically and files are written to disk
60//!
61//! ## Generated HTML Structure
62//!
63//! The generated HTML files include:
64//!
65//! ```html
66//! <!DOCTYPE HTML>
67//! <html lang="en-US">
68//! <head>
69//!     <meta charset="UTF-8">
70//!     <meta http-equiv="refresh" content="0; url=/your/target/path/">
71//!     <script type="text/javascript">
72//!         window.location.href = "/your/target/path/";
73//!     </script>
74//!     <title>Page Redirection</title>
75//! </head>
76//! <body>
77//!     If you are not redirected automatically, follow this
78//!     <a href='/your/target/path/'>link</a>.
79//! </body>
80//! </html>
81//! ```
82//!
83//! ## Error Handling
84//!
85//! The library uses the [`RedirectorError`] type for comprehensive error handling:
86//!
87//! ```rust
88//! use link_bridge::{Redirector, RedirectorError};
89//!
90//! match Redirector::new("invalid?path") {
91//!     Ok(redirector) => println!("Success!"),
92//!     Err(RedirectorError::InvalidUrlPath(e)) => {
93//!         println!("Invalid path: {}", e);
94//!     }
95//!     Err(e) => println!("Other error: {}", e),
96//! }
97//! ```
98//!
99
100#![cfg_attr(docsrs, feature(doc_cfg))]
101#![warn(missing_docs)]
102#![cfg_attr(docsrs, feature(rustdoc_missing_doc_code_examples))]
103#![cfg_attr(docsrs, warn(rustdoc::invalid_codeblock_attributes))]
104
105mod redirector;
106
107pub use redirector::Redirector;
108pub use redirector::RedirectorError;