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
//! # sysuri
//!
//! A cross-platform Rust crate for registering custom URI schemes with the operating system.
//!
//! ## Features
//!
//! - Cross-platform support (Windows, macOS, Linux)
//! - Simple API for registering and unregistering URI schemes
//! - Callback-based URI handling
//! - No unsafe code
//! - Comprehensive error handling
//!
//! ## Quick Start
//!
//! ```no_run
//! use sysuri::{UriScheme, register};
//! use std::path::PathBuf;
//! use std::env;
//!
//! // Get the current executable path
//! let exe = env::current_exe().unwrap();
//!
//! // Create a URI scheme
//! let scheme = UriScheme::new(
//! "myapp",
//! "My Application Protocol",
//! exe
//! );
//!
//! // Register it with the OS
//! register(&scheme).unwrap();
//! ```
//!
//! ## URI Handler
//!
//! When your application is launched via a custom URI, the URI is typically passed
//! as a command-line argument. You can use the `parse_args` function to extract it:
//!
//! ```no_run
//! use sysuri::parse_args;
//!
//! fn main() {
//! if let Some(uri) = parse_args() {
//! println!("Opened with URI: {}", uri);
//! // Handle the URI...
//! } else {
//! println!("Normal application startup");
//! // Run normal application logic...
//! }
//! }
//! ```
pub use ;
pub use ;
use ;
use HashMap;
/// Global URI handler registry
static HANDLERS: Lazy =
new;
/// Register a URI scheme with the operating system
///
/// This function registers the URI scheme so that when a URI with this scheme
/// is opened (e.g., by clicking a link), your application will be launched.
///
/// # Arguments
///
/// * `scheme` - The URI scheme to register
///
/// # Returns
///
/// Returns `Ok(())` if the registration was successful, or an `Error` if it failed.
///
/// # Example
///
/// ```no_run
/// use sysuri::{UriScheme, register};
/// use std::env;
///
/// let exe = env::current_exe().unwrap();
/// let scheme = UriScheme::new("myapp", "My App", exe);
/// register(&scheme).unwrap();
/// ```
/// Unregister a URI scheme from the operating system
///
/// # Arguments
///
/// * `scheme` - The scheme name to unregister
///
/// # Example
///
/// ```no_run
/// use sysuri::unregister;
///
/// unregister("myapp").unwrap();
/// ```
/// Check if a URI scheme is already registered
///
/// # Arguments
///
/// * `scheme` - The scheme name to check
///
/// # Returns
///
/// Returns `Ok(true)` if the scheme is registered, `Ok(false)` if not,
/// or an `Error` if the check failed.
///
/// # Example
///
/// ```no_run
/// use sysuri::is_registered;
///
/// if is_registered("myapp").unwrap() {
/// println!("myapp:// is already registered");
/// }
/// ```
/// Register a URI handler callback
///
/// This function registers a callback that will be invoked when a URI
/// is passed to your application. Note that the actual URI is typically
/// passed as a command-line argument, so you'll need to call `handle_uri`
/// with the argument to trigger the callback.
///
/// # Arguments
///
/// * `scheme` - The scheme name to handle
/// * `handler` - The handler that will process URIs
///
/// # Example
///
/// ```no_run
/// use sysuri::{register_handler, FnHandler};
///
/// let handler = FnHandler::new(|uri| {
/// println!("Received URI: {}", uri);
/// });
///
/// register_handler("myapp", handler);
/// ```
/// Handle a URI by calling the registered handler
///
/// This function should be called with the URI that was passed to your
/// application (typically as a command-line argument).
///
/// # Arguments
///
/// * `uri` - The full URI to handle (e.g., "myapp://action/data")
///
/// # Returns
///
/// Returns `Ok(())` if a handler was found and called, or an `Error` if
/// no handler was registered for the scheme.
///
/// # Example
///
/// ```no_run
/// use sysuri::handle_uri;
///
/// handle_uri("myapp://open/file").unwrap();
/// ```
/// Parse command-line arguments to find a URI
///
/// This is a convenience function that looks through command-line arguments
/// for something that looks like a custom URI scheme.
///
/// # Returns
///
/// Returns the first argument that contains "://" or `None` if no URI is found.
///
/// # Example
///
/// ```no_run
/// use sysuri::parse_args;
///
/// if let Some(uri) = parse_args() {
/// println!("Opened with URI: {}", uri);
/// }
/// ```
/// Extract the scheme from a URI
///
/// # Arguments
///
/// * `uri` - The full URI (e.g., "myapp://action")
///
/// # Returns
///
/// Returns the scheme part (e.g., "myapp") or `None` if the URI is invalid.
///
/// # Example
///
/// ```
/// use sysuri::extract_scheme;
///
/// assert_eq!(extract_scheme("myapp://test"), Some("myapp"));
/// assert_eq!(extract_scheme("invalid"), None);
/// ```
/// Check if the application should run in URI handler mode
///
/// This is a convenience function that combines `parse_args` and `handle_uri`.
/// If a URI is found in the arguments, it will be handled and `true` is returned.
/// Otherwise, `false` is returned and the application should run normally.
///
/// # Returns
///
/// Returns `Ok(true)` if a URI was found and handled, `Ok(false)` if no URI
/// was found (normal startup), or an `Error` if handling failed.
///
/// # Example
///
/// ```no_run
/// use sysuri::{should_handle_uri, register_handler, FnHandler};
///
/// fn main() {
/// // Register handler first
/// register_handler("myapp", FnHandler::new(|uri| {
/// println!("Got URI: {}", uri);
/// }));
///
/// // Check if we should handle a URI
/// match should_handle_uri() {
/// Ok(true) => {
/// println!("Handled URI, exiting...");
/// return;
/// }
/// Ok(false) => {
/// println!("Normal startup");
/// }
/// Err(e) => {
/// eprintln!("Error handling URI: {}", e);
/// }
/// }
///
/// // Run normal application logic...
/// }
/// ```