use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "source", rename_all = "snake_case")]
pub enum Reference {
Ollama {
namespace: String,
name: String,
tag: String,
},
HuggingFace {
owner: String,
repo: String,
revision: String,
quant: Option<String>,
},
Local { path: String },
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError(pub String);
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::error::Error for ParseError {}
fn bad(msg: impl Into<String>) -> ParseError {
ParseError(msg.into())
}
impl Reference {
pub fn parse(input: &str) -> Result<Reference, ParseError> {
let s = input.trim();
if s.is_empty() {
return Err(bad("empty model reference"));
}
if looks_like_path(s) {
return Ok(Reference::Local {
path: s.to_string(),
});
}
if let Some(rest) = strip_scheme(s, "hf") {
return parse_hf(rest);
}
if let Some(rest) = strip_scheme(s, "huggingface") {
return parse_hf(rest);
}
if let Some(rest) = strip_scheme(s, "ollama") {
return parse_ollama(rest);
}
if let Some(rest) = strip_scheme(s, "file") {
return Ok(Reference::Local {
path: rest.to_string(),
});
}
parse_ollama(s)
}
pub fn key(&self) -> String {
match self {
Reference::Ollama {
namespace,
name,
tag,
} => {
format!("ollama/{namespace}/{name}:{tag}")
}
Reference::HuggingFace {
owner,
repo,
revision,
quant,
} => {
let q = quant.as_deref().unwrap_or("auto");
format!("hf/{owner}/{repo}@{revision}#{q}")
}
Reference::Local { path } => format!("local/{path}"),
}
}
pub fn display_name(&self) -> String {
match self {
Reference::Ollama {
namespace,
name,
tag,
} if namespace == "library" => {
format!("{name}:{tag}")
}
Reference::Ollama {
namespace,
name,
tag,
} => format!("{namespace}/{name}:{tag}"),
Reference::HuggingFace {
owner, repo, quant, ..
} => match quant {
Some(q) => format!("{owner}/{repo}@{q}"),
None => format!("{owner}/{repo}"),
},
Reference::Local { path } => path
.rsplit('/')
.next()
.unwrap_or(path)
.trim_end_matches(".gguf")
.to_string(),
}
}
pub fn needs_download(&self) -> bool {
!matches!(self, Reference::Local { .. })
}
}
fn strip_scheme<'a>(s: &'a str, scheme: &str) -> Option<&'a str> {
let lower = s.to_ascii_lowercase();
let prefix = format!("{scheme}:");
if lower.starts_with(&format!("{prefix}//")) {
Some(&s[prefix.len() + 2..])
} else if lower.starts_with(&prefix) {
Some(&s[prefix.len()..])
} else {
None
}
}
fn looks_like_path(s: &str) -> bool {
s.starts_with('.')
|| s.starts_with('/')
|| s.starts_with('~')
|| s.ends_with(".gguf")
|| (s.len() > 2 && s.as_bytes()[1] == b':' && (s.contains('\\') || s.contains('/')))
}
fn parse_hf(rest: &str) -> Result<Reference, ParseError> {
if rest.is_empty() {
return Err(bad("hf: needs an owner/repo"));
}
let (head, revision) = match rest.split_once('#') {
Some((h, r)) if !r.is_empty() => (h, r.to_string()),
Some(_) => return Err(bad("empty revision after '#'")),
None => (rest, "main".to_string()),
};
let (path, quant) = match head.split_once('@') {
Some((p, q)) if !q.is_empty() => (p, Some(q.to_string())),
Some(_) => return Err(bad("empty quantization after '@'")),
None => (head, None),
};
let mut parts = path.split('/');
let owner = parts.next().unwrap_or("");
let repo = parts.next().unwrap_or("");
if owner.is_empty() || repo.is_empty() || parts.next().is_some() {
return Err(bad(format!(
"expected owner/repo, got \"{path}\" — a HuggingFace reference has exactly two parts"
)));
}
Ok(Reference::HuggingFace {
owner: owner.to_string(),
repo: repo.to_string(),
revision,
quant,
})
}
fn parse_ollama(rest: &str) -> Result<Reference, ParseError> {
if rest.is_empty() {
return Err(bad("ollama: needs a model name"));
}
let (path, tag) = match rest.rsplit_once(':') {
Some((p, t)) if !t.is_empty() && !t.contains('/') => (p, t.to_string()),
Some((_, "")) => return Err(bad("empty tag after ':'")),
_ => (rest, "latest".to_string()),
};
let mut parts = path.split('/');
let first = parts.next().unwrap_or("");
let second = parts.next();
if parts.next().is_some() {
return Err(bad(format!(
"expected name or namespace/name, got \"{path}\""
)));
}
let (namespace, name) = match second {
None => ("library".to_string(), first.to_string()),
Some(n) => (first.to_string(), n.to_string()),
};
if name.is_empty() || namespace.is_empty() {
return Err(bad(format!("incomplete model name: \"{rest}\"")));
}
Ok(Reference::Ollama {
namespace,
name,
tag,
})
}