use crate::alloc_prelude::*;
#[cfg(feature = "serde")]
use crate::serde_helpers::cow_from_string;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct TableDef {
pub schema: &'static str,
pub name: &'static str,
pub is_rls_enabled: bool,
}
impl TableDef {
#[must_use]
pub const fn new(schema: &'static str, name: &'static str) -> Self {
Self {
schema,
name,
is_rls_enabled: false,
}
}
#[must_use]
pub const fn rls_enabled(self) -> Self {
Self {
schema: self.schema,
name: self.name,
is_rls_enabled: true,
}
}
#[must_use]
pub const fn into_table(self) -> Table {
Table {
schema: Cow::Borrowed(self.schema),
name: Cow::Borrowed(self.name),
is_rls_enabled: if self.is_rls_enabled {
Some(true)
} else {
None
},
}
}
}
impl Default for TableDef {
fn default() -> Self {
Self::new("public", "")
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct Table {
#[cfg_attr(feature = "serde", serde(deserialize_with = "cow_from_string"))]
pub schema: Cow<'static, str>,
#[cfg_attr(feature = "serde", serde(deserialize_with = "cow_from_string"))]
pub name: Cow<'static, str>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub is_rls_enabled: Option<bool>,
}
impl Table {
#[must_use]
pub fn new(schema: impl Into<Cow<'static, str>>, name: impl Into<Cow<'static, str>>) -> Self {
Self {
schema: schema.into(),
name: name.into(),
is_rls_enabled: None,
}
}
#[must_use]
pub const fn rls_enabled(mut self) -> Self {
self.is_rls_enabled = Some(true);
self
}
#[inline]
#[must_use]
pub fn schema(&self) -> &str {
&self.schema
}
#[inline]
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
}
impl Default for Table {
fn default() -> Self {
Self::new("public", "")
}
}
impl From<TableDef> for Table {
fn from(def: TableDef) -> Self {
def.into_table()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_const_table_def() {
const TABLE: TableDef = TableDef::new("public", "users").rls_enabled();
assert_eq!(TABLE.schema, "public");
assert_eq!(TABLE.name, "users");
const {
assert!(TABLE.is_rls_enabled);
}
}
#[test]
fn test_table_def_to_table() {
const DEF: TableDef = TableDef::new("public", "users");
let table = DEF.into_table();
assert_eq!(table.schema(), "public");
assert_eq!(table.name(), "users");
}
}