use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use anyhow::{Context as _, Result};
use regex::Regex;
fn url_scheme_re() -> &'static Regex {
static RE: OnceLock<Regex> = OnceLock::new();
RE.get_or_init(|| Regex::new(r"^[A-Za-z][A-Za-z0-9+\-.]*://").expect("static regex"))
}
pub fn looks_like_url(s: &str) -> bool {
url_scheme_re().is_match(s)
}
#[derive(Debug, Clone)]
pub enum Input {
File(PathBuf),
Url(url::Url),
Raw(String),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
#[clap(rename_all = "lower")]
pub enum InputKind {
File,
Url,
Raw,
}
impl Input {
#[allow(dead_code)]
pub fn from_arg(raw: &str) -> Result<Self> {
Self::from_arg_as(raw, None)
}
pub fn from_arg_as(raw: &str, force: Option<InputKind>) -> Result<Self> {
match force {
Some(InputKind::Url) => {
let u = url::Url::parse(raw).with_context(|| format!("invalid URL: {raw}"))?;
Ok(Input::Url(u))
}
Some(InputKind::File) => {
let p = PathBuf::from(raw)
.canonicalize()
.with_context(|| format!("cannot resolve path: {raw}"))?;
Ok(Input::File(p))
}
Some(InputKind::Raw) => Ok(Input::Raw(raw.to_string())),
None => {
if looks_like_url(raw) {
let u = url::Url::parse(raw).with_context(|| format!("invalid URL: {raw}"))?;
return Ok(Input::Url(u));
}
match PathBuf::from(raw).canonicalize() {
Ok(p) => Ok(Input::File(p)),
Err(_) => Ok(Input::Raw(raw.to_string())),
}
}
}
}
pub fn match_string(&self) -> String {
match self {
Input::File(p) => crate::matcher::normalize_path(p),
Input::Url(u) => u.as_str().to_string(),
Input::Raw(s) => s.clone(),
}
}
pub fn display_string(&self) -> String {
match self {
Input::File(p) => crate::matcher::strip_verbatim(&p.to_string_lossy()),
Input::Url(u) => u.as_str().to_string(),
Input::Raw(s) => s.clone(),
}
}
pub fn as_file(&self) -> Option<&Path> {
match self {
Input::File(p) => Some(p),
_ => None,
}
}
#[allow(dead_code)]
pub fn as_url(&self) -> Option<&url::Url> {
match self {
Input::Url(u) => Some(u),
_ => None,
}
}
#[allow(dead_code)]
pub fn as_raw(&self) -> Option<&str> {
match self {
Input::Raw(s) => Some(s.as_str()),
_ => None,
}
}
pub fn kind_label(&self) -> &'static str {
match self {
Input::File(_) => "file",
Input::Url(_) => "url",
Input::Raw(_) => "raw",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn url_detection() {
assert!(looks_like_url("http://example.com"));
assert!(looks_like_url("https://example.com/path"));
assert!(looks_like_url("ftp://host"));
assert!(looks_like_url("file:///etc/hosts"));
assert!(!looks_like_url("C:\\Users\\x\\file.txt"));
assert!(!looks_like_url("/home/x/file.txt"));
assert!(!looks_like_url("./relative"));
}
#[test]
fn raw_fallback_when_not_url_or_file() {
let i = Input::from_arg("issue:1234").unwrap();
assert!(matches!(i, Input::Raw(_)));
assert_eq!(i.kind_label(), "raw");
assert_eq!(i.match_string(), "issue:1234");
}
#[test]
fn force_as_raw_skips_canonicalize() {
let i = Input::from_arg_as(".", Some(InputKind::Raw)).unwrap();
assert!(matches!(i, Input::Raw(_)));
assert_eq!(i.display_string(), ".");
}
#[test]
fn url_still_detected_first() {
let i = Input::from_arg("https://example.com/foo").unwrap();
assert!(matches!(i, Input::Url(_)));
}
}