drizzle_types/postgres/ddl/
schema.rs1#[cfg(feature = "std")]
8use std::borrow::Cow;
9
10#[cfg(all(feature = "alloc", not(feature = "std")))]
11use alloc::borrow::Cow;
12
13#[cfg(feature = "serde")]
14use crate::serde_helpers::cow_from_string;
15
16#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
22pub struct SchemaDef {
23 pub name: &'static str,
25}
26
27impl SchemaDef {
28 #[must_use]
30 pub const fn new(name: &'static str) -> Self {
31 Self { name }
32 }
33
34 #[must_use]
36 pub const fn into_schema(self) -> Schema {
37 Schema {
38 name: Cow::Borrowed(self.name),
39 }
40 }
41}
42
43impl Default for SchemaDef {
44 fn default() -> Self {
45 Self::new("public")
46 }
47}
48
49#[derive(Clone, Debug, PartialEq, Eq)]
55#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
56#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
57pub struct Schema {
58 #[cfg_attr(feature = "serde", serde(deserialize_with = "cow_from_string"))]
60 pub name: Cow<'static, str>,
61}
62
63impl Schema {
64 #[must_use]
66 pub fn new(name: impl Into<Cow<'static, str>>) -> Self {
67 Self { name: name.into() }
68 }
69
70 #[inline]
72 #[must_use]
73 pub fn name(&self) -> &str {
74 &self.name
75 }
76}
77
78impl Default for Schema {
79 fn default() -> Self {
80 Self::new("public")
81 }
82}
83
84impl From<SchemaDef> for Schema {
85 fn from(def: SchemaDef) -> Self {
86 def.into_schema()
87 }
88}
89
90#[cfg(test)]
91mod tests {
92 use super::*;
93
94 #[test]
95 fn test_const_schema_def() {
96 const SCHEMA: SchemaDef = SchemaDef::new("custom_schema");
97
98 assert_eq!(SCHEMA.name, "custom_schema");
99 }
100
101 #[test]
102 fn test_schema_def_to_schema() {
103 const DEF: SchemaDef = SchemaDef::new("public");
104 let schema = DEF.into_schema();
105 assert_eq!(schema.name(), "public");
106 }
107}