gqb 0.10.0

GQL Query Builder API.
Documentation
use crate::prelude::*;

/// A template for matching edges in a graph query
pub struct MatchEdgeTemplate
{
  /// Source variable for the edge
  pub source: Option<Variable>,
  /// Destination variable for the edge
  pub destination: Option<Variable>,
  /// Labels
  pub labels: Vec<String>,
  /// Properties
  pub properties: graphcore::ValueMap,
}

/// Trait for types that can be converted into an edge template (labels and properties, and optionally source and destination variables)
pub trait IntoMatchEdgeTemplate
{
  /// Variable type for the template
  type VariableType: FromVariable + Into<Variable>;

  /// Convert the type into an edge template (labels and properties, and optionally source and destination variables)
  fn into_edge_template(self, builder: &mut Builder) -> KeyOrTemplate<MatchEdgeTemplate>;
}

impl IntoMatchEdgeTemplate for MatchEdgeTemplate
{
  type VariableType = Variable;
  fn into_edge_template(self, _: &mut Builder) -> KeyOrTemplate<MatchEdgeTemplate>
  {
    KeyOrTemplate::Template(self)
  }
}

/// A template for creating edges in a graph query
pub struct CreateEdgeTemplate
{
  /// Source variable for the edge
  pub source: Variable,
  /// Destination variable for the edge
  pub destination: Variable,
  /// Labels
  pub labels: Vec<String>,
  /// Properties
  pub properties: graphcore::ValueMap,
}

/// Trait for types that can be converted into an edge template (labels and properties, and source and destination variables)
pub trait IntoCreateEdgeTemplate
{
  /// Variable type for the template
  type VariableType: FromVariable + Into<Variable>;

  /// Convert the type into an edge template (labels and properties, and optionally source and destination variables)
  fn into_edge_template(self, builder: &mut Builder) -> CreateEdgeTemplate;
}

impl IntoCreateEdgeTemplate for CreateEdgeTemplate
{
  type VariableType = Variable;
  fn into_edge_template(self, _: &mut Builder) -> CreateEdgeTemplate
  {
    self
  }
}

/// Macro to generate IntoMatchEdgeTemplate implementation for types that support IntoCreateEdgeTemplate
#[macro_export]
macro_rules! match_edge_template {
  ($type:ty) => {
    impl IntoMatchEdgeTemplate for $type
    {
      type VariableType = <Self as $crate::IntoCreateEdgeTemplate>::VariableType;

      fn into_edge_template(
        self,
        builder: &mut Builder,
      ) -> $crate::KeyOrTemplate<(
        Option<$crate::Variable>,
        Vec<String>,
        graphcore::ValueMap,
        Option<$crate::Variable>,
      )>
      {
        let (src, labels, props, dst) =
          <Self as $crate::IntoCreateEdgeTemplate>::into_edge_template(self, builder);
        $crate::KeyOrTemplate::Template((Some(src), labels, props, Some(dst)))
      }
    }
  };
}

/// Macro to generate IntoMatchEdgeTemplate implementation for types that support IntoCreateEdgeTemplate via an alias
#[macro_export]
macro_rules! match_edge_template_for_alias {
  ($type:ty, $alias:tt) => {
    impl IntoMatchEdgeTemplate for $type
    {
      type VariableType = <$alias as $crate::IntoCreateEdgeTemplate>::VariableType;

      fn into_edge_template(self, builder: &mut Builder)
      -> KeyOrTemplate<$crate::MatchEdgeTemplate>
      {
        let template =
          <$alias as $crate::IntoCreateEdgeTemplate>::into_edge_template(self.0, builder);
        KeyOrTemplate::Template($crate::MatchEdgeTemplate {
          source: Some(template.source),
          destination: Some(template.destination),
          labels: template.labels,
          properties: template.properties,
        })
      }
    }
  };
}

impl<T0, T1, T2, T3> IntoMatchEdgeTemplate for (T0, T1, T2, T3)
where
  T0: Into<Option<Variable>>,
  T1: Into<Vec<String>>,
  T2: Into<graphcore::ValueMap>,
  T3: Into<Option<Variable>>,
{
  type VariableType = Variable;
  fn into_edge_template(self, _builder: &mut Builder) -> KeyOrTemplate<MatchEdgeTemplate>
  {
    KeyOrTemplate::Template(MatchEdgeTemplate {
      source: self.0.into(),
      destination: self.3.into(),
      labels: self.1.into(),
      properties: self.2.into(),
    })
  }
}

impl<T0, T1, T2, T3> IntoCreateEdgeTemplate for (T0, T1, T2, T3)
where
  T0: Into<Variable>,
  T1: Into<Vec<String>>,
  T2: Into<graphcore::ValueMap>,
  T3: Into<Variable>,
{
  type VariableType = Variable;

  fn into_edge_template(self, _builder: &mut Builder) -> CreateEdgeTemplate
  {
    CreateEdgeTemplate {
      source: self.0.into(),
      destination: self.3.into(),
      labels: self.1.into(),
      properties: self.2.into(),
    }
  }
}