commit 331f67f130eb2e53edeb4370134551cf316b15c8 Author: everyone Date: Sun Oct 29 23:02:56 2023 -0700 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..8efae40 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "[rust]": { + "editor.formatOnSave": true, + "editor.formatOnSaveMode": "file", + "editor.defaultFormatter": "rust-lang.rust-analyzer" + } +} \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..729226d --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "graphql_query_builder" +authors = ["DrakeTDL"] +version = "0.1.0" +edition = "2021" +description = "A GraphQL query builder." +repository = "https://git.disroot.org/everyone/graphql_query_builder" +license-file = "LICENSE" +keywords= ["graphql", "gql", "builder"] +# categories = [] +exclude = [".vscode", ".gitignore"] + +[lib] +name = "graphql_query_builder" +path = "src/lib.rs" +crate-type = ["cdylib", "rlib"] + +[dependencies] +either = "1.9.0" +strum = { version = "0.25", features = ["derive"] } +strum_macros = "0.25" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f7be8ec --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 [fullname] + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..0006eab --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,69 @@ +use std::collections::HashMap; +use strum_macros::Display; + +#[derive(Clone)] +pub enum VariableValues { + Int(i32), + Str(&'static str), + Enum(&'static str), + VecInt(Vec), + VecStr(Vec<&'static str>), + VecEnum(Vec<&'static str>), +} +type Fields = Vec>; +#[derive(Clone)] +pub struct Args { + pub operation: String, + pub fields: Fields, + pub variables: Option>, +} + +pub fn tabs(level: usize) -> String { + "\t".repeat(level) +} + +// pub struct OperationArgs { +// pub level: usize, +// pub field: &'static str, +// pub variables: Option>, +// pub sub_field: Option, +// pub union: Option, +// } + +#[derive(Display)] +#[strum(serialize_all = "snake_case")] +pub enum Operation { + Query, + Mutation, + Subscription, +} + +pub struct Builder { + pub raw: String, +} + +impl Builder { + pub fn new(operation: Operation, opts: &Args) -> String { + let subfields = Self::parse_sub_fields(&opts, 1).join("\n"); + let field = &opts.operation; + format!("{operation} {{\n\t{field} {{\n{subfields}\n\t}}\n}}",) + } + fn parse_sub_fields(args: &Args, level: usize) -> Vec { + let level = level + 1; + let tabs = tabs(level); + args.fields + .clone() + .into_iter() + .map(|arg| match arg { + either::Either::Left(field) => { + format!("{tabs}{field}", field = field) + } + either::Either::Right(args) => { + let field = &args.operation; + let sub_field = Self::parse_sub_fields(&args, level).join("\n"); + format!("{tabs}{field} {{\n{sub_field}\n{tabs}}}") + } + }) + .collect() + } +}