use derive_more::AsRef;
use serde::{Deserialize, Serialize};
use tor_linkspec::{HasRelayIds, RelayIds};
#[cfg(test)]
use tor_llcrypto::pk;
use crate::GuardSetSelector;
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd, AsRef)]
pub(crate) struct FallbackId(pub(crate) RelayIds);
impl FallbackId {
pub(crate) fn from_relay_ids<T>(target: &T) -> Self
where
T: tor_linkspec::HasRelayIds + ?Sized,
{
Self(RelayIds::from_relay_ids(target))
}
}
impl HasRelayIds for FallbackId {
fn identity(
&self,
key_type: tor_linkspec::RelayIdType,
) -> Option<tor_linkspec::RelayIdRef<'_>> {
self.0.identity(key_type)
}
}
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Hash, Ord, PartialOrd, AsRef)]
#[serde(transparent)]
pub(crate) struct GuardId(pub(crate) RelayIds);
impl GuardId {
#[cfg(test)]
pub(crate) fn new(ed25519: pk::ed25519::Ed25519Identity, rsa: pk::rsa::RsaIdentity) -> Self {
Self(
RelayIds::builder()
.ed_identity(ed25519)
.rsa_identity(rsa)
.build()
.expect("Couldn't build RelayIds"),
)
}
pub(crate) fn from_relay_ids<T>(target: &T) -> Self
where
T: tor_linkspec::HasRelayIds + ?Sized,
{
Self(RelayIds::from_relay_ids(target))
}
}
impl HasRelayIds for GuardId {
fn identity(
&self,
key_type: tor_linkspec::RelayIdType,
) -> Option<tor_linkspec::RelayIdRef<'_>> {
self.0.identity(key_type)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub(crate) enum FirstHopIdInner {
Guard(GuardSetSelector, GuardId),
Fallback(FallbackId),
}
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct FirstHopId(pub(crate) FirstHopIdInner);
impl From<FallbackId> for FirstHopId {
fn from(id: FallbackId) -> Self {
Self(FirstHopIdInner::Fallback(id))
}
}
impl AsRef<RelayIds> for FirstHopId {
fn as_ref(&self) -> &RelayIds {
match &self.0 {
FirstHopIdInner::Guard(_, id) => id.as_ref(),
FirstHopIdInner::Fallback(id) => id.as_ref(),
}
}
}
impl tor_linkspec::HasRelayIds for FirstHopId {
fn identity(
&self,
key_type: tor_linkspec::RelayIdType,
) -> Option<tor_linkspec::RelayIdRef<'_>> {
self.as_ref().identity(key_type)
}
}
impl FirstHopId {
pub fn get_relay<'a>(&self, netdir: &'a tor_netdir::NetDir) -> Option<tor_netdir::Relay<'a>> {
netdir.by_ids(self)
}
pub(crate) fn in_sample(sample: GuardSetSelector, id: GuardId) -> Self {
Self(FirstHopIdInner::Guard(sample, id))
}
}