Skip to content

Commit

Permalink
Merge 6817e90 into d9e6554
Browse files Browse the repository at this point in the history
  • Loading branch information
gabotechs authored Oct 1, 2022
2 parents d9e6554 + 6817e90 commit 3c67d59
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
5 changes: 3 additions & 2 deletions graphqxl_synthesizer/src/synth_description.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::synths::{Synth, SynthConfig, SynthContext};
use crate::utils::escape_non_escaped_quotes;

pub(crate) struct DescriptionSynth {
pub(crate) text: String,
Expand Down Expand Up @@ -27,14 +28,14 @@ impl Synth for DescriptionSynth {
for line in self.text.split('\n') {
result += "\n";
result += &" ".repeat(context.indent_lvl * self.indent_spaces);
result += line;
result += &escape_non_escaped_quotes(line);
}
result += "\n";
result += &" ".repeat(context.indent_lvl * self.indent_spaces);
result += "\"\"\"";
} else {
result += "\"";
result += &self.text;
result += &escape_non_escaped_quotes(&self.text);
result += "\"";
}
result
Expand Down
4 changes: 2 additions & 2 deletions graphqxl_synthesizer/src/synth_value_data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::is_last_iter;
use crate::utils::{escape_non_escaped_quotes, is_last_iter};
use crate::{Synth, SynthContext};
use graphqxl_parser::{ValueBasicData, ValueData};

Expand All @@ -22,7 +22,7 @@ impl Synth for ValueDataSynth {
res
}
ValueBasicData::Boolean(v) => v.to_string(),
ValueBasicData::String(v) => format!("\"{v}\""),
ValueBasicData::String(v) => format!("\"{}\"", escape_non_escaped_quotes(v)),
},
ValueData::List(items) => {
let mut summed = "[".to_string();
Expand Down
18 changes: 18 additions & 0 deletions graphqxl_synthesizer/src/utils/escape_non_escaped_quotes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub(crate) fn escape_non_escaped_quotes(str: &str) -> String {
str.replace("\\\"", "\"").replace('\"', "\\\"")
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_escapes_quotes() {
assert_eq!(escape_non_escaped_quotes("\""), "\\\"")
}

#[test]
fn test_not_escapes_already_escaped_quote() {
assert_eq!(escape_non_escaped_quotes("\\\""), "\\\"")
}
}
2 changes: 2 additions & 0 deletions graphqxl_synthesizer/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod escape_non_escaped_quotes;
mod is_last_iter;

pub(crate) use escape_non_escaped_quotes::*;
pub(crate) use is_last_iter::*;
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ struct Args {
#[clap()]
input: String,

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

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

Expand All @@ -20,7 +23,9 @@ struct Args {

fn main() -> Result<()> {
let args = Args::parse();
let out_path = if args.input.ends_with("graphqxl") {
let out_path = if let Some(out_path) = args.output {
out_path
} else if args.input.ends_with("graphqxl") {
args.input[..args.input.len() - 2].to_string() + "l"
} else {
args.input.to_string() + ".graphql"
Expand Down

0 comments on commit 3c67d59

Please sign in to comment.