use crate::WrittenObject;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ReaderKind {
View,
Function,
Procedure,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ViewReader {
pub kind: ReaderKind,
pub schema: Option<String>,
pub name: String,
pub relation: String,
pub parameter_types: Vec<String>,
}
impl ViewReader {
#[must_use]
pub const fn new(
kind: ReaderKind,
schema: Option<String>,
name: String,
relation: String,
parameter_types: Vec<String>,
) -> Self {
Self { kind, schema, name, relation, parameter_types }
}
#[must_use]
pub fn object(&self) -> WrittenObject<'_> {
WrittenObject::new(self.schema.as_deref(), &self.name, &self.relation)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct ViewReplacement<'a> {
pub view: WrittenObject<'a>,
pub create: &'a str,
pub readers: &'a [ViewReader],
}
impl<'a> ViewReplacement<'a> {
#[must_use]
pub const fn new(view: WrittenObject<'a>, create: &'a str, readers: &'a [ViewReader]) -> Self {
Self { view, create, readers }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_reader_names_itself_as_a_written_object() {
let reader = ViewReader::new(
ReaderKind::Procedure,
Some("retail_core".to_owned()),
"\"core_customers_load\"".to_owned(),
"retail_core.\"core_customers_load\"".to_owned(),
Vec::new(),
);
let object = reader.object();
assert_eq!(object.schema, Some("retail_core"));
assert_eq!(object.relation, "retail_core.\"core_customers_load\"");
}
}