use core::convert::Infallible;
use crate::{FromGraphqlOrderBy, OrderDirection, TryFromGraphqlOrderBy};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ShotOrderBy {
At(OrderDirection),
Id(OrderDirection),
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct ShotSqliteOrder(Vec<&'static str>);
impl FromGraphqlOrderBy<Vec<ShotOrderBy>> for ShotSqliteOrder {
fn from_graphql_order_by(order: Vec<ShotOrderBy>) -> Self {
let mut levels = Vec::new();
for term in order {
match term {
ShotOrderBy::At(_) => {
levels.extend(["at_nanos", "at_pts", "at_timebase_num", "at_timebase_den"])
}
ShotOrderBy::Id(_) => levels.push("id"),
}
}
Self(levels)
}
}
#[test]
fn one_citizen_word_crosses_into_its_level_sequence() {
let crossed = ShotSqliteOrder::from_graphql_order_by(vec![
ShotOrderBy::At(OrderDirection::Asc),
ShotOrderBy::Id(OrderDirection::Asc),
]);
assert_eq!(
crossed.0,
[
"at_nanos",
"at_pts",
"at_timebase_num",
"at_timebase_den",
"id"
],
"the wire word count is the citizen's; the level count is the storage's"
);
}
#[test]
fn the_infallible_half_lifts_into_the_fallible_one() {
let crossed =
ShotSqliteOrder::try_from_graphql_order_by(vec![ShotOrderBy::Id(OrderDirection::Desc)])
.expect("a total crossing cannot fail");
assert_eq!(crossed.0, ["id"]);
}
const _: () = {
const fn assert_infallible<S, O>()
where
S: TryFromGraphqlOrderBy<O, Error = Infallible>,
{
}
assert_infallible::<ShotSqliteOrder, Vec<ShotOrderBy>>();
};