sysuri
A cross-platform Rust crate for registering custom URI schemes with the operating system.
Features
- Cross-platform support: Windows, macOS, and Linux
- Simple API: Register and unregister URI schemes with just a few lines of code
- Callback-based handling: Define handlers for incoming URI requests
- No unsafe code: Built with safety in mind
- Well-documented: Comprehensive API documentation and examples
- Lightweight: Minimal dependencies
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Quick Start
Basic Registration
use ;
use env;
Handling URI Callbacks
When your application is launched via a custom URI, the URI is passed as a command-line argument:
use ;
Alternative: Manual URI Parsing
use parse_args;
Platform-Specific Behavior
Windows
On Windows, sysuri registers URI schemes in HKEY_CURRENT_USER\Software\Classes, which doesn't require administrator privileges.
Testing:
start myapp://test/path
macOS
On macOS, sysuri creates a minimal .app bundle in ~/Applications with the appropriate Info.plist configuration.
Testing:
Linux
On Linux, sysuri creates a .desktop file in ~/.local/share/applications/ and registers it using xdg-mime.
Testing:
Examples
The crate includes several examples demonstrating different usage patterns:
Basic Registration
Demonstrates simple URI scheme registration and verification.
Multiple URI Schemes
Shows how to register multiple related URI schemes (e.g., myapp://, myapp-dev://, myapp-secure://).
Callback Handlers
Demonstrates the callback-based URI handling system with URI parsing.
API Overview
Core Functions
register(scheme: &UriScheme) -> Result<()>- Register a URI schemeunregister(scheme: &str) -> Result<()>- Unregister a URI schemeis_registered(scheme: &str) -> Result<bool>- Check if a scheme is registered
Handler Functions
register_handler(scheme: &str, handler: impl UriHandler)- Register a URI handlerhandle_uri(uri: &str) -> Result<()>- Handle a URI using registered handlersshould_handle_uri() -> Result<bool>- Check and handle URI from command-line args
Utility Functions
parse_args() -> Option<String>- Extract URI from command-line argumentsextract_scheme(uri: &str) -> Option<&str>- Extract scheme from a URI
Types
UriScheme- Represents a custom URI scheme registrationUriHandler- Trait for implementing URI handlersFnHandler- Function-based URI handler implementation
Advanced Usage
Custom Handler Implementation
You can implement the UriHandler trait for more complex handling:
use UriHandler;
With Icon (Windows/Linux)
let scheme = new
.with_icon;
Conditional Registration
use ;
let scheme_name = "myapp";
if !is_registered? else
Error Handling
The crate uses a comprehensive error type covering common failure scenarios:
use ;
match register
URI Scheme Naming
According to RFC 3986, URI schemes must:
- Start with a letter
- Contain only letters, digits,
+,-, or. - Be case-insensitive (lowercase is recommended)
Valid examples: myapp, my-app, app.protocol, my+app
Invalid examples: my_app, 123app, my app
The crate validates schemes automatically and returns Error::InvalidScheme for invalid names.
Testing
Run the test suite:
Note: Some platform-specific tests may require appropriate permissions and can modify system settings. They use unique scheme names to avoid conflicts.
Security Considerations
- URI schemes are registered per-user (not system-wide) on all platforms
- The crate validates URI scheme names according to RFC 3986
- Executable paths are verified to exist before registration
- No elevation/admin privileges required
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Inspired by the need for a simple, cross-platform URI registration solution
- Built with safety and usability in mind
Changelog
Version 0.1.0 (Initial Release)
- Cross-platform URI scheme registration (Windows, macOS, Linux)
- Callback-based URI handling system
- Comprehensive examples and documentation
- Full test coverage