use super::TypeVisitor;
use crate::{Column, Describe, FieldName, RecordAddress, Table};
struct Db;
struct Other;
impl crate::Habitat for Db {
type Blueprint = Table;
}
impl crate::Habitat for Other {
type Blueprint = Table;
}
struct Media;
struct Clip;
struct Caption;
impl Describe<Db> for Media {
const BLUEPRINT: Table = Table::new(
RecordAddress::new(FieldName::new("media"), &[FieldName::new("id")]),
&[Column::new(FieldName::new("id"), "INTEGER", false)],
);
}
impl Describe<Db> for Clip {
const BLUEPRINT: Table = Table::new(
RecordAddress::new(FieldName::new("clip"), &[FieldName::new("id")]),
&[
Column::new(FieldName::new("id"), "INTEGER", false),
Column::new(FieldName::new("title"), "TEXT", true),
],
);
}
impl Describe<Db> for Caption {
const BLUEPRINT: Table = Table::new(
RecordAddress::new(FieldName::new("caption"), &[FieldName::new("id")]),
&[Column::new(FieldName::new("id"), "INTEGER", false)],
);
}
impl Describe<Other> for Caption {
const BLUEPRINT: Table = Table::new(
RecordAddress::new(FieldName::new("caption"), &[FieldName::new("id")]),
&[Column::new(FieldName::new("id"), "int8", false)],
);
}
struct App;
impl App {
fn type_iter<V>(v: &mut V) -> Result<(), V::Error>
where
V: TypeVisitor,
Media: Describe<V::Db>,
Clip: Describe<V::Db>,
Caption: Describe<V::Db>,
{
v.next_type::<Media>()?;
v.next_type::<Clip>()?;
v.next_type::<Caption>()
}
}
#[derive(Default)]
struct Seen(Vec<&'static str>);
impl TypeVisitor for Seen {
type Db = Db;
type Error = core::convert::Infallible;
fn next_type<E: Describe<Db>>(&mut self) -> Result<(), Self::Error> {
self.0.push(E::BLUEPRINT.address().table().as_str());
Ok(())
}
}
struct Refuses {
at: &'static str,
seen: Vec<&'static str>,
}
impl TypeVisitor for Refuses {
type Db = Db;
type Error = &'static str;
fn next_type<E: Describe<Db>>(&mut self) -> Result<(), &'static str> {
let table = E::BLUEPRINT.address().table().as_str();
if table == self.at {
return Err(table);
}
self.seen.push(table);
Ok(())
}
}
#[test]
fn the_walk_hands_over_every_node_once_in_declaration_order() {
let mut seen = Seen::default();
App::type_iter(&mut seen).expect("this visitor refuses nothing");
assert_eq!(seen.0, ["media", "clip", "caption"]);
}
#[test]
fn a_refusal_ends_the_walk_and_the_nodes_after_it_are_not_visited() {
let mut refuses = Refuses {
at: "clip",
seen: Vec::new(),
};
assert_eq!(App::type_iter(&mut refuses), Err("clip"));
assert_eq!(
refuses.seen,
["media"],
"the node before the refusal was visited and the one after it was not"
);
}
#[test]
fn one_walk_serves_a_visitor_built_for_either_database() {
#[derive(Default)]
struct Elsewhere(Vec<&'static str>);
impl TypeVisitor for Elsewhere {
type Db = Other;
type Error = core::convert::Infallible;
fn next_type<E: Describe<Other>>(&mut self) -> Result<(), Self::Error> {
self
.0
.push(E::BLUEPRINT.columns().flat().expect("a flat descriptor")[0].ty());
Ok(())
}
}
fn captions<V>(v: &mut V) -> Result<(), V::Error>
where
V: TypeVisitor,
Caption: Describe<V::Db>,
{
v.next_type::<Caption>()
}
let mut here = Seen::default();
captions(&mut here).expect("this visitor refuses nothing");
assert_eq!(here.0, ["caption"]);
let mut there = Elsewhere::default();
captions(&mut there).expect("this visitor refuses nothing");
assert_eq!(there.0, ["int8"], "the other database's own descriptor");
}