Skip to content

Commit

Permalink
Use @generated in header to ignore generated files in rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
longsleep committed Aug 14, 2023
1 parent 39d3e95 commit 3134a6a
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 32 deletions.
16 changes: 10 additions & 6 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use syn::Item::Macro;

use crate::{code, GenerationConfig, TableOptions};

pub const FILE_SIGNATURE: &str = "/* This file is generated and managed by dsync */";
pub const FILE_SIGNATURE: &str = "/* @generated and managed by dsync */";

// TODO: handle postgres array types
// TODO: handle postgres tuple/record types
Expand Down Expand Up @@ -150,7 +150,7 @@ fn handle_table_macro(macro_item: syn::ItemMacro, _config: &GenerationConfig) ->
let mut table_columns: Vec<ParsedColumnMacro> = vec![];

let mut skip_until_semicolon = false;

for item in macro_item.mac.tokens.into_iter() {
if skip_until_semicolon {
if let proc_macro2::TokenTree::Punct(punct) = item {
Expand All @@ -160,15 +160,15 @@ fn handle_table_macro(macro_item: syn::ItemMacro, _config: &GenerationConfig) ->
}
continue;
}

match &item {
proc_macro2::TokenTree::Ident(ident) => {
// skip any "use" statements
if ident.to_string().eq("use") {
skip_until_semicolon = true;
continue;
}

table_name_ident = Some(ident.clone());
}
proc_macro2::TokenTree::Group(group) => {
Expand Down Expand Up @@ -229,7 +229,11 @@ fn handle_table_macro(macro_item: syn::ItemMacro, _config: &GenerationConfig) ->
}
}

if column_name.is_some() || column_type.is_some() || column_nullable || column_unsigned {
if column_name.is_some()
|| column_type.is_some()
|| column_nullable
|| column_unsigned
{
// looks like a column was in the middle of being parsed, let's panic!
panic!(
"Unsupported schema format! (It seems a column was partially defined)"
Expand Down Expand Up @@ -270,7 +274,7 @@ fn handle_table_macro(macro_item: syn::ItemMacro, _config: &GenerationConfig) ->
//
// The docs page for sql_types is comprehensive but it hides some alias types like Int4, Float8, etc.:
// https://docs.rs/diesel/latest/diesel/sql_types/index.html
fn schema_type_to_rust_type(schema_type: String) -> String {
fn schema_type_to_rust_type(schema_type: String) -> String {
match schema_type.to_lowercase().as_str() {
"unsigned" => panic!("Unsigned types are not yet supported, please open an issue if you need this feature!"), // TODO: deal with this later
"inet" => panic!("Unsigned types are not yet supported, please open an issue if you need this feature!"), // TODO: deal with this later
Expand Down
6 changes: 3 additions & 3 deletions test/autogenerated_all/models/todos/generated.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* This file is generated and managed by dsync */
/* @generated and managed by dsync */

use crate::diesel::*;
use crate::schema::*;
use diesel::QueryResult;
use serde::{Deserialize, Serialize};

use serde::{Deserialize, Serialize};

type Connection = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

Expand All @@ -16,7 +16,7 @@ pub struct Todo {
}


#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset)]
#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodo {
pub created_at: Option<chrono::NaiveDateTime>,
Expand Down
6 changes: 3 additions & 3 deletions test/autogenerated_attributes/models/todos/generated.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* This file is generated and managed by dsync */
/* @generated and managed by dsync */

use crate::diesel::*;
use crate::schema::*;
use diesel::QueryResult;
use serde::{Deserialize, Serialize};

use serde::{Deserialize, Serialize};

type Connection = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

Expand All @@ -21,7 +21,7 @@ pub struct CreateTodo {
pub id: i32,
}

#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset)]
#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodo {
pub created_at: Option<chrono::NaiveDateTime>,
Expand Down
6 changes: 3 additions & 3 deletions test/autogenerated_primary_keys/models/todos/generated.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* This file is generated and managed by dsync */
/* @generated and managed by dsync */

use crate::diesel::*;
use crate::schema::*;
use diesel::QueryResult;
use serde::{Deserialize, Serialize};

use serde::{Deserialize, Serialize};

type Connection = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

Expand All @@ -21,7 +21,7 @@ pub struct CreateTodo {
pub text: String,
}

#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset)]
#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodo {
pub text: Option<String>,
Expand Down
6 changes: 3 additions & 3 deletions test/cleanup_generated_content/models/todos/generated.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* This file is generated and managed by dsync */
/* @generated and managed by dsync */

use crate::diesel::*;
use crate::schema::*;
use diesel::QueryResult;
use serde::{Deserialize, Serialize};

use serde::{Deserialize, Serialize};

type Connection = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

Expand All @@ -26,7 +26,7 @@ pub struct CreateTodo {
pub completed: bool,
}

#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset)]
#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodo {
pub text: Option<String>,
Expand Down
4 changes: 2 additions & 2 deletions test/manual_primary_keys/models/todos/generated.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* This file is generated and managed by dsync */
/* @generated and managed by dsync */

use crate::diesel::*;
use crate::schema::*;
use diesel::QueryResult;
use serde::{Deserialize, Serialize};

use serde::{Deserialize, Serialize};

type Connection = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

Expand Down
6 changes: 3 additions & 3 deletions test/multiple_primary_keys/models/users/generated.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* This file is generated and managed by dsync */
/* @generated and managed by dsync */

use crate::diesel::*;
use crate::schema::*;
use diesel::QueryResult;
use serde::{Deserialize, Serialize};

use serde::{Deserialize, Serialize};

type Connection = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

Expand All @@ -24,7 +24,7 @@ pub struct CreateUser {
pub secret: String,
}

#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset)]
#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset, Default)]
#[diesel(table_name=users)]
pub struct UpdateUser {
pub secret: Option<String>,
Expand Down
6 changes: 3 additions & 3 deletions test/simple_table/models/todos/generated.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* This file is generated and managed by dsync */
/* @generated and managed by dsync */

use crate::diesel::*;
use crate::schema::*;
use diesel::QueryResult;
use serde::{Deserialize, Serialize};

use serde::{Deserialize, Serialize};

type Connection = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

Expand All @@ -27,7 +27,7 @@ pub struct CreateTodo {
pub completed: bool,
}

#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset)]
#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodo {
pub unsigned: Option<u32>,
Expand Down
6 changes: 3 additions & 3 deletions test/simple_table_async/models/todos/generated.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* This file is generated and managed by dsync */
/* @generated and managed by dsync */

use crate::diesel::*;
use crate::schema::*;
use diesel::QueryResult;

use serde::{Deserialize, Serialize};
use diesel_async::RunQueryDsl;


type Connection = diesel_async::pooled_connection::deadpool::Object<diesel_async::AsyncPgConnection>;

#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset, Selectable)]
Expand All @@ -28,7 +28,7 @@ pub struct CreateTodo {
pub completed: bool,
}

#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset)]
#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodo {
pub unsigned: Option<u32>,
Expand Down
6 changes: 3 additions & 3 deletions test/use_statements/models/fang_tasks/generated.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* This file is generated and managed by dsync */
/* @generated and managed by dsync */

use crate::diesel::*;
use crate::schema::*;
use diesel::QueryResult;
use serde::{Deserialize, Serialize};

use serde::{Deserialize, Serialize};

type Connection = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

Expand Down Expand Up @@ -38,7 +38,7 @@ pub struct CreateFangTask {
pub updated_at: chrono::DateTime<chrono::Utc>,
}

#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset)]
#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset, Default)]
#[diesel(table_name=fang_tasks)]
pub struct UpdateFangTask {
pub metadata: Option<serde_json::Value>,
Expand Down

0 comments on commit 3134a6a

Please sign in to comment.