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

Automatic formatting check #22

Merged
merged 4 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ name: Rust

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

env:
CARGO_TERM_COLOR: always
Expand All @@ -20,3 +17,10 @@ jobs:
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose

format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run rustfmt
run: cargo fmt --check
4 changes: 2 additions & 2 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! ## Error handling
//!
//!
//! This module provides error handling utilities for the Supabase client.

use anyhow::Error;
Expand Down Expand Up @@ -53,4 +53,4 @@ pub async fn authorization_failed_error() -> Result<(), Error> {

pub async fn invalid_query_error() -> Result<(), Error> {
Err(Error::msg("SUPABASE_RS: Invalid query"))
}
}
6 changes: 3 additions & 3 deletions src/graphql/client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! # GraphQL Client
//!
//! # GraphQL Client
//!
//! The GraphQL client is used to send GraphQL queries to the Supabase API.

use crate::SupabaseClient;

// local imports
use crate::graphql::utils::format_endpoint::endpoint;
use crate::graphql::Query;
use crate::graphql::RootTypes;
use crate::graphql::utils::format_endpoint::endpoint;
33 changes: 15 additions & 18 deletions src/graphql/error_types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! # Error Types
//!
//!
//! This module contains functions that generate error messages for various error types.
//!
//!
//! ## Functions
//! - `illegal_table_name`: Generates an error message for an illegal table name.
//! - `illegal_field_name`: Generates an error message for an illegal field name.
Expand All @@ -11,9 +11,9 @@
//! - `failed_to_parse_json`: Generates an error message indicating that JSON parsing failed.
//! - `print_red`: Prints a message in red color for errors.
//!
//!
//!

use anyhow::{Result, Error as AnyError};
use anyhow::{Error as AnyError, Result};
use serde_json::Value;

