Skip to main content

obd2_core/store/
mod.rs

1//! Storage traits for persisting vehicle and session data.
2//!
3//! obd2-core defines traits only. Implementations live in separate crates
4//! (e.g., `obd2-store-sqlite`) or are provided by consumers (e.g., HaulLogic
5//! might implement `VehicleStore` against its own PostgreSQL database).
6//!
7//! # Design Rationale
8//!
9//! Keeping storage out of the core library means:
10//! - Core has no SQLite/database dependency
11//! - Consumers choose their own storage backend
12//! - Mobile apps can use platform-native storage (Core Data, Room)
13//! - The library works without any persistence at all
14
15use async_trait::async_trait;
16use crate::error::Obd2Error;
17use crate::protocol::pid::Pid;
18use crate::protocol::enhanced::Reading;
19use crate::protocol::dtc::Dtc;
20use crate::vehicle::{VehicleProfile, ThresholdSet};
21
22/// Persist and retrieve vehicle profiles and threshold overrides.
23///
24/// A vehicle profile includes the VIN, decoded vehicle info, matched spec,
25/// and supported PID set. Threshold overrides allow per-VIN customization
26/// of alert limits beyond what the spec defines.
27#[async_trait]
28pub trait VehicleStore: Send + Sync {
29    /// Save or update a vehicle profile.
30    ///
31    /// If a profile with the same VIN already exists, it is updated.
32    async fn save_vehicle(&self, profile: &VehicleProfile) -> Result<(), Obd2Error>;
33
34    /// Retrieve a vehicle profile by VIN.
35    ///
36    /// Returns `None` if the VIN is not found in storage.
37    async fn get_vehicle(&self, vin: &str) -> Result<Option<VehicleProfile>, Obd2Error>;
38
39    /// Save per-VIN threshold overrides.
40    ///
41    /// These take priority over spec-defined thresholds (BR-5.1).
42    async fn save_thresholds(&self, vin: &str, thresholds: &ThresholdSet) -> Result<(), Obd2Error>;
43
44    /// Retrieve per-VIN threshold overrides.
45    ///
46    /// Returns `None` if no overrides exist for this VIN.
47    async fn get_thresholds(&self, vin: &str) -> Result<Option<ThresholdSet>, Obd2Error>;
48}
49
50/// Persist diagnostic session data for history and analysis.
51///
52/// Session data includes PID readings and DTC events captured during
53/// a diagnostic session. This enables historical trending, baseline
54/// learning, and post-session review.
55#[async_trait]
56pub trait SessionStore: Send + Sync {
57    /// Save a PID reading to the session history.
58    async fn save_reading(&self, vin: &str, pid: Pid, reading: &Reading) -> Result<(), Obd2Error>;
59
60    /// Save a DTC scan event to the session history.
61    async fn save_dtc_event(&self, vin: &str, dtcs: &[Dtc]) -> Result<(), Obd2Error>;
62}