pub mod redb;
use uuid::Uuid;
use crate::types::{ObjectType, ResolvedAlias, ResolvedIdentity, TargetSource};
#[derive(Clone, Debug, PartialEq)]
pub struct CachedTarget {
pub id: Uuid,
pub simbad_oid: Option<i64>,
pub primary_designation: String,
pub common_name: Option<String>,
pub object_type: ObjectType,
pub otype_raw: String,
pub ra_deg: f64,
pub dec_deg: f64,
pub v_mag: Option<f64>,
pub source: TargetSource,
pub resolved_at: String,
pub aliases: Vec<ResolvedAlias>,
}
impl CachedTarget {
#[must_use]
pub fn to_identity(&self) -> ResolvedIdentity {
ResolvedIdentity {
simbad_oid: self.simbad_oid,
primary_designation: self.primary_designation.clone(),
common_name: self.common_name.clone(),
object_type: self.object_type,
otype_raw: self.otype_raw.clone(),
ra_deg: self.ra_deg,
dec_deg: self.dec_deg,
v_mag: self.v_mag,
aliases: self.aliases.clone(),
source: self.source,
}
}
pub fn position(&self) -> skymath::Result<skymath::Equatorial> {
skymath::Equatorial::j2000(
skymath::Angle::from_degrees(self.ra_deg),
skymath::Angle::from_degrees(self.dec_deg),
)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct SearchHit {
pub target: CachedTarget,
pub matched_alias: String,
pub rank: u8,
}
pub const RANK_EXACT: u8 = 0;
pub const RANK_PREFIX: u8 = 1;
pub const RANK_SUBSTRING: u8 = 2;
pub const RANK_FUZZY: u8 = 3;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum UpsertOutcome {
Inserted,
Updated,
SkippedUserOverride,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PendingState {
Pending,
Resolved,
Unresolved,
}
impl PendingState {
#[must_use]
pub fn as_wire(self) -> &'static str {
match self {
Self::Pending => "pending",
Self::Resolved => "resolved",
Self::Unresolved => "unresolved",
}
}
#[must_use]
pub fn from_wire(s: &str) -> Option<Self> {
match s {
"pending" => Some(Self::Pending),
"resolved" => Some(Self::Resolved),
"unresolved" => Some(Self::Unresolved),
_ => None,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct PendingItem {
pub id: String,
pub query: String,
pub state: PendingState,
pub attempts: i64,
pub target_id: Option<Uuid>,
}
#[derive(Debug, thiserror::Error)]
pub enum CacheError {
#[error("cache backend error: {0}")]
Backend(String),
#[error("invalid stored uuid '{0}': {1}")]
InvalidUuid(String, uuid::Error),
#[error("invalid stored enum value: '{0}'")]
InvalidEnum(String),
}
#[derive(Debug, thiserror::Error)]
pub enum QueueError {
#[error("queue backend error: {0}")]
Backend(String),
#[error("invalid stored uuid '{0}': {1}")]
InvalidUuid(String, uuid::Error),
#[error("invalid stored state: '{0}'")]
InvalidState(String),
}
#[async_trait::async_trait]
pub trait Cache: Send + Sync {
async fn get_by_id(&self, id: Uuid) -> Result<Option<CachedTarget>, CacheError>;
async fn get_by_simbad_oid(&self, oid: i64) -> Result<Option<CachedTarget>, CacheError>;
async fn get_by_normalized(&self, normalized: &str)
-> Result<Option<CachedTarget>, CacheError>;
async fn search(&self, query: &str, limit: usize) -> Result<Vec<SearchHit>, CacheError>;
async fn upsert(
&self,
identity: &ResolvedIdentity,
namespace: &Uuid,
) -> Result<(Uuid, UpsertOutcome), CacheError>;
async fn add_user_alias(&self, target_id: Uuid, alias: &str) -> Result<bool, CacheError>;
async fn remove_user_alias(&self, alias_id: &str) -> Result<bool, CacheError>;
async fn list(&self) -> Result<Vec<CachedTarget>, CacheError>;
}
#[async_trait::async_trait]
pub trait Queue: Send + Sync {
async fn enqueue(&self, id: &str, query: &str) -> Result<(), QueueError>;
async fn claim_pending(&self, n: usize) -> Result<Vec<PendingItem>, QueueError>;
async fn mark_resolved(&self, id: &str, target_id: Uuid) -> Result<(), QueueError>;
async fn mark_unresolved(&self, id: &str) -> Result<(), QueueError>;
async fn release(&self, id: &str) -> Result<(), QueueError>;
async fn get(&self, id: &str) -> Result<Option<PendingItem>, QueueError>;
async fn pending_count(&self) -> Result<usize, QueueError>;
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::AliasKind;
#[test]
fn pending_state_wire_round_trips() {
for st in [PendingState::Pending, PendingState::Resolved, PendingState::Unresolved] {
assert_eq!(PendingState::from_wire(st.as_wire()), Some(st));
}
assert_eq!(PendingState::Pending.as_wire(), "pending");
assert_eq!(PendingState::Resolved.as_wire(), "resolved");
assert_eq!(PendingState::Unresolved.as_wire(), "unresolved");
assert_eq!(PendingState::from_wire("bogus"), None);
assert_eq!(PendingState::from_wire(""), None);
}
#[test]
fn cached_target_to_identity_preserves_the_identity_fields() {
let target = CachedTarget {
id: Uuid::nil(),
simbad_oid: Some(1_575_544),
primary_designation: "M 31".to_owned(),
common_name: Some("Andromeda Galaxy".to_owned()),
object_type: ObjectType::Galaxy,
otype_raw: "G".to_owned(),
ra_deg: 10.684_708,
dec_deg: 41.268_75,
v_mag: Some(3.44),
source: TargetSource::Resolved,
resolved_at: "2026-07-11T00:00:00Z".to_owned(),
aliases: vec![ResolvedAlias::new("NGC 224", AliasKind::Designation)],
};
let identity = target.to_identity();
assert_eq!(identity.simbad_oid, Some(1_575_544));
assert_eq!(identity.primary_designation, "M 31");
assert_eq!(identity.common_name.as_deref(), Some("Andromeda Galaxy"));
assert_eq!(identity.object_type, ObjectType::Galaxy);
assert_eq!(identity.otype_raw, "G");
assert_eq!(identity.v_mag, Some(3.44));
assert!((identity.ra_deg - 10.684_708).abs() < f64::EPSILON);
assert_eq!(identity.source, TargetSource::Resolved);
assert_eq!(identity.aliases, target.aliases);
}
}