// Copyright (c) Kim Altintop // Copyright (c) The Radicle Team //! Known literal refname components. use super::*; /// A known literal [`RefStr`]. pub trait Lit: Sized + sealed::Sealed { /// The literal value. const SELF: Self; /// The literal refname string. const NAME: &'static RefStr; /// Converts a component into this literal when it matches. #[inline] fn from_component(component: &name::Component<'_>) -> Option { (component.as_ref() == Self::NAME).then_some(Self::SELF) } } impl From for &'static RefStr { #[inline] fn from(_: T) -> Self { T::NAME } } /// Seals the literal trait. mod sealed { /// Marker trait for literal implementors. pub trait Sealed {} } /// A known literal component. #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)] pub enum KnownLit { /// The `refs` component. Refs, /// The `heads` component. Heads, /// The `namespaces` component. Namespaces, /// The `remotes` component. Remotes, /// The `tags` component. Tags, /// The `notes` component. Notes, } impl KnownLit { /// Returns the known literal matching `component`. pub fn from_component(component: &name::Component<'_>) -> Option { let component: &RefStr = component.as_ref(); if component == Refs::NAME { Some(Self::Refs) } else if component == Heads::NAME { Some(Self::Heads) } else if component == Namespaces::NAME { Some(Self::Namespaces) } else if component == Remotes::NAME { Some(Self::Remotes) } else if component == Tags::NAME { Some(Self::Tags) } else if component == Notes::NAME { Some(Self::Notes) } else { None } } } impl From for name::Component<'static> { #[inline] fn from(lit: KnownLit) -> Self { match lit { KnownLit::Refs => Refs.into(), KnownLit::Heads => Heads.into(), KnownLit::Namespaces => Namespaces.into(), KnownLit::Remotes => Remotes.into(), KnownLit::Tags => Tags.into(), KnownLit::Notes => Notes.into(), } } } /// Either a known literal or any component. pub enum SomeLit<'a> { /// A known literal. Known(KnownLit), /// Any other component. Any(name::Component<'a>), } impl SomeLit<'_> { /// Returns the known literal, if this value is known. pub fn known(self) -> Option { match self { Self::Known(lit) => Some(lit), Self::Any(_) => None, } } } impl<'a> From> for SomeLit<'a> { #[inline] fn from(component: name::Component<'a>) -> Self { match KnownLit::from_component(&component) { Some(lit) => Self::Known(lit), None => Self::Any(component), } } } /// Literal tuple for `refs/heads/`. pub type RefsHeads = (Refs, Heads, T); /// Literal tuple for `refs/tags/`. pub type RefsTags = (Refs, Tags, T); /// Literal tuple for `refs/notes/`. pub type RefsNotes = (Refs, Notes, T); /// Literal tuple for `refs/remotes/`. pub type RefsRemotes = (Refs, Remotes, T); /// Literal tuple for `refs/namespaces//`. pub type RefsNamespaces<'a, T> = (Refs, Namespaces, T, Qualified<'a>); /// Creates a `refs/heads/` literal tuple. #[inline] pub fn refs_heads>(name: T) -> RefsHeads { (Refs, Heads, name) } /// Creates a `refs/tags/` literal tuple. #[inline] pub fn refs_tags>(name: T) -> RefsTags { (Refs, Tags, name) } /// Creates a `refs/notes/` literal tuple. #[inline] pub fn refs_notes>(name: T) -> RefsNotes { (Refs, Notes, name) } /// Creates a `refs/remotes/` literal tuple. #[inline] pub fn refs_remotes>(name: T) -> RefsRemotes { (Refs, Remotes, name) } /// Creates a `refs/namespaces//` literal tuple. #[inline] pub fn refs_namespaces<'a, 'b, T>(namespace: T, name: Qualified<'b>) -> RefsNamespaces<'b, T> where T: Into>, { (Refs, Namespaces, namespace, name) } /// The `refs` literal. #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)] pub struct Refs; impl Lit for Refs { const SELF: Self = Self; const NAME: &'static RefStr = name::REFS; } impl sealed::Sealed for Refs {} /// The `heads` literal. #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)] pub struct Heads; impl Lit for Heads { const SELF: Self = Self; const NAME: &'static RefStr = name::HEADS; } impl sealed::Sealed for Heads {} /// The `namespaces` literal. #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)] pub struct Namespaces; impl Lit for Namespaces { const SELF: Self = Self; const NAME: &'static RefStr = name::NAMESPACES; } impl sealed::Sealed for Namespaces {} /// The `remotes` literal. #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)] pub struct Remotes; impl Lit for Remotes { const SELF: Self = Self; const NAME: &'static RefStr = name::REMOTES; } impl sealed::Sealed for Remotes {} /// The `tags` literal. #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)] pub struct Tags; impl Lit for Tags { const SELF: Self = Self; const NAME: &'static RefStr = name::TAGS; } impl sealed::Sealed for Tags {} /// The `notes` literal. #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)] pub struct Notes; impl Lit for Notes { const SELF: Self = Self; const NAME: &'static RefStr = name::NOTES; } impl sealed::Sealed for Notes {}