Skip to content

Commit

Permalink
remove cli dependency to binary 'generate_tsql' from library 'tsql' b…
Browse files Browse the repository at this point in the history
…enches
  • Loading branch information
LetsMelon committed Aug 19, 2023
1 parent e1aac5c commit 4264235
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ hmac-sha256 = { version = "1.1.7", optional = true }
criterion = { version = "0.4", features = ["html_reports"] }

[features]
default = ["generate"]
default = []
generate = ["dep:hmac-sha256"]

[[bench]]
Expand Down
44 changes: 20 additions & 24 deletions lib/benches/parsing_tables.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::env;
use std::fmt::Display;
use std::process::{Command, Stdio};
use std::time::Duration;

use anyhow::Result;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use tsql::parse_str;
#[cfg(feature = "generate")]
use tsql::generate::generate_table;
use tsql::types::Table;
use tsql::{parse_str, TransformTSQL};

struct Arguments {
tables: usize,
Expand All @@ -24,28 +25,23 @@ impl Display for Arguments {
}
}

fn generate_test_file(arguments: &Arguments) -> Result<String> {
let current_dir = env::current_dir()?;
// TODO remove this "hacky" way to get the target directory.
// Because I think this will fail with the user sets a custom directory for the `target` dir.
// Maybe some env variable has the absolute path to the dir. Or figure it out with a build script.
let target_dir = current_dir
.as_path()
.parent()
.unwrap()
.join("target")
.join("debug");
fn generate_test_content(arguments: &Arguments) -> Result<String> {
let mut buffer = Vec::new();

// TODO don't call the executable, call the functions directly
let output = Command::new(&target_dir.join("generate_tsql"))
.current_dir(&target_dir)
.arg("--stdout")
.args(["--tables", &arguments.tables.to_string()])
.args(["--fields", &arguments.fields.to_string()])
.stdout(Stdio::piped())
.output()?;
for i in 0..arguments.tables {
#[cfg(feature = "generate")]
let table: Table = generate_table(i, arguments.fields);
#[cfg(not(feature = "generate"))]
let table: Table = {
// TODO make this nicer and maybe into a compile time warning and not a runtime
panic!("The feature `generate` has to be enabled to run this benches.");
todo!()
};

Ok(String::from_utf8(output.stdout)?)
table.transform_into_tsql(&mut buffer)?;
}

Ok(String::from_utf8(buffer)?)
}

fn criterion_benchmark(c: &mut Criterion) {
Expand All @@ -59,7 +55,7 @@ fn criterion_benchmark(c: &mut Criterion) {
];

for argument in arguments {
let content = generate_test_file(&argument).unwrap();
let content = generate_test_content(&argument).unwrap();

c.bench_function(&format!("{}", argument), |b| {
b.iter(|| black_box(parse_str(&content)))
Expand Down
2 changes: 0 additions & 2 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use crate::parser::parse;

#[cfg(feature = "generate")]
pub mod generate;
#[cfg(not(feature = "generate"))]
mod generate;

mod parser;
pub mod types;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ use std::rc::Rc;
use anyhow::{bail, Result};
use static_assertions::const_assert_eq;

use crate::generate::hash_number_and_stringify;
#[cfg(feature = "generate")]
use crate::generate::GenerateDummy;
use crate::generate::{hash_number_and_stringify, GenerateDummy};
use crate::parser::types::{FieldExtra, FieldType, RawDataType, RawField, RawTable};
use crate::{TransformSQL, TransformTSQL};

Expand Down Expand Up @@ -302,6 +301,7 @@ impl TransformTSQL for Field {
}
}

#[cfg(feature = "generate")]
impl GenerateDummy for Field {
fn generate_dummy(number: usize) -> Self {
let name = hash_number_and_stringify(number);
Expand Down

0 comments on commit 4264235

Please sign in to comment.