Skip to content

Commit

Permalink
Simplify generate crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Virgiel committed May 22, 2023
1 parent 426caad commit 87714e8
Show file tree
Hide file tree
Showing 49 changed files with 537 additions and 619 deletions.
File renamed without changes.
10 changes: 0 additions & 10 deletions benches/generated/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,2 @@
// This file was generated with `cornucopia`. Do not modify.

mod array_iterator;
mod domain;
mod type_traits;
mod utils;
pub use array_iterator::ArrayIterator;
pub use domain::{Domain, DomainArray};
pub use type_traits::{ArraySql, BytesSql, IterSql, StringSql};
pub(crate) use utils::slice_iter;
pub mod async_;
pub mod sync;
File renamed without changes.
8 changes: 8 additions & 0 deletions benches/generated/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// This file was generated with `cornucopia`. Do not modify.

mod array_iterator;
pub mod client;
mod domain;
#[allow(clippy::all, clippy::pedantic)]
#[allow(unused_variables)]
#[allow(unused_imports)]
#[allow(dead_code)]
pub mod queries;
mod type_traits;
#[allow(clippy::all, clippy::pedantic)]
#[allow(unused_variables)]
#[allow(unused_imports)]
#[allow(dead_code)]
pub mod types;
mod utils;
pub use array_iterator::ArrayIterator;
pub use domain::{Domain, DomainArray};
pub use type_traits::{ArraySql, BytesSql, IterSql, StringSql};
pub(crate) use utils::slice_iter;
49 changes: 17 additions & 32 deletions benches/generated/src/queries/bench.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file was generated with `cornucopia`. Do not modify.

