use crate::{pricing::Pricing, query::QuerySpec, scope::ScopeExpr};
use serde::de::DeserializeOwned;
pub trait FleetGet: crate::sealed::Sealed + Send + Sync {
type WireResponse: DeserializeOwned;
type Response;
fn assemble(
wire: Self::WireResponse,
pagination: Option<crate::model::Pagination>,
) -> Self::Response;
fn required_scopes(&self) -> ScopeExpr;
fn pricing(&self) -> Pricing;
fn path(&self) -> String;
fn query(&self) -> QuerySpec;
}
pub trait VehicleEndpoint: FleetGet {
fn vin(&self) -> &str;
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
pricing::Pricing,
query::QuerySpec,
scope::{Scope, ScopeExpr},
};
struct DummyVehicles;
impl crate::sealed::Sealed for DummyVehicles {}
impl FleetGet for DummyVehicles {
type WireResponse = serde_json::Value;
type Response = serde_json::Value;
fn assemble(
wire: Self::WireResponse,
_pagination: Option<crate::model::Pagination>,
) -> Self::Response {
wire
}
fn required_scopes(&self) -> ScopeExpr {
ScopeExpr::One(Scope::VehicleDeviceData)
}
fn pricing(&self) -> Pricing {
Pricing::NotCharged
}
fn path(&self) -> String {
"/api/1/vehicles".into()
}
fn query(&self) -> QuerySpec {
QuerySpec::new()
}
}
#[test]
fn fleetget_contract() {
let d = DummyVehicles;
assert_eq!(d.path(), "/api/1/vehicles");
assert_eq!(d.pricing(), Pricing::NotCharged);
assert!(
d.required_scopes()
.is_satisfied_by(&[Scope::VehicleDeviceData])
);
}
#[test]
fn passthrough_assemble_ignores_pagination() {
let p = crate::model::Pagination {
previous: None,
next: Some(2),
current: Some(1),
per_page: Some(1),
count: Some(9),
pages: Some(9),
};
let out = <DummyVehicles as FleetGet>::assemble(serde_json::json!({"a":1}), Some(p));
assert_eq!(out, serde_json::json!({"a":1}));
}
#[test]
fn assemble_needs_no_request_instance() {
let out = <DummyVehicles as FleetGet>::assemble(serde_json::Value::Null, None);
assert!(out.is_null());
}
}