use super::node::{JsonType, PropertySchema};
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct VertexSchema {
pub schema: PropertySchema,
}
impl Default for VertexSchema {
fn default() -> Self {
Self::new(PropertySchema::object())
}
}
impl VertexSchema {
pub fn new(schema: PropertySchema) -> Self {
Self { schema }
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct EdgeConnection {
pub from: String,
pub to: String,
}
impl EdgeConnection {
pub fn new(from: impl Into<String>, to: impl Into<String>) -> Self {
Self {
from: from.into(),
to: to.into(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct EdgeSchema {
pub connections: Vec<EdgeConnection>,
pub schema: PropertySchema,
}
impl Default for EdgeSchema {
fn default() -> Self {
Self::new(PropertySchema::object())
}
}
impl EdgeSchema {
pub fn new(schema: PropertySchema) -> Self {
Self {
connections: Vec::new(),
schema,
}
}
pub fn with_connection(mut self, from: impl Into<String>, to: impl Into<String>) -> Self {
self.connections.push(EdgeConnection::new(from, to));
self
}
}
pub(crate) fn is_object_root(schema: &PropertySchema) -> bool {
schema.types.as_deref() == Some(&[JsonType::Object])
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum SchemaMode {
#[default]
None,
Open,
Closed,
}
impl SchemaMode {
pub fn as_str(&self) -> &'static str {
match self {
SchemaMode::None => "none",
SchemaMode::Open => "open",
SchemaMode::Closed => "closed",
}
}
pub fn from_name(name: &str) -> Option<Self> {
match name {
"none" => Some(SchemaMode::None),
"open" => Some(SchemaMode::Open),
"closed" => Some(SchemaMode::Closed),
_ => None,
}
}
pub fn parse(name: &str) -> Result<Self, crate::error::SchemaError> {
Self::from_name(name).ok_or_else(|| crate::error::SchemaError::InvalidSchema {
path: "/mode".to_string(),
reason: format!("unknown mode '{name}'"),
hint: "The mode is one of \"none\", \"open\", \"closed\" (lowercase); in the DSL also \
the tokens SchemaMode.none, SchemaMode.open, SchemaMode.closed."
.to_string(),
})
}
pub fn default_additional_properties(&self) -> bool {
!matches!(self, SchemaMode::Closed)
}
}
impl std::fmt::Display for SchemaMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ElementKind {
#[cfg_attr(feature = "serde", serde(rename = "vertex"))]
Vertex,
#[cfg_attr(feature = "serde", serde(rename = "edge"))]
Edge,
}
impl ElementKind {
#[inline]
pub const fn as_str(self) -> &'static str {
match self {
Self::Vertex => "vertex",
Self::Edge => "edge",
}
}
}
impl std::fmt::Display for ElementKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}