Skip to main content

elefant_tools/models/
constraint.rs

1use crate::models::check_constraint::PostgresCheckConstraint;
2use crate::models::foreign_key::PostgresForeignKey;
3use crate::models::not_null_constraint::PostgresNotNullConstraint;
4use crate::models::unique_constraint::PostgresUniqueConstraint;
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Clone, Serialize, Deserialize)]
8#[serde(tag = "type")]
9pub enum PostgresConstraint {
10    Check(PostgresCheckConstraint),
11    ForeignKey(PostgresForeignKey),
12    NotNull(PostgresNotNullConstraint),
13    Unique(PostgresUniqueConstraint),
14}
15
16impl From<PostgresCheckConstraint> for PostgresConstraint {
17    fn from(value: PostgresCheckConstraint) -> Self {
18        PostgresConstraint::Check(value)
19    }
20}
21
22impl From<PostgresForeignKey> for PostgresConstraint {
23    fn from(value: PostgresForeignKey) -> Self {
24        PostgresConstraint::ForeignKey(value)
25    }
26}
27
28impl From<PostgresNotNullConstraint> for PostgresConstraint {
29    fn from(value: PostgresNotNullConstraint) -> Self {
30        PostgresConstraint::NotNull(value)
31    }
32}
33
34impl From<PostgresUniqueConstraint> for PostgresConstraint {
35    fn from(value: PostgresUniqueConstraint) -> Self {
36        PostgresConstraint::Unique(value)
37    }
38}
39
40impl PostgresConstraint {
41    pub(crate) fn name(&self) -> &str {
42        match self {
43            PostgresConstraint::Check(constraint) => &constraint.name,
44            PostgresConstraint::ForeignKey(constraint) => &constraint.name,
45            PostgresConstraint::NotNull(constraint) => &constraint.name,
46            PostgresConstraint::Unique(constraint) => &constraint.name,
47        }
48    }
49}