drizzle_types/postgres/ddl/
schema.rs

1//! PostgreSQL Schema DDL types
2//!
3//! This module provides two complementary types:
4//! - [`SchemaDef`] - A const-friendly definition type for compile-time schema definitions
5//! - [`Schema`] - A runtime type for serde serialization/deserialization
6
7#[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// =============================================================================
17// Const-friendly Definition Type
18// =============================================================================
19
20/// Const-friendly schema definition for compile-time schema definitions.
21#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
22pub struct SchemaDef {
23    /// Schema name
24    pub name: &'static str,
25}
26
27impl SchemaDef {
28    /// Create a new schema definition
29    #[must_use]
30    pub const fn new(name: &'static str) -> Self {
31        Self { name }
32    }
33
34    /// Convert to runtime [`Schema`] type
35    #[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// =============================================================================
50// Runtime Type for Serde
51// =============================================================================
52
53/// Runtime schema entity for serde serialization.
54#[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    /// Schema name
59    #[cfg_attr(feature = "serde", serde(deserialize_with = "cow_from_string"))]
60    pub name: Cow<'static, str>,
61}
62
63impl Schema {
64    /// Create a new schema (runtime)
65    #[must_use]
66    pub fn new(name: impl Into<Cow<'static, str>>) -> Self {
67        Self { name: name.into() }
68    }
69
70    /// Get the schema name
71    #[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}