Skip to content

Commit

Permalink
Merge 1624d4b into 0c024df
Browse files Browse the repository at this point in the history
  • Loading branch information
gabotechs authored Oct 1, 2022
2 parents 0c024df + 1624d4b commit 58819f1
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 61 deletions.
28 changes: 14 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions graphqxl_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
pest = "2.0"
pest_derive = "2.0"
pest = "2.3.1"
pest_derive = "2.3.1"
indexmap = "1.9.1"

37 changes: 26 additions & 11 deletions graphqxl_parser/src/utils/owned_span.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
use crate::Rule;
use pest::Span;

#[derive(Clone, Default, Debug)]
#[derive(Clone, Debug)]
pub struct OwnedSpan {
pub err_placeholder: pest::error::Error<Rule>,
pub input: String,
pub start: usize,
pub end: usize,
}

impl Default for OwnedSpan {
fn default() -> Self {
let err = pest::error::Error::new_from_span(
pest::error::ErrorVariant::CustomError {
message: "".to_string(),
},
Span::new("", 0, 0).unwrap(),
);
Self {
err_placeholder: err,
input: "".to_string(),
start: 0,
end: 0,
}
}
}

// FIXME: this implementation is only for the tests, It should be behind a #[cfg(Test)],
// but then I don't know how to make the real implementation available only for no test
impl PartialEq for OwnedSpan {
Expand All @@ -18,23 +37,19 @@ impl PartialEq for OwnedSpan {
impl<'a> From<Span<'a>> for OwnedSpan {
fn from(span: Span<'a>) -> Self {
Self {
err_placeholder: pest::error::Error::new_from_span(
pest::error::ErrorVariant::CustomError {
message: "".to_string(),
},
span,
),
input: span.as_str().to_string(),
start: span.start(),
end: span.end(),
}
}
}

impl From<(&str, usize, usize)> for OwnedSpan {
fn from(spec: (&str, usize, usize)) -> Self {
Self {
input: spec.0.to_string(),
start: spec.1,
end: spec.2,
}
}
}

impl<'a> From<&'a OwnedSpan> for Span<'a> {
fn from(span: &'a OwnedSpan) -> Self {
Span::new(span.input.as_str(), span.start, span.end).unwrap()
Expand Down
4 changes: 4 additions & 0 deletions graphqxl_synthesizer/src/synth_block_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ pub(crate) struct BlockDefSynth(pub(crate) BlockDef);

impl Synth for BlockDefSynth {
fn synth(&self, context: &SynthContext) -> String {
if self.0.name.id.starts_with(&context.config.private_prefix) {
return "".to_string();
}

let symbol = match self.0.kind {
BlockDefType::Type => "type",
BlockDefType::Input => "input",
Expand Down
5 changes: 4 additions & 1 deletion graphqxl_synthesizer/src/synth_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ impl Synth for SchemaSynth {
self.0.subscription.id
)))
}
if to_include.is_empty() {
return "".to_string();
}
let pair_synth = PairSynth::top_level(
&context.config,
DescriptionSynth::text(&context.config, &self.0.description.as_str()),
DescriptionSynth::text(&context.config, &self.0.description),
ChainSynth(vec![
Box::new(StringSynth("schema ".to_string())),
Box::new(MultilineListSynth::no_suffix(
Expand Down
14 changes: 10 additions & 4 deletions graphqxl_synthesizer/src/synth_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ impl Synth for SpecSynth {
DefType::Type(name) => {
let def = self.0.types.get(&name.id).unwrap().to_owned();
if def.generic.is_none() {
result += &BlockDefSynth(def).synth(context);
result += "\n\n";
let block_def_string = &BlockDefSynth(def).synth(context);
if !block_def_string.is_empty() {
result += block_def_string;
result += "\n\n";
}
}
}
DefType::Input(name) => {
let def = self.0.inputs.get(&name.id).unwrap().to_owned();
if def.generic.is_none() {
result += &BlockDefSynth(def).synth(context);
result += "\n\n";
let block_def_string = &BlockDefSynth(def).synth(context);
if !block_def_string.is_empty() {
result += block_def_string;
result += "\n\n";
}
}
}
DefType::Enum(name) => {
Expand Down
20 changes: 11 additions & 9 deletions graphqxl_synthesizer/src/synths/synth.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#[derive(Copy, Clone)]
#[derive(Clone)]
pub struct SynthConfig {
pub indent_spaces: usize,
pub max_one_line_args: usize,
pub max_one_line_ors: usize,
pub allow_multiline_values: bool,
pub private_prefix: String,
}

impl Default for SynthConfig {
Expand All @@ -13,19 +14,20 @@ impl Default for SynthConfig {
max_one_line_args: 2,
max_one_line_ors: 2,
allow_multiline_values: false,
private_prefix: "_".to_string(),
}
}
}

#[derive(Copy, Clone, Default)]
#[derive(Clone, Default)]
pub(crate) struct SynthContext {
pub(crate) indent_lvl: usize,
pub(crate) config: SynthConfig,
}

impl SynthContext {
pub(crate) fn plus_one_indent_lvl(&self) -> Self {
let mut clone = *self;
let mut clone = self.clone();
clone.indent_lvl += 1;
clone
}
Expand All @@ -44,39 +46,39 @@ mod tests {

impl SynthConfig {
pub(crate) fn indent_spaces(&self, n: usize) -> Self {
let mut clone = *self;
let mut clone = self.clone();
clone.indent_spaces = n;
clone
}

pub(crate) fn max_one_line_args(&self, n: usize) -> Self {
let mut clone = *self;
let mut clone = self.clone();
clone.max_one_line_args = n;
clone
}

pub(crate) fn max_one_line_ors(&self, n: usize) -> Self {
let mut clone = *self;
let mut clone = self.clone();
clone.max_one_line_ors = n;
clone
}

pub(crate) fn allow_multiline_values(&self) -> Self {
let mut clone = *self;
let mut clone = self.clone();
clone.allow_multiline_values = true;
clone
}
}

impl SynthContext {
pub(crate) fn with_indent_lvl(&self, lvl: usize) -> Self {
let mut clone = *self;
let mut clone = self.clone();
clone.indent_lvl = lvl;
clone
}

pub(crate) fn with_config(&self, config: SynthConfig) -> Self {
let mut clone = *self;
let mut clone = self.clone();
clone.config = config;
clone
}
Expand Down
32 changes: 19 additions & 13 deletions graphqxl_transpiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ mod transpile_block_def;
mod transpile_generic_block_def;
mod utils;

// TODO: we should not need to mutate the spec here
pub fn transpile_spec(spec: &Spec) -> Result<Spec, pest::error::Error<Rule>> {
let mut target = Spec::default();

let mut types = spec.types.clone();
let mut inputs = spec.inputs.clone();

for def in spec.order.iter() {
match def {
DefType::Type(name) => {
let transpiled = transpile_block_def(IdOrBlock::Id(name.clone()), &spec.types, 0)?;
let transpiled = transpile_block_def(IdOrBlock::Id(name.clone()), &types, 0)?;
target.types.insert(name.id.clone(), transpiled);
target.order.push(DefType::Type(name.clone()));
}
Expand All @@ -24,31 +28,33 @@ pub fn transpile_spec(spec: &Spec) -> Result<Spec, pest::error::Error<Rule>> {
} else {
return Err(custom_error(&name.span, "generic type not found"));
};
let resolved = transpile_generic_block_def(generic_type, &spec.types)?;
let transpiled = transpile_block_def(IdOrBlock::Block(resolved), &spec.types, 0)?;
let resolved = transpile_generic_block_def(generic_type, &types)?;
let transpiled = transpile_block_def(IdOrBlock::Block(resolved), &types, 0)?;
types.insert(name.id.clone(), transpiled.clone());
target.types.insert(name.id.clone(), transpiled);
target.order.push(DefType::Type(name.clone()));
}
DefType::Input(name) => {
let transpiled = transpile_block_def(IdOrBlock::Id(name.clone()), &spec.inputs, 0)?;
let transpiled = transpile_block_def(IdOrBlock::Id(name.clone()), &inputs, 0)?;
target.inputs.insert(name.id.clone(), transpiled);
target.order.push(DefType::Input(name.clone()));
}
DefType::Enum(name) => {
let transpiled = transpile_block_def(IdOrBlock::Id(name.clone()), &spec.enums, 0)?;
target.enums.insert(name.id.clone(), transpiled);
target.order.push(DefType::Enum(name.clone()));
}
DefType::GenericInput(name) => {
let generic_input = if let Some(generic_input) = spec.generic_inputs.get(&name.id) {
generic_input
} else {
return Err(custom_error(&name.span, "generic input not found"));
};
let resolved = transpile_generic_block_def(generic_input, &spec.inputs)?;
let transpiled = transpile_block_def(IdOrBlock::Block(resolved), &spec.types, 0)?;
target.types.insert(name.id.clone(), transpiled);
target.order.push(DefType::Type(name.clone()));
let resolved = transpile_generic_block_def(generic_input, &inputs)?;
let transpiled = transpile_block_def(IdOrBlock::Block(resolved), &inputs, 0)?;
inputs.insert(name.id.clone(), transpiled.clone());
target.inputs.insert(name.id.clone(), transpiled);
target.order.push(DefType::Input(name.clone()));
}
DefType::Enum(name) => {
let transpiled = transpile_block_def(IdOrBlock::Id(name.clone()), &spec.enums, 0)?;
target.enums.insert(name.id.clone(), transpiled);
target.order.push(DefType::Enum(name.clone()));
}
DefType::Interface(name) => {
let transpiled =
Expand Down
12 changes: 5 additions & 7 deletions graphqxl_transpiler/src/utils/custom_error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use graphqxl_parser::{OwnedSpan, Rule};
use pest::Span;

pub(crate) fn custom_error(span: &OwnedSpan, msg: &str) -> pest::error::Error<Rule> {
pest::error::Error::new_from_span(
pest::error::ErrorVariant::CustomError {
message: msg.to_string(),
},
Span::from(span),
)
let mut err = span.err_placeholder.clone();
err.variant = pest::error::ErrorVariant::CustomError {
message: msg.to_string(),
};
err
}
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ struct Args {

#[clap(long)]
indent_spaces: Option<usize>,

#[clap(long)]
private_prefix: Option<String>,
}

fn main() -> Result<()> {
Expand All @@ -37,6 +40,7 @@ fn main() -> Result<()> {
transpiled,
SynthConfig {
indent_spaces: args.indent_spaces.unwrap_or(2),
private_prefix: args.private_prefix.unwrap_or_else(|| "_".to_string()),
..Default::default()
},
);
Expand Down

0 comments on commit 58819f1

Please sign in to comment.