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()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Generation<Domain> {
value: u64,
domain: std::marker::PhantomData<Domain>,
}
impl<Domain> Default for Generation<Domain> {
fn default() -> Self {
Self {
value: 0,
domain: std::marker::PhantomData,
}
}
}
impl<Domain: Copy + PartialEq> Generation<Domain> {
pub fn tick(&mut self) -> Self {
self.value = self
.value
.checked_add(1)
.expect("request generation exhausted");
*self
}
pub fn is_current(&self, other: Self) -> bool {
*self == other
}
}
impl<Domain> std::fmt::Display for Generation<Domain> {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.value.fmt(formatter)
}
}
impl<Domain> serde::Serialize for Generation<Domain> {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_u64(self.value)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn generation_guards_drop_stale_captures() {
let mut clock = Generation::<()>::default();
let spawn = clock.tick();
assert!(clock.is_current(spawn));
let _newer = clock.tick();
assert!(!clock.is_current(spawn));
}
}