/// Generates an error message for an illegal table name.
Expand All @@ -28,10 +28,7 @@ use serde_json::Value;
///
/// A `String` containing the error message.
pub fn illegal_table_name(table_name: &str) -> String {
let amount_of_numbers: usize = table_name
.chars()
.take_while(|c| c.is_numeric())
.count();
let amount_of_numbers: usize = table_name.chars().take_while(|c| c.is_numeric()).count();
let arrow_amount: String = "^".repeat(amount_of_numbers);

let is_plural: &str = if amount_of_numbers > 1 { "s" } else { "" };
Expand Down Expand Up @@ -60,10 +57,7 @@ pub fn illegal_table_name(table_name: &str) -> String {
///
/// A `String` containing the error message.
pub fn illegal_field_name(field_name: &str) -> String {
let amount_of_numbers: usize = field_name
.chars()
.take_while(|c| c.is_numeric())
.count();
let amount_of_numbers: usize = field_name.chars().take_while(|c| c.is_numeric()).count();
let arrow_amount: String = "^".repeat(amount_of_numbers);

let is_plural: &str = if amount_of_numbers > 1 { "s" } else { "" };
Expand Down Expand Up @@ -109,9 +103,9 @@ pub fn table_does_not_exist(name: &str) -> String {
pub fn field_does_not_exist_on_table(field: &str, table: &str) -> String {
let error: String = format!(
"\x1b[1;31mField does not exist on table: \n {} -> {}\x1b[0m",
table,
field
).to_string();
table, field
)
.to_string();

print_red(&error);

Expand All @@ -128,8 +122,11 @@ pub fn field_does_not_exist_on_table(field: &str, table: &str) -> String {
///
/// A `String` containing the error message.
pub fn table_name_does_not_end_with_collection(table_name: &str) -> String {
let error: String =
format!("\x1b[1;31mTable name does not end with \x1b[1;34m`Collection`\x1b[1;31m: {}\x1b[0m", table_name).to_string();
let error: String = format!(
"\x1b[1;31mTable name does not end with \x1b[1;34m`Collection`\x1b[1;31m: {}\x1b[0m",
table_name
)
.to_string();
let arrow_amount: String = "^".repeat(table_name.len());

print_red(&error);
Expand Down Expand Up @@ -164,4 +161,4 @@ pub fn failed_to_parse_json(error: String) -> AnyError {
/// * `error` - A string slice that holds the error message.
pub fn print_red(error: &str) {
println!("\x1b[1;31m{}\x1b[0m", error);
}
}
74 changes: 36 additions & 38 deletions src/graphql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,60 @@

//! # GraphQL for Supabase
//! This module provides a GraphQL client for interacting with the Supabase API.
//!
//!
//! ## Features
//! - **Query**: Send queries to the Supabase API.
//!
//!
//! ## Usage
//! Before using the GraphQL client, ensure you have a valid `SupabaseClient` instance.
//!
//!
//! ### Example: Authenticate with Supabase
//! ```ignore
//! use supabase_rs::SupabaseClient;
//!
//!
//! #[tokio::main]
//! async fn main() {
//! let supabase_client: SupabaseClient = SupabaseClient::new(
//! std::env::var("SUPABASE_URL").unwrap(),
//! std::env::var("SUPABASE_KEY").unwrap()
//! );
//! ```
//!
//!
//! ### Example: Send a GraphQL query
//! ```ignore
//! let request_graphql: Request = Request::new(
//! supabase_client,
//! json!({
//! "query": r#"
//! {
//! usersCollection(first: 1) {
//! edges {
//! node {
//! {
//! usersCollection(first: 1) {
//! edges {
//! node {
//! user_id,
//! username,
//! email
//! }
//! }
//! }
//! }
//! }
//! }
//! }
//! "#,
//! }),
//! supabase_rs::graphql::RootTypes::Query
//! );
//!
//!
//! let response: Result<serde_json::Value, anyhow::Error> = request_graphql.send().await;
//!
//!
//! match response {
//! Ok(response) => println!("{:#?}", response),
//! Err(error) => println!("{:#?}", error),
//! }}
//! ```
//!
//!
//!
//!
//!
//! ## Error Handling
//! The GraphQL client returns a `Result<Value, Error>` where `Value` is the response from the Supabase API and `Error` is an error message in case of failure.
//!
//!
//! ## GraphQL Query
//! The GraphQL query should be in the following format:
//! ```ignore
Expand All @@ -71,67 +71,65 @@
//! }
//! ```
//! *Note*: All tables in Supabase end with `Collection`. Ensure you append `Collection` to the table name.
//!
//!
//! ## GraphQL Mutation
//!
//!
//! The GraphQL mutation should be in the following format:
//!
//!
pub mod client;
pub mod request;
pub mod parse;
pub mod utils;
pub mod query;
pub mod error_types;
pub mod parse;
pub mod parsing;
pub mod query;
pub mod request;
pub mod utils;

use crate::SupabaseClient;
use serde_json::Value;

/// #### Query
///
///
/// Represents a GraphQL query.
#[derive(Debug)]
pub struct Query {
pub query: Value,
}


/// #### RootTypes
///
///
/// The root types for GraphQL operations.
///
///
/// - `Query`: Represents a query operation.
/// - `Mutation`: Represents a mutation operation.
/// - `Subscription`: Represents a subscription operation.
/// - `Fragment`: Represents a fragment operation.
///
///
/// *Note*: Only `Query` is supported at the moment.
///
///
/// ## Example
///
///
/// ```ignore
/// use supabase_rs::graphql::RootTypes;
///
///
/// let root_type: RootTypes = RootTypes::Query;
///
///
/// println!("{:?}", root_type);
/// ```
///
///
#[derive(Debug)]
pub enum RootTypes {
Query,
Mutation,
Subscription,
Fragment
Fragment,
}


#[derive(Debug)]
pub enum GraphQLOperators {
First,
Last,
Before,
After,
Filter,
OrderBy
}
OrderBy,
}
14 changes: 6 additions & 8 deletions src/graphql/parse.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use anyhow::{ Result, Error };
use serde_json::Value;
use crate::graphql::error_types::{
illegal_table_name,
table_does_not_exist,
field_does_not_exist_on_table,
field_does_not_exist_on_table, illegal_table_name, table_does_not_exist,
table_name_does_not_end_with_collection,
};
use anyhow::{Error, Result};
use serde_json::Value;

pub fn parse_outer(query: &Value) -> bool {
if let Some(query_str) = query.as_str() {
Expand All @@ -21,13 +19,13 @@ pub fn parse_outer(query: &Value) -> bool {
}

/// Get the table name from the query
///
///
/// # Arguments
/// - `query` - A JSON Value containing the query
///
///
/// # Returns
/// A `Result` containing the table name as a `String` if successful, or an `Error` if the outer structure is invalid
///
///
pub fn get_table_name(query: &Value) -> Result<String, Error> {
if parse_outer(query) {
let query_str: &str = query["query"].as_str().unwrap_or("");
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/parsing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod node_fields;
pub mod operators;
pub mod node_fields;
1 change: 1 addition & 0 deletions src/graphql/parsing/node_fields.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions src/graphql/parsing/operators.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

12 changes: 6 additions & 6 deletions src/graphql/query.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use anyhow::{ Result, Error as AnyError };
use anyhow::{Error as AnyError, Result};
use serde_json::Value;

use crate::graphql::{ RootTypes, Query };
use crate::graphql::parse::parse_outer;
use crate::graphql::utils::format_endpoint::endpoint;
use crate::graphql::utils::headers::headers;
use crate::graphql::parse::parse_outer;
use crate::graphql::{Query, RootTypes};
use crate::SupabaseClient;

// FIX ME: This is a temporary fix to suppress the warning
impl Query {
/// # Verify the query
///
///
/// This method verifies the query to ensure it is in the correct format.
///
///
///
///
pub async fn verify(&self) -> Result<bool> {
Ok(parse_outer(&self.query))
}
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod format_endpoint;
pub mod headers;
pub mod headers;
6 changes: 3 additions & 3 deletions src/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
//! #[tokio::main]
//! async fn main() {
//! let client = SupabaseClient::new(
//! "your_supabase_url", "your_supabase_key"
//! "your_supabase_url".to_string(), "your_supabase_key".to_string()
//! );
//! let insert_result = client.insert(
//! "your_table_name", json!({"column_name": "value"})
Expand All @@ -37,7 +37,7 @@
//! #[tokio::main]
//! async fn main() {
//! let client = SupabaseClient::new(
//! "your_supabase_url", "your_supabase_key"
//! "your_supabase_url".to_string(), "your_supabase_key".to_string()
//! );
//! let unique_insert_result = client.insert_if_unique(
//! "your_table_name", json!({"unique_column_name": "unique_value"})
Expand Down Expand Up @@ -206,7 +206,7 @@ impl SupabaseClient {
/// #[tokio::main]
/// async fn main() {
/// // Initialize the Supabase client
/// let client = SupabaseClient::new("your_supabase_url", "your_supabase_key");
/// let client = SupabaseClient::new("your_supabase_url".to_string(), "your_supabase_key".to_string());
///
/// // This will insert a new row into the table if the value is unique
/// let unique_insert_result = client.insert_if_unique(
Expand Down
Loading