Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support set_id #7

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "surreal-simple-querybuilder"
version = "0.3.1"
version = "0.3.2"
edition = "2021"
license = "MIT"
description = "A query-building & utility crate for SurrealDB and its SQL querying language that aims to be simple"
Expand All @@ -17,7 +17,7 @@ members = [
[dependencies]
serde = { version = "1.0.144", features = ["derive"] }

surreal-simple-querybuilder-proc-macro = "0.1.0"
surreal-simple-querybuilder-proc-macro = {path = "model-proc-macro"}

[dev-dependencies]
serde_json = "1.0"
30 changes: 30 additions & 0 deletions model-proc-macro/src/ast/model.rs
Expand Up @@ -55,6 +55,10 @@ impl Display for Model {
}
}

pub fn origin(self) -> SchemaField<N> {
SchemaField::with_origin("", SchemaFieldType::None, self.origin)
}

pub fn with_origin(origin: OriginHolder<N>) -> Self {
let origin = Some(origin);

Expand All @@ -64,6 +68,32 @@ impl Display for Model {
}
}

pub fn set_node(self) -> #name<{N+1}> {

let origin = self.origin.unwrap_or_else(|| OriginHolder::new([""; N]));
let mut new_origin: [&'static str; N + 1] = [""; N + 1];
new_origin[..N].clone_from_slice(&origin.segments);

new_origin[N] = Self::label;

#name::with_origin(OriginHolder::new(new_origin))
}

pub fn set_id(self,id: &str) -> #name<{N+2}> {

let origin = self.origin.unwrap_or_else(|| OriginHolder::new([""; N]));
let mut new_origin: [&'static str; N + 2] = [""; N + 2];
new_origin[..N].clone_from_slice(&origin.segments);

if (N > 0 && new_origin[N - 1] != ":") {
new_origin[N] = ":";
}

new_origin[N + 1] = Box::leak(id.to_owned().into_boxed_str()); // to static str

#name::with_origin(OriginHolder::new(new_origin))
}

#(#field_foreign_functions)*
}

Expand Down
4 changes: 4 additions & 0 deletions src/model/schema_field.rs
Expand Up @@ -6,6 +6,7 @@ use crate::prelude::IntoQueryBuilderSegment;
use crate::prelude::QueryBuilderSegment;

pub enum SchemaFieldType {
None,
Property,
Relation,
ForeignRelation,
Expand Down Expand Up @@ -93,6 +94,7 @@ impl<const N: usize> Display for SchemaField<N> {
write!(f, "{holder}")?;

match &self.field_type {
SchemaFieldType::None => write!(f, "")?,
SchemaFieldType::Property => write!(f, ".")?,
SchemaFieldType::Relation => write!(f, "->")?,
SchemaFieldType::ForeignRelation => write!(f, "<-")?,
Expand All @@ -103,6 +105,7 @@ impl<const N: usize> Display for SchemaField<N> {
None => {
// prefix depending on the field type
match &self.field_type {
SchemaFieldType::None => {}
SchemaFieldType::Property => {}
SchemaFieldType::Relation => write!(f, "->")?,
SchemaFieldType::ForeignRelation => write!(f, "<-")?,
Expand All @@ -122,6 +125,7 @@ impl<const N: usize> ToNodeBuilder for SchemaField<N> {
"{self} = ${}",
self
.to_string()
.replace(":", "_")
.replace(".", "_")
.replace("->", "_")
.replace("<-", "_")
Expand Down
12 changes: 12 additions & 0 deletions src/node_builder.rs
Expand Up @@ -56,6 +56,18 @@ pub trait ToNodeBuilder<T: Display = Self>: Display {
format!("{label_name}:{self}")
}

/// # Example
/// ```
/// use surreal_simple_querybuilder::prelude::*;
///
/// let label = "Account".set_id("John");
///
/// assert_eq!(label, "Account:John");
/// ```
fn set_id(&self, id: &str) -> String {
format!("{self}:{id}")
}

/// # Example
/// ```
/// use surreal_simple_querybuilder::prelude::*;
Expand Down