crossmap_cli/argument.rs
1//! What a command-line argument names, decided from its text alone: standard input, a URL or a path. Nothing here
2//! reads anything, so the decision is the same wherever the reading is done (the native reader in `read_source`, or a
3//! host such as the npm package's Node shim).
4
5/// The argument that stands for standard input.
6pub const STDIN: &str = "-";
7
8/// The URL schemes the command reads, in the order the error for any other scheme lists them.
9pub const SUPPORTED_SCHEMES: [&str; 4] = ["http", "https", "data", "file"];
10
11/// Whether an argument names a URL: it starts with a scheme of two or more characters (a letter, then letters,
12/// digits, `+`, `-` or `.`, as RFC 3986 allows) and a colon. Two characters at least, so a Windows drive path such as
13/// `C:\x.json` stays a path; the character set admits `s3:` and `git+ssh:`, which are reported as unsupported rather
14/// than read as file names.
15pub fn is_url(argument: &str) -> bool {
16 scheme_of(argument).is_some()
17}
18
19/// The scheme an argument starts with, as written (not lower-cased), when it names a URL.
20pub fn scheme_of(argument: &str) -> Option<&str> {
21 let colon = argument.find(':')?;
22 let scheme = &argument[..colon];
23 let mut characters = scheme.chars();
24 let first = characters.next()?;
25 let valid = first.is_ascii_alphabetic()
26 && scheme.len() >= 2
27 && characters.all(|character| {
28 character.is_ascii_alphanumeric() || matches!(character, '+' | '.' | '-')
29 });
30 valid.then_some(scheme)
31}