#[derive(Debug, Clone, Copy)]
pub struct Field {
pub source: &'static str,
pub alias: &'static str,
}
#[must_use]
pub const fn field(source: &'static str, alias: &'static str) -> Field {
Field { source, alias }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
Database,
Table,
Column,
ForeignKey,
Index,
Check,
View,
Routine,
Trigger,
Event,
Sequence,
Partition,
Statistic,
DatabaseSpace,
ObjectSpace,
}
pub static ALL: &[Kind] = &[
Kind::Database,
Kind::Table,
Kind::Column,
Kind::ForeignKey,
Kind::Index,
Kind::Check,
Kind::View,
Kind::Routine,
Kind::Trigger,
Kind::Event,
Kind::Sequence,
Kind::Partition,
Kind::Statistic,
Kind::DatabaseSpace,
Kind::ObjectSpace,
];
pub static SEARCHABLE: &[Kind] = &[
Kind::Database,
Kind::Table,
Kind::Column,
Kind::View,
Kind::Routine,
Kind::Trigger,
Kind::Event,
Kind::Sequence,
Kind::Index,
];
impl Kind {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Database => "database",
Self::Table => "table",
Self::Column => "column",
Self::ForeignKey => "key",
Self::Index => "index",
Self::Check => "check",
Self::View => "view",
Self::Routine => "routine",
Self::Trigger => "trigger",
Self::Event => "event",
Self::Sequence => "sequence",
Self::Partition => "partition",
Self::Statistic => "statistic",
Self::DatabaseSpace => "database_space",
Self::ObjectSpace => "object_space",
}
}
#[must_use]
pub fn parse(name: &str) -> Option<Self> {
ALL.iter().copied().find(|kind| kind.as_str() == name)
}
}
#[derive(Debug, Clone, Copy)]
pub struct Descriptor {
pub kind: Kind,
pub source: &'static str,
pub short: &'static [Field],
pub extra: &'static [Field],
pub database_column: Option<&'static str>,
pub table_column: Option<&'static str>,
pub name_column: &'static str,
pub predicate: Option<&'static str>,
pub group_by: Option<&'static str>,
pub order_by: &'static str,
}
pub const PLAIN: Descriptor = Descriptor {
kind: Kind::Table,
source: "",
short: &[],
extra: &[],
database_column: None,
table_column: None,
name_column: "",
predicate: None,
group_by: None,
order_by: "",
};
#[must_use]
pub fn fields(descriptor: &Descriptor, detailed: bool) -> Vec<Field> {
let extra = if detailed { descriptor.extra } else { &[] };
descriptor.short.iter().chain(extra).copied().collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_kind_answers_to_its_own_name() {
for kind in ALL.iter().copied() {
assert_eq!(Kind::parse(kind.as_str()), Some(kind));
}
assert_eq!(ALL.len(), 15, "the published set has a fixed size");
assert_eq!(Kind::parse("not_a_kind"), None);
}
#[test]
fn every_searchable_kind_is_a_published_kind() {
for kind in SEARCHABLE {
assert!(ALL.contains(kind), "{}", kind.as_str());
}
}
#[test]
fn the_detailed_form_is_the_short_one_plus_its_extras() {
static SHORT: &[Field] = &[field("A", "a")];
static EXTRA: &[Field] = &[field("B", "b")];
let descriptor = Descriptor {
short: SHORT,
extra: EXTRA,
..PLAIN
};
let short = fields(&descriptor, false);
let detailed = fields(&descriptor, true);
assert_eq!(short.len(), 1);
assert_eq!(detailed.len(), 2);
assert_eq!(short[0].alias, detailed[0].alias);
assert_eq!(detailed[1].alias, "b");
}
#[test]
fn a_kind_that_states_nothing_reads_nothing() {
assert!(fields(&PLAIN, true).is_empty());
assert!(PLAIN.source.is_empty());
}
}