use serde::{Deserialize, Serialize};
macro_rules! opaque_id {
($name:ident, $doc:literal) => {
#[doc = $doc]
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(transparent)]
pub struct $name(String);
impl $name {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl From<String> for $name {
fn from(value: String) -> Self {
$name(value)
}
}
impl From<&str> for $name {
fn from(value: &str) -> Self {
$name(value.to_string())
}
}
impl AsRef<str> for $name {
fn as_ref(&self) -> &str {
&self.0
}
}
};
}
opaque_id!(
RepoId,
"Opaque `\"group/project\"` repo identity (protocol: the UI never\n\
parses it; each backend owns its grammar)."
);
opaque_id!(
Sha,
"Opaque content id: MUST change when content changes (the cache\n\
is content-keyed and immutable). A commit sha on backends that\n\
have them; whatever the adapter guarantees otherwise."
);
opaque_id!(
GitRef,
"A resolvable revision name — branch, tag, or commit sha — as the\n\
backend interprets it (`repo/tree`'s `ref` param). `None` at a\n\
call site means the default branch; this type never encodes it."
);
opaque_id!(
RepoPath,
"An opaque repository-relative path, not a host filesystem path."
);
opaque_id!(
OrgId,
"An opaque organization or group identity, independent of its displayed caption."
);
opaque_id!(
OwnerId,
"An opaque repository namespace; it does not imply a personal account or organization."
);
impl Sha {
pub fn short(&self) -> String {
self.as_str().chars().take(7).collect()
}
}