#[derive(Debug)]
pub struct InsertUserParams<T1: crate::client::StringSql, T2: crate::client::StringSql> {
pub struct InsertUserParams<T1: crate::StringSql, T2: crate::StringSql> {
pub name: T1,
pub hair_color: Option<T2>,
}
Expand Down Expand Up @@ -167,7 +167,7 @@ pub mod sync {
let stmt = self.stmt.prepare(self.client)?;
let it = self
.client
.query_raw(stmt, crate::client::slice_iter(&self.params))?
.query_raw(stmt, crate::slice_iter(&self.params))?
.iterator()
.map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row))));
Ok(it)
Expand Down Expand Up @@ -215,7 +215,7 @@ pub mod sync {
let stmt = self.stmt.prepare(self.client)?;
let it = self
.client
.query_raw(stmt, crate::client::slice_iter(&self.params))?
.query_raw(stmt, crate::slice_iter(&self.params))?
.iterator()
.map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row))));
Ok(it)
Expand Down Expand Up @@ -263,7 +263,7 @@ pub mod sync {
let stmt = self.stmt.prepare(self.client)?;
let it = self
.client
.query_raw(stmt, crate::client::slice_iter(&self.params))?
.query_raw(stmt, crate::slice_iter(&self.params))?
.iterator()
.map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row))));
Ok(it)
Expand Down Expand Up @@ -314,7 +314,7 @@ pub mod sync {
let stmt = self.stmt.prepare(self.client)?;
let it = self
.client
.query_raw(stmt, crate::client::slice_iter(&self.params))?
.query_raw(stmt, crate::slice_iter(&self.params))?
.iterator()
.map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row))));
Ok(it)
Expand Down Expand Up @@ -349,12 +349,7 @@ pub mod sync {
}
pub struct InsertUserStmt(crate::client::sync::Stmt);
impl InsertUserStmt {
pub fn bind<
'a,
C: GenericClient,
T1: crate::client::StringSql,
T2: crate::client::StringSql,
>(
pub fn bind<'a, C: GenericClient, T1: crate::StringSql, T2: crate::StringSql>(
&'a mut self,
client: &'a mut C,
name: &'a T1,
Expand All @@ -364,7 +359,7 @@ pub mod sync {
client.execute(stmt, &[name, hair_color])
}
}
impl<'a, C: GenericClient, T1: crate::client::StringSql, T2: crate::client::StringSql>
impl<'a, C: GenericClient, T1: crate::StringSql, T2: crate::StringSql>
crate::client::sync::Params<
'a,
super::InsertUserParams<T1, T2>,
Expand Down Expand Up @@ -410,7 +405,7 @@ pub mod sync {
}
pub struct PostByUserIdsStmt(crate::client::sync::Stmt);
impl PostByUserIdsStmt {
pub fn bind<'a, C: GenericClient, T1: crate::client::ArraySql<Item = i32>>(
pub fn bind<'a, C: GenericClient, T1: crate::ArraySql<Item = i32>>(
&'a mut self,
client: &'a mut C,
ids: &'a T1,
Expand Down Expand Up @@ -458,7 +453,7 @@ pub mod sync {
}
pub struct CommentsByPostIdStmt(crate::client::sync::Stmt);
impl CommentsByPostIdStmt {
pub fn bind<'a, C: GenericClient, T1: crate::client::ArraySql<Item = i32>>(
pub fn bind<'a, C: GenericClient, T1: crate::ArraySql<Item = i32>>(
&'a mut self,
client: &'a mut C,
ids: &'a T1,
Expand Down Expand Up @@ -551,7 +546,7 @@ pub mod async_ {
let stmt = self.stmt.prepare(self.client).await?;
let it = self
.client
.query_raw(stmt, crate::client::slice_iter(&self.params))
.query_raw(stmt, crate::slice_iter(&self.params))
.await?
.map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row))))
.into_stream();
Expand Down Expand Up @@ -603,7 +598,7 @@ pub mod async_ {
let stmt = self.stmt.prepare(self.client).await?;
let it = self
.client
.query_raw(stmt, crate::client::slice_iter(&self.params))
.query_raw(stmt, crate::slice_iter(&self.params))
.await?
.map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row))))
.into_stream();
Expand Down Expand Up @@ -655,7 +650,7 @@ pub mod async_ {
let stmt = self.stmt.prepare(self.client).await?;
let it = self
.client
.query_raw(stmt, crate::client::slice_iter(&self.params))
.query_raw(stmt, crate::slice_iter(&self.params))
.await?
.map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row))))
.into_stream();
Expand Down Expand Up @@ -710,7 +705,7 @@ pub mod async_ {
let stmt = self.stmt.prepare(self.client).await?;
let it = self
.client
.query_raw(stmt, crate::client::slice_iter(&self.params))
.query_raw(stmt, crate::slice_iter(&self.params))
.await?
.map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row))))
.into_stream();
Expand Down Expand Up @@ -746,12 +741,7 @@ pub mod async_ {
}
pub struct InsertUserStmt(crate::client::async_::Stmt);
impl InsertUserStmt {
pub async fn bind<
'a,
C: GenericClient,
T1: crate::client::StringSql,
T2: crate::client::StringSql,
>(
pub async fn bind<'a, C: GenericClient, T1: crate::StringSql, T2: crate::StringSql>(
&'a mut self,
client: &'a C,
name: &'a T1,
Expand All @@ -761,12 +751,7 @@ pub mod async_ {
client.execute(stmt, &[name, hair_color]).await
}
}
impl<
'a,
C: GenericClient + Send + Sync,
T1: crate::client::StringSql,
T2: crate::client::StringSql,
>
impl<'a, C: GenericClient + Send + Sync, T1: crate::StringSql, T2: crate::StringSql>
crate::client::async_::Params<
'a,
super::InsertUserParams<T1, T2>,
Expand Down Expand Up @@ -816,7 +801,7 @@ pub mod async_ {
}
pub struct PostByUserIdsStmt(crate::client::async_::Stmt);
impl PostByUserIdsStmt {
pub fn bind<'a, C: GenericClient, T1: crate::client::ArraySql<Item = i32>>(
pub fn bind<'a, C: GenericClient, T1: crate::ArraySql<Item = i32>>(
&'a mut self,
client: &'a C,
ids: &'a T1,
Expand Down Expand Up @@ -864,7 +849,7 @@ pub mod async_ {
}
pub struct CommentsByPostIdStmt(crate::client::async_::Stmt);
impl CommentsByPostIdStmt {
pub fn bind<'a, C: GenericClient, T1: crate::client::ArraySql<Item = i32>>(
pub fn bind<'a, C: GenericClient, T1: crate::ArraySql<Item = i32>>(
&'a mut self,
client: &'a C,
ids: &'a T1,
Expand Down
File renamed without changes.
File renamed without changes.
24 changes: 4 additions & 20 deletions crates/cornucopia/src/codegen.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use core::str;
use std::fmt::Write;

use codegen_template::code;

use crate::{
prepare_queries::{Preparation, PreparedField},
Expand Down Expand Up @@ -134,27 +131,14 @@ pub fn idx_char(idx: usize) -> String {
format!("T{idx}")
}

fn gen_lib() -> String {
code!($WARNING
#[allow(clippy::all, clippy::pedantic)]
#[allow(unused_variables)]
#[allow(unused_imports)]
#[allow(dead_code)]
pub mod types;
#[allow(clippy::all, clippy::pedantic)]
#[allow(unused_variables)]
#[allow(unused_imports)]
#[allow(dead_code)]
pub mod queries;
pub mod client;
)
}

pub(crate) fn gen(name: &str, preparation: Preparation, settings: CodegenSettings) -> Vfs {
let mut vfs = Vfs::empty();
let cargo = cargo::gen_cargo_file(name, &preparation.dependency_analysis, settings);
vfs.add("Cargo.toml", cargo);
vfs.add("src/lib.rs", gen_lib());
vfs.add(
"src/lib.rs",
client::gen_lib(&preparation.dependency_analysis),
);
let types = gen_type_modules(&preparation.types, &settings);
vfs.add("src/types.rs", types);
queries::gen_queries(&mut vfs, &preparation, settings);
Expand Down
99 changes: 52 additions & 47 deletions crates/cornucopia/src/codegen/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,48 @@ use crate::CodegenSettings;

use super::{vfs::Vfs, DependencyAnalysis, WARNING};

pub(crate) fn gen_lib(dependency_analysis: &DependencyAnalysis) -> String {
let mut w = code!($WARNING
#[allow(clippy::all, clippy::pedantic)]
#[allow(unused_variables)]
#[allow(unused_imports)]
#[allow(dead_code)]
pub mod types;
#[allow(clippy::all, clippy::pedantic)]
#[allow(unused_variables)]
#[allow(unused_imports)]
#[allow(dead_code)]
pub mod queries;
pub mod client;

mod array_iterator;
mod domain;
mod type_traits;
mod utils;

pub (crate) use utils::slice_iter;

pub use array_iterator::ArrayIterator;
pub use domain::{Domain, DomainArray};
pub use type_traits::{ArraySql, BytesSql, IterSql, StringSql};
);
if dependency_analysis.json {
code!(w => pub use type_traits::JsonSql; )
}

w
}

pub(crate) fn gen_clients(
vfs: &mut Vfs,
dependency_analysis: &DependencyAnalysis,
settings: &CodegenSettings,
) {
// Generate common client files
vfs.add("src/client/utils.rs", core_utils());
vfs.add("src/client/domain.rs", core_domain());
vfs.add("src/client/array_iterator.rs", core_array());
vfs.add(
"src/client/type_traits.rs",
core_type_traits(dependency_analysis),
);
// Generate common files
vfs.add("src/utils.rs", core_utils());
vfs.add("src/domain.rs", core_domain());
vfs.add("src/array_iterator.rs", core_array());
vfs.add("src/type_traits.rs", core_type_traits(dependency_analysis));
if settings.gen_sync {
vfs.add("src/client/sync.rs", sync());
}
Expand All @@ -29,48 +58,24 @@ pub(crate) fn gen_clients(
);
vfs.add("src/client/async_/deadpool.rs", async_deadpool());
}
vfs.add("src/client.rs", client(settings, dependency_analysis))
vfs.add("src/client.rs", client(settings))
}

pub fn client(settings: &CodegenSettings, dependency_analysis: &DependencyAnalysis) -> String {
let mut w = code!($WARNING
mod array_iterator;
mod domain;
mod type_traits;
mod utils;

pub (crate) use utils::slice_iter;

pub use array_iterator::ArrayIterator;
pub use domain::{Domain, DomainArray};
pub use type_traits::{ArraySql, BytesSql, IterSql, StringSql};
);

if dependency_analysis.json {
code!(w => pub use type_traits::JsonSql; )
}

pub fn client(settings: &CodegenSettings) -> String {
match (settings.gen_async, settings.gen_sync) {
(true, false) => {
code!(w =>
pub(crate) mod async_;
pub use async_::*;
)
}
(false, true) => {
code!(w =>
pub(crate) mod sync;
pub use sync::*;
)
}
_ => {
code!(w =>
pub mod sync;
pub mod async_;
)
}
};
w
(true, false) => code!(
pub(crate) mod async_;
pub use async_::*;
),
(false, true) => code!(
pub(crate) mod sync;
pub use sync::*;
),
_ => code!(
pub mod sync;
pub mod async_;
),
}
}

pub fn core_utils() -> String {
Expand Down
2 changes: 1 addition & 1 deletion crates/cornucopia/src/codegen/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ fn gen_row_query(w: &mut impl Write, row: &PreparedItem, ctx: &GenCtx) {
let stmt = self.stmt.prepare(self.client)$fn_await?;
let it = self
.client
.query_raw(stmt, crate::client::slice_iter(&self.params))
.query_raw(stmt, crate::slice_iter(&self.params))
$fn_await?
$raw_pre
.map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row))))
Expand Down
Loading

0 comments on commit 87714e8

Please sign in to comment.