use crate::{
parser::{
parse_falkor_enum, redis_value_as_typed_string, redis_value_as_typed_string_vec,
redis_value_as_vec, SchemaParsable,
},
EntityType, FalkorDBError, FalkorResult, GraphSchema,
};
#[derive(Copy, Clone, Debug, Eq, PartialEq, strum::EnumString, strum::Display)]
#[strum(serialize_all = "UPPERCASE")]
pub enum ConstraintType {
Unique,
Mandatory,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, strum::EnumString, strum::Display)]
pub enum ConstraintStatus {
#[strum(serialize = "OPERATIONAL")]
Active,
#[strum(serialize = "UNDER CONSTRUCTION")]
Pending,
Failed,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Constraint {
pub constraint_type: ConstraintType,
pub label: String,
pub properties: Vec<String>,
pub entity_type: EntityType,
pub status: ConstraintStatus,
}
impl SchemaParsable for Constraint {
#[cfg_attr(
feature = "tracing",
tracing::instrument(name = "Parse Constraint", skip_all, level = "info")
)]
fn parse(
value: redis::Value,
_: &mut GraphSchema,
) -> FalkorResult<Self> {
let [constraint_type_raw, label_raw, properties_raw, entity_type_raw, status_raw]: [redis::Value; 5] = redis_value_as_vec(value)
.and_then(|res| res.try_into()
.map_err(|_| FalkorDBError::ParsingArrayToStructElementCount("Expected exactly 5 elements in constraint object")))?;
Ok(Self {
constraint_type: parse_falkor_enum(constraint_type_raw)?,
label: redis_value_as_typed_string(label_raw)?,
properties: redis_value_as_typed_string_vec(properties_raw)?,
entity_type: parse_falkor_enum(entity_type_raw)?,
status: parse_falkor_enum(status_raw)?,
})
}
}