Skip to content

Commit

Permalink
chore: more fixes, more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
m4tx committed Jan 7, 2025
1 parent de82f60 commit d082bb1
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 26 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

26 changes: 7 additions & 19 deletions flareon-cli/src/migration_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl MigrationGenerator {
migration.toposort_operations();
migration
.dependencies
.extend(migration.get_foreign_key_dependencies(&self.crate_name));
.extend(migration.get_foreign_key_dependencies());

Ok(Some(migration))
}
Expand Down Expand Up @@ -527,7 +527,7 @@ pub struct SourceFile {

impl SourceFile {
#[must_use]
pub fn new(path: PathBuf, content: syn::File) -> Self {
fn new(path: PathBuf, content: syn::File) -> Self {
assert!(
path.is_relative(),
"path must be relative to the src directory"
Expand Down Expand Up @@ -666,7 +666,7 @@ pub struct GeneratedMigration {
}

impl GeneratedMigration {
fn get_foreign_key_dependencies(&self, crate_name: &str) -> Vec<DynDependency> {
fn get_foreign_key_dependencies(&self) -> Vec<DynDependency> {
let create_ops = self.get_create_ops_map();
let ops_adding_foreign_keys = self.get_ops_adding_foreign_keys();

Expand Down Expand Up @@ -977,20 +977,8 @@ impl Repr for DynOperation {
fn repr(&self) -> TokenStream {
match self {
Self::CreateModel {
table_name,
model_ty,
fields,
..
table_name, fields, ..
} => {
let model_name = match model_ty {
syn::Type::Path(syn::TypePath { path, .. }) => path
.segments
.last()
.expect("TypePath must have at least one segment")
.ident
.to_string(),
_ => unreachable!("model_ty is expected to be a TypePath"),
};
let fields = fields.iter().map(Repr::repr).collect::<Vec<_>>();
quote! {
::flareon::db::migrations::Operation::create_model()
Expand Down Expand Up @@ -1265,7 +1253,7 @@ mod tests {
}],
};

let external_dependencies = migration.get_foreign_key_dependencies("my_crate");
let external_dependencies = migration.get_foreign_key_dependencies();
assert!(external_dependencies.is_empty());
}

Expand All @@ -1292,7 +1280,7 @@ mod tests {
}],
};

let external_dependencies = migration.get_foreign_key_dependencies("my_crate");
let external_dependencies = migration.get_foreign_key_dependencies();
assert_eq!(external_dependencies.len(), 1);
assert_eq!(
external_dependencies[0],
Expand Down Expand Up @@ -1342,7 +1330,7 @@ mod tests {
],
};

let external_dependencies = migration.get_foreign_key_dependencies("my_crate");
let external_dependencies = migration.get_foreign_key_dependencies();
assert_eq!(external_dependencies.len(), 2);
assert!(external_dependencies.contains(&DynDependency::Model {
model_type: parse_quote!(my_crate::Table2),
Expand Down
1 change: 0 additions & 1 deletion flareon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ time.workspace = true
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
tower = { workspace = true, features = ["util"] }
tower-sessions = { workspace = true, features = ["memory-store"] }
env_logger = "0.11.5"

[dev-dependencies]
async-stream.workspace = true
Expand Down
3 changes: 1 addition & 2 deletions flareon/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ use subtle::ConstantTimeEq;
use thiserror::Error;

use crate::config::SecretKey;
use crate::db::DbValue;
#[cfg(feature = "db")]
use crate::db::{ColumnType, DatabaseField, FromDbValue, SqlxValueRef, ToDbValue};
use crate::db::{ColumnType, DatabaseField, DbValue, FromDbValue, SqlxValueRef, ToDbValue};
use crate::request::{Request, RequestExt};

#[derive(Debug, Error)]
Expand Down
10 changes: 8 additions & 2 deletions flareon/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl DatabaseError {
}
}

/// An alias for [`Result`] that uses [`DatabaseError`] as the error type.
pub type Result<T> = std::result::Result<T, DatabaseError>;

/// A model trait for database models.
Expand Down Expand Up @@ -211,6 +212,8 @@ impl Column {
}
}

/// A marker trait that denotes that a type can be used as a primary key in a
/// database.
pub trait PrimaryKey: DatabaseField + Clone {}

/// A row structure that holds the data of a single row retrieved from the
Expand Down Expand Up @@ -913,7 +916,7 @@ pub struct RowsNum(pub u64);
/// ```
/// use flareon::db::{model, Auto, Model};
/// # use flareon::db::migrations::{Field, Operation};
/// # use flareon::db::{Database, Identifier};
/// # use flareon::db::{Database, Identifier, DatabaseField};
/// # use flareon::Result;
///
/// #[model]
Expand All @@ -925,7 +928,7 @@ pub struct RowsNum(pub u64);
/// # async fn main() -> Result<()> {
///
/// # const OPERATION: Operation = Operation::create_model()
/// # .table_name(Identifier::new("todoapp__my_model"))
/// # .table_name(Identifier::new("my_model"))
/// # .fields(&[
/// # Field::new(Identifier::new("id"), <i32 as DatabaseField>::TYPE)
/// # .primary_key()
Expand Down Expand Up @@ -955,6 +958,7 @@ impl<T> Auto<T> {
/// Creates a new `Auto` instance that is automatically generated by the
/// database.
#[must_use]
#[allow(clippy::self_named_constructors)]
pub const fn auto() -> Self {
Self::Auto
}
Expand Down Expand Up @@ -1025,6 +1029,8 @@ impl<const LIMIT: u32> PartialEq<LimitedString<LIMIT>> for String {
}
}

/// An error returned by [`LimitedString::new`] when the string is longer than
/// the specified limit.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Error)]
#[error("string is too long ({length} > {LIMIT})")]
pub struct NewLimitedStringError<const LIMIT: u32> {
Expand Down
2 changes: 1 addition & 1 deletion flareon/src/db/relations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl From<ForeignKeyOnDeletePolicy> for sea_query::ForeignKeyAction {
}
}

/// A foreign key on delete constraint.
/// A foreign key on update constraint.
///
/// This is used to define the behavior of a foreign key when the referenced row
/// is updated.
Expand Down

0 comments on commit d082bb1

Please sign in to comment.