浏览代码

Clean up use statements slightly and reformat.

Kestrel 7 月之前
父节点
当前提交
6203ed6c27

+ 7 - 9
microrm/src/cli.rs

@@ -102,8 +102,6 @@ impl clap::Subcommand for EmptyCommand {
 
 #[cfg(test)]
 mod tests {
-    use crate::schema::{RelationDomain, RelationRange, Relation};
-
     use super::{CLIObject, ClapInterface, InterfaceCustomization, ValueRole};
     use clap::Parser;
     use microrm::prelude::*;
@@ -128,7 +126,7 @@ mod tests {
         #[key]
         name: String,
 
-        txs: RelationRange<CTRelation>,
+        txs: microrm::RelationRange<CTRelation>,
     }
 
     #[derive(Entity)]
@@ -136,7 +134,7 @@ mod tests {
         #[key]
         name: String,
 
-        txs: RelationRange<ETRelation>,
+        txs: microrm::RelationRange<ETRelation>,
     }
 
     #[derive(Entity)]
@@ -145,15 +143,15 @@ mod tests {
         title: String,
         amount: isize,
 
-        customer: RelationDomain<CTRelation>,
-        employee: RelationDomain<ETRelation>,
+        customer: microrm::RelationDomain<CTRelation>,
+        employee: microrm::RelationDomain<ETRelation>,
     }
 
     #[derive(Database)]
     struct TransactionTestDB {
-        customers: IDMap<Customer>,
-        employees: IDMap<Employee>,
-        transactions: IDMap<Transaction>,
+        customers: microrm::IDMap<Customer>,
+        employees: microrm::IDMap<Employee>,
+        transactions: microrm::IDMap<Transaction>,
     }
 
     #[derive(clap::Parser, Debug)]

+ 17 - 25
microrm/src/cli/clap_interface.rs

@@ -1,8 +1,9 @@
-use microrm::{
+use crate::{
     prelude::*,
     schema::{
         datum::{ConcreteDatumList, Datum, DatumDiscriminator, DatumDiscriminatorRef},
-        entity::{Entity, EntityID, EntityPartList, EntityPartVisitor, EntityVisitor},
+        entity::{Entity, EntityID, EntityPart, EntityPartList, EntityPartVisitor, EntityVisitor},
+        relation::{Relation, RelationDomain, RelationMap, RelationRange},
     },
 };
 
@@ -21,7 +22,7 @@ fn add_keys<E: Entity, IC: InterfaceCustomization>(
     );
     impl<'a, IC: InterfaceCustomization, E: Entity> EntityPartVisitor for UVisitor<'a, IC, E> {
         type Entity = E;
-        fn visit<EP: microrm::schema::entity::EntityPart>(&mut self) {
+        fn visit<EP: EntityPart>(&mut self) {
             if !IC::has_value_for(EP::Entity::entity_name(), EP::part_name(), self.1) {
                 let arg = clap::Arg::new(EP::part_name())
                     .required(true)
@@ -52,7 +53,7 @@ fn collect_keys<E: Entity, IC: InterfaceCustomization>(
     );
     impl<'a, IC: InterfaceCustomization, E: Entity> EntityPartVisitor for UVisitor<'a, IC, E> {
         type Entity = E;
-        fn visit<EP: microrm::schema::entity::EntityPart>(&mut self) {
+        fn visit<EP: EntityPart>(&mut self) {
             if !IC::has_value_for(EP::Entity::entity_name(), EP::part_name(), self.2) {
                 self.1.push(EntityKey::UserInput(
                     self.0
@@ -138,7 +139,7 @@ impl<O: CLIObject> InterfaceVerb<O> {
         }
         impl<'l, IC: InterfaceCustomization, E: Entity> EntityPartVisitor for RelationFinder<'l, IC, E> {
             type Entity = E;
-            fn visit<EP: microrm::schema::entity::EntityPart>(&mut self) {
+            fn visit<EP: EntityPart>(&mut self) {
                 if EP::part_name() != self.subcommand {
                     return;
                 }
@@ -258,7 +259,7 @@ impl<'l, Error: CLIError, OE: Entity> Attacher<'l, Error, OE> {
 
 impl<'l, Error: CLIError, E: Entity> EntityPartVisitor for Attacher<'l, Error, E> {
     type Entity = E;
-    fn visit_datum<EP: microrm::schema::entity::EntityPart>(&mut self, datum: &EP::Datum) {
+    fn visit_datum<EP: EntityPart>(&mut self, datum: &EP::Datum) {
         if EP::part_name() != self.relation {
             return;
         }
@@ -281,16 +282,10 @@ impl<'l, Error: CLIError, OE: Entity> DatumDiscriminatorRef for Attacher<'l, Err
     fn visit_relation_map<E: Entity>(&mut self, map: &RelationMap<E>) {
         self.do_operation(map);
     }
-    fn visit_relation_range<R: microrm::schema::Relation>(
-        &mut self,
-        map: &microrm::schema::RelationRange<R>,
-    ) {
+    fn visit_relation_range<R: Relation>(&mut self, map: &RelationRange<R>) {
         self.do_operation(map);
     }
-    fn visit_relation_domain<R: microrm::schema::Relation>(
-        &mut self,
-        map: &microrm::schema::RelationDomain<R>,
-    ) {
+    fn visit_relation_domain<R: Relation>(&mut self, map: &RelationDomain<R>) {
         self.do_operation(map);
     }
 }
@@ -464,10 +459,7 @@ impl<O: CLIObject, IC: InterfaceCustomization> ClapInterface<O, IC> {
                 struct RelationFieldWalker<E: Entity>(std::marker::PhantomData<E>);
                 impl<E: Entity> EntityPartVisitor for RelationFieldWalker<E> {
                     type Entity = E;
-                    fn visit_datum<EP: microrm::schema::entity::EntityPart>(
-                        &mut self,
-                        datum: &EP::Datum,
-                    ) {
+                    fn visit_datum<EP: EntityPart>(&mut self, datum: &EP::Datum) {
                         struct Discriminator(&'static str);
 
                         impl DatumDiscriminatorRef for Discriminator {
@@ -483,15 +475,15 @@ impl<O: CLIObject, IC: InterfaceCustomization> ClapInterface<O, IC> {
                             fn visit_relation_map<E: Entity>(&mut self, amap: &RelationMap<E>) {
                                 inspect_ai(self.0, amap);
                             }
-                            fn visit_relation_domain<R: microrm::schema::Relation>(
+                            fn visit_relation_domain<R: Relation>(
                                 &mut self,
-                                adomain: &microrm::schema::RelationDomain<R>,
+                                adomain: &RelationDomain<R>,
                             ) {
                                 inspect_ai(self.0, adomain);
                             }
-                            fn visit_relation_range<R: microrm::schema::Relation>(
+                            fn visit_relation_range<R: Relation>(
                                 &mut self,
-                                arange: &microrm::schema::RelationRange<R>,
+                                arange: &RelationRange<R>,
                             ) {
                                 inspect_ai(self.0, arange);
                             }
@@ -519,7 +511,7 @@ impl<O: CLIObject, IC: InterfaceCustomization> ClapInterface<O, IC> {
         );
         impl<'l, IC: InterfaceCustomization, E: Entity> EntityPartVisitor for PartVisitor<'l, IC, E> {
             type Entity = E;
-            fn visit<EP: microrm::schema::entity::EntityPart>(&mut self) {
+            fn visit<EP: EntityPart>(&mut self) {
                 struct Discriminator<'l, IC: InterfaceCustomization>(
                     &'l mut Vec<clap::Command>,
                     &'static str,
@@ -539,13 +531,13 @@ impl<O: CLIObject, IC: InterfaceCustomization> ClapInterface<O, IC> {
                             ValueRole::AttachmentTarget,
                         ));
                     }
-                    fn visit_relation_domain<R: microrm::schema::Relation>(&mut self) {
+                    fn visit_relation_domain<R: Relation>(&mut self) {
                         self.0.push(add_keys::<R::Range, IC>(
                             clap::Command::new(self.1),
                             ValueRole::AttachmentTarget,
                         ));
                     }
-                    fn visit_relation_range<R: microrm::schema::Relation>(&mut self) {
+                    fn visit_relation_range<R: Relation>(&mut self) {
                         self.0.push(add_keys::<R::Domain, IC>(
                             clap::Command::new(self.1),
                             ValueRole::AttachmentTarget,

+ 3 - 3
microrm/src/lib.rs

@@ -90,16 +90,16 @@ pub mod db;
 mod query;
 pub mod schema;
 
+pub use schema::relation::{Relation, RelationDomain, RelationMap, RelationRange};
 pub use schema::{IDMap, Serialized, Stored};
-pub use schema::relation::{Relation, RelationMap, RelationDomain, RelationRange};
 
 #[cfg(feature = "clap")]
 pub mod cli;
 
 /// Module prelude with commonly-used traits that shouldn't have overlap with other crates.
 pub mod prelude {
-    pub use crate::query::{RelationInterface, Insertable, Queryable};
-    pub use crate::schema::{Database, relation::Relation};
+    pub use crate::query::{Insertable, Queryable, RelationInterface};
+    pub use crate::schema::{relation::Relation, Database};
     pub use microrm_macros::{Database, Entity};
 }
 

+ 11 - 4
microrm/src/query.rs

@@ -1,11 +1,15 @@
 use crate::{
-    DBResult, Error,
     db::{Connection, StatementContext, StatementRow, Transaction},
     schema::{
         datum::{Datum, QueryEquivalent, QueryEquivalentList},
-        entity::{Entity, EntityID, EntityPart, EntityPartList, EntityPartVisitor, helpers::check_relation},
-        relation::{RelationData, LocalSide}, Stored, IDMap,
+        entity::{
+            helpers::check_relation, Entity, EntityID, EntityPart, EntityPartList,
+            EntityPartVisitor,
+        },
+        relation::{LocalSide, RelationData},
+        IDMap, Stored,
     },
+    DBResult, Error,
 };
 
 use std::collections::HashMap;
@@ -681,7 +685,10 @@ pub trait Queryable: Clone {
     // Relationiation-following and joining methods
     // ----------------------------------------------------------------------
     /// Join based on an existing relation
-    fn join<AD: RelationInterface + Datum, EP: EntityPart<Entity = Self::EntityOutput, Datum = AD>>(
+    fn join<
+        AD: RelationInterface + Datum,
+        EP: EntityPart<Entity = Self::EntityOutput, Datum = AD>,
+    >(
         self,
         part: EP,
     ) -> impl Queryable<EntityOutput = AD::RemoteEntity, OutputContainer = Vec<Stored<AD::RemoteEntity>>>

+ 6 - 3
microrm/src/query/components.rs

@@ -3,11 +3,12 @@
 use crate::{
     db::{Connection, StatementContext},
     prelude::Queryable,
-    query::{RelationInterface, QueryPart},
+    query::{QueryPart, RelationInterface},
     schema::{
         datum::{Datum, DatumDiscriminator, DatumVisitor, QueryEquivalent, QueryEquivalentList},
         entity::{Entity, EntityPart, EntityPartList, EntityPartVisitor},
-        relation::{Relation, LocalSide}, Stored,
+        relation::{LocalSide, Relation},
+        Stored,
     },
 };
 
@@ -382,7 +383,9 @@ impl<
             .build()
             .attach(
                 QueryPart::Join,
-                format!("`{relation_name}` ON `{local_name}`.`id` = `{relation_name}`.`{local_field}`"),
+                format!(
+                    "`{relation_name}` ON `{local_name}`.`id` = `{relation_name}`.`{local_field}`"
+                ),
             )
             .attach(
                 QueryPart::Join,

+ 5 - 2
microrm/src/schema.rs

@@ -118,7 +118,6 @@ impl<T: Entity> PartialEq for Stored<T> {
 // Entity field types
 // ----------------------------------------------------------------------
 
-
 /// Stores an arbitrary Rust data type as serialized JSON in a string field.
 #[derive(Clone)]
 pub struct Serialized<T: serde::Serialize + serde::de::DeserializeOwned + Clone> {
@@ -179,7 +178,11 @@ impl<T: 'static + serde::Serialize + serde::de::DeserializeOwned + std::fmt::Deb
         stmt.transfer(json);
     }
 
-    fn build_from(rdata: relation::RelationData, stmt: &mut StatementRow, index: &mut i32) -> DBResult<Self>
+    fn build_from(
+        rdata: relation::RelationData,
+        stmt: &mut StatementRow,
+        index: &mut i32,
+    ) -> DBResult<Self>
     where
         Self: Sized,
     {

+ 2 - 2
microrm/src/schema/build.rs

@@ -156,7 +156,7 @@ pub(crate) fn collect_from_database<DB: Database>() -> DatabaseSchema {
                 PartType::RelationDomain {
                     table_name: relation_table_name,
                     range_name,
-                    injective
+                    injective,
                 } => {
                     let mut relation_table = TableInfo::new(relation_table_name.clone());
 
@@ -183,7 +183,7 @@ pub(crate) fn collect_from_database<DB: Database>() -> DatabaseSchema {
                 PartType::RelationRange {
                     table_name: relation_table_name,
                     domain_name,
-                    injective
+                    injective,
                 } => {
                     let mut relation_table = TableInfo::new(relation_table_name.clone());
 

+ 1 - 1
microrm/src/schema/collect.rs

@@ -1,7 +1,7 @@
 use std::collections::HashMap;
 
 use crate::schema::{
-    datum::{Datum,DatumDiscriminator},
+    datum::{Datum, DatumDiscriminator},
     entity::{Entity, EntityPart, EntityPartList, EntityPartVisitor, EntityVisitor},
     relation::Relation,
 };

+ 6 - 2
microrm/src/schema/datum.rs

@@ -1,7 +1,7 @@
 use crate::{
     db::{StatementContext, StatementRow},
     schema::entity::{Entity, EntityVisitor},
-    schema::relation::{RelationData, RelationDomain, RelationMap, RelationRange, Relation},
+    schema::relation::{Relation, RelationData, RelationDomain, RelationMap, RelationRange},
     DBResult,
 };
 
@@ -31,7 +31,11 @@ pub trait Datum: Clone + std::fmt::Debug {
     /// Bind this datum to a [`StatementContext`] at a given index.
     fn bind_to(&self, _stmt: &mut StatementContext, index: i32);
     /// Construct an instance of this datum from a table row.
-    fn build_from(_adata: RelationData, _stmt: &mut StatementRow, _index: &mut i32) -> DBResult<Self>
+    fn build_from(
+        _adata: RelationData,
+        _stmt: &mut StatementRow,
+        _index: &mut i32,
+    ) -> DBResult<Self>
     where
         Self: Sized,
     {

+ 1 - 1
microrm/src/schema/datum/datum_common.rs

@@ -1,6 +1,6 @@
 use crate::{
     db::{self, Bindable, StatementContext, StatementRow},
-    schema::{relation::RelationData, datum::Datum},
+    schema::{datum::Datum, relation::RelationData},
     DBResult, Error,
 };
 

+ 3 - 4
microrm/src/schema/relation.rs

@@ -1,14 +1,13 @@
 use crate::{
-    Error, DBResult,
-    db::{Connection, StatementRow, StatementContext},
+    db::{Connection, StatementContext, StatementRow},
+    query::RelationInterface,
     schema::{
         datum::{ConcreteDatum, Datum, DatumDiscriminator, DatumDiscriminatorRef},
         entity::{Entity, EntityVisitor},
     },
-    query::RelationInterface,
+    DBResult, Error,
 };
 
-
 /// Represents an arbitrary relation between two types of entities.
 ///
 /// In its most generic form, this represents a table that stores an arbitrary set of `(Domain::ID,

+ 40 - 24
microrm/src/schema/tests.rs

@@ -846,40 +846,56 @@ mod injective_test {
     fn single_connection() {
         let db = ABDB::open_path(":memory:").expect("couldn't open in-memory database");
 
-        let a1 = db.authors.insert_and_return(Author {
-            name: "Homer".to_string(),
-            works: Default::default(),
-        }).expect("couldn't insert author");
-
-        let a2 = db.authors.insert_and_return(Author {
-            name: "Virgil".to_string(),
-            works: Default::default(),
-        }).expect("couldn't insert author");
-
-        let b1_id = db.books.insert(Book {
-            title: "Odyssey".to_string(),
-            creator: Default::default(),
-        }).expect("couldn't insert book");
-        let b2_id = db.books.insert(Book {
-            title: "Aeneid".to_string(),
-            creator: Default::default(),
-        }).expect("couldn't insert book");
-
-        a1.works.connect_to(b1_id).expect("couldn't connect a1 and b1");
-        a2.works.connect_to(b2_id).expect("couldn't connect a2 and b2");
+        let a1 = db
+            .authors
+            .insert_and_return(Author {
+                name: "Homer".to_string(),
+                works: Default::default(),
+            })
+            .expect("couldn't insert author");
+
+        let a2 = db
+            .authors
+            .insert_and_return(Author {
+                name: "Virgil".to_string(),
+                works: Default::default(),
+            })
+            .expect("couldn't insert author");
+
+        let b1_id = db
+            .books
+            .insert(Book {
+                title: "Odyssey".to_string(),
+                creator: Default::default(),
+            })
+            .expect("couldn't insert book");
+        let b2_id = db
+            .books
+            .insert(Book {
+                title: "Aeneid".to_string(),
+                creator: Default::default(),
+            })
+            .expect("couldn't insert book");
+
+        a1.works
+            .connect_to(b1_id)
+            .expect("couldn't connect a1 and b1");
+        a2.works
+            .connect_to(b2_id)
+            .expect("couldn't connect a2 and b2");
 
         // we can't claim that Homer wrote the Aeneid because it's an injective relationship and
         // only one Author can claim the Book as their work
         match a1.works.connect_to(b2_id) {
             Err(microrm::Error::ConstraintViolation(_)) => {
                 // all good
-            },
+            }
             Err(_) => {
                 panic!("Unexpected error while testing injective connection");
-            },
+            }
             Ok(_) => {
                 panic!("Unexpected success while testing injective connection");
-            },
+            }
         }
     }
 }