elefant_tools/models/
constraint.rs

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