Bladeren bron

rustfmt and clippy pass.

Kestrel 7 maanden geleden
bovenliggende
commit
34b4b675ab
4 gewijzigde bestanden met toevoegingen van 112 en 63 verwijderingen
  1. 1 3
      microrm-macros/src/index.rs
  2. 59 31
      microrm/src/cli.rs
  3. 25 13
      microrm/src/cli/eval.rs
  4. 27 16
      microrm/src/cli/parse.rs

+ 1 - 3
microrm-macros/src/index.rs

@@ -27,7 +27,5 @@ pub fn index_cols(tokens: TokenStream) -> TokenStream {
             .to_tokens(&mut part_list);
     }
 
-    let out = quote! { <#entity_path as ::microrm::schema::index::IndexPartList<#entity_path,(#part_list)>>::PartList }.into();
-
-    out
+    quote! { <#entity_path as ::microrm::schema::index::IndexPartList<#entity_path,(#part_list)>>::PartList }.into()
 }

+ 59 - 31
microrm/src/cli.rs

@@ -20,8 +20,8 @@ use crate::{
     Error,
 };
 
-mod parse;
 mod eval;
+mod parse;
 
 /// Trait used to expose errors from the autogenerated clap interface code. Implemented for
 /// [`Error`] but can be implemented for other error types if extra information needs to be passed
@@ -59,19 +59,35 @@ pub trait EntityInterface {
     type CustomCommand: clap::Subcommand + std::fmt::Debug;
 
     /// Invoked when a custom command is parsed.
-    fn run_custom(ctx: &Self::Context, cmd: Self::CustomCommand, query_ctx: impl Queryable<EntityOutput = Self::Entity>, insert_ctx: &impl Insertable<Self::Entity>) -> Result<(), Self::Error>;
+    fn run_custom(
+        ctx: &Self::Context,
+        cmd: Self::CustomCommand,
+        query_ctx: impl Queryable<EntityOutput = Self::Entity>,
+        insert_ctx: &impl Insertable<Self::Entity>,
+    ) -> Result<(), Self::Error>;
 
     /// Provide a summary of the entity, ideally a string with no newlines that can identify the
     /// entity at a glance.
-    fn summarize(_: &Self::Entity) -> Option<String> { None }
+    fn summarize(_: &Self::Entity) -> Option<String> {
+        None
+    }
 
     /// Provided to allow overriding the value passed to autogenerated commands, so that it is not
     /// requested on the command-line.
-    fn should_override(_entity: &'static str, _field: &'static str, _role: ValueRole) -> bool { false }
-    
+    fn should_override(_entity: &'static str, _field: &'static str, _role: ValueRole) -> bool {
+        false
+    }
+
     /// Invoked to request the concrete value for a value. Will only be invoked if
     /// should_override() for the same parameters previously returned true.
-    fn override_for(_ctx: &Self::Context, _entity: &'static str, _field: &'static str, _role: ValueRole) -> String { unreachable!() }
+    fn override_for(
+        _ctx: &Self::Context,
+        _entity: &'static str,
+        _field: &'static str,
+        _role: ValueRole,
+    ) -> String {
+        unreachable!()
+    }
 }
 
 /// Empty subcommand, used as a default value for CLIObjects that have no implemented additional
@@ -167,12 +183,9 @@ mod tests {
         transactions: microrm::IDMap<Transaction>,
     }
 
-
     #[derive(Debug, clap::Subcommand)]
     enum CCustom {
-        Create {
-            name: String,
-        }
+        Create { name: String },
     }
 
     #[derive(Debug)]
@@ -183,10 +196,18 @@ mod tests {
         type Context = ();
 
         type CustomCommand = CCustom;
-        fn run_custom(_ctx: &Self::Context, cmd: Self::CustomCommand, _query_ctx: impl Queryable<EntityOutput = Self::Entity>, insert_ctx: &impl Insertable<Self::Entity>) -> Result<(), Self::Error> {
+        fn run_custom(
+            _ctx: &Self::Context,
+            cmd: Self::CustomCommand,
+            _query_ctx: impl Queryable<EntityOutput = Self::Entity>,
+            insert_ctx: &impl Insertable<Self::Entity>,
+        ) -> Result<(), Self::Error> {
             match cmd {
                 CCustom::Create { name } => {
-                    insert_ctx.insert(Customer { name, txs: Default::default() })?;
+                    insert_ctx.insert(Customer {
+                        name,
+                        txs: Default::default(),
+                    })?;
                 },
             }
             Ok(())
@@ -195,9 +216,7 @@ mod tests {
 
     #[derive(Debug, clap::Subcommand)]
     enum ECustom {
-        Create {
-            name: String,
-        }
+        Create { name: String },
     }
 
     #[derive(Debug)]
@@ -208,10 +227,18 @@ mod tests {
         type Context = ();
 
         type CustomCommand = ECustom;
-        fn run_custom(_ctx: &Self::Context, cmd: Self::CustomCommand, _query_ctx: impl Queryable<EntityOutput = Self::Entity>, insert_ctx: &impl Insertable<Self::Entity>) -> Result<(), Self::Error> {
+        fn run_custom(
+            _ctx: &Self::Context,
+            cmd: Self::CustomCommand,
+            _query_ctx: impl Queryable<EntityOutput = Self::Entity>,
+            insert_ctx: &impl Insertable<Self::Entity>,
+        ) -> Result<(), Self::Error> {
             match cmd {
                 ECustom::Create { name } => {
-                    insert_ctx.insert(Employee { name, txs: Default::default() })?;
+                    insert_ctx.insert(Employee {
+                        name,
+                        txs: Default::default(),
+                    })?;
                 },
             }
             Ok(())
@@ -220,10 +247,7 @@ mod tests {
 
     #[derive(Debug, clap::Subcommand)]
     enum TCustom {
-        Create {
-            title: String,
-            amount: isize,
-        }
+        Create { title: String, amount: isize },
     }
 
     #[derive(Debug)]
@@ -234,10 +258,20 @@ mod tests {
         type Context = ();
 
         type CustomCommand = TCustom;
-        fn run_custom(_ctx: &Self::Context, cmd: Self::CustomCommand, _query_ctx: impl Queryable<EntityOutput = Self::Entity>, insert_ctx: &impl Insertable<Self::Entity>) -> Result<(), Self::Error> {
+        fn run_custom(
+            _ctx: &Self::Context,
+            cmd: Self::CustomCommand,
+            _query_ctx: impl Queryable<EntityOutput = Self::Entity>,
+            insert_ctx: &impl Insertable<Self::Entity>,
+        ) -> Result<(), Self::Error> {
             match cmd {
                 TCustom::Create { title, amount } => {
-                    insert_ctx.insert(Transaction { title, amount, customer: Default::default(), employee: Default::default() })?;
+                    insert_ctx.insert(Transaction {
+                        title,
+                        amount,
+                        customer: Default::default(),
+                        employee: Default::default(),
+                    })?;
                 },
             }
             Ok(())
@@ -292,10 +326,7 @@ mod tests {
                 .expect("couldn't count entries"),
             0
         );
-        run_cmd(
-            &db,
-            &["execname", "customer", "create", "a_key"],
-        );
+        run_cmd(&db, &["execname", "customer", "create", "a_key"]);
         assert_eq!(
             db.customers
                 .keyed("a_key")
@@ -303,10 +334,7 @@ mod tests {
                 .expect("couldn't count entries"),
             1
         );
-        run_cmd(
-            &db,
-            &["execname", "customer", "delete", "a_key"],
-        );
+        run_cmd(&db, &["execname", "customer", "delete", "a_key"]);
         assert_eq!(
             db.customers
                 .keyed("a_key")

+ 25 - 13
microrm/src/cli/eval.rs

@@ -1,11 +1,14 @@
-use super::{parse::{EntityKey, Verb}, CLIError, EntityInterface, Autogenerate};
+use super::{
+    parse::{EntityKey, Verb},
+    Autogenerate, CLIError, EntityInterface,
+};
 use crate::{
+    query::{Insertable, Queryable, RelationInterface},
     schema::{
-        datum::{Datum,DatumDiscriminatorRef,ConcreteDatumList},
-        entity::{Entity, EntityID, EntityPartList, EntityPartVisitor, EntityPart},
-        relation::{Relation, RelationMap, RelationDomain, RelationRange}
+        datum::{ConcreteDatumList, Datum, DatumDiscriminatorRef},
+        entity::{Entity, EntityID, EntityPart, EntityPartList, EntityPartVisitor},
+        relation::{Relation, RelationDomain, RelationMap, RelationRange},
     },
-    query::{Insertable,Queryable,RelationInterface},
 };
 
 // helper alias for later
@@ -15,7 +18,7 @@ impl<EI: EntityInterface> Autogenerate<EI> {
     /// Execute the parsed command.
     pub fn perform(
         self,
-        ctx: &EI::Context, 
+        ctx: &EI::Context,
         query_ctx: impl Queryable<EntityOutput = EI::Entity>,
         insert_ctx: &impl Insertable<EI::Entity>,
     ) -> Result<(), EI::Error> {
@@ -29,8 +32,10 @@ impl<EI: EntityInterface> Autogenerate<EI> {
                 let remote_keys = EntityKey::to_string_vec::<EI>(&remote_keys, ctx);
                 let outer_obj = query_ctx
                     .keyed(
-                        UniqueList::<EI::Entity>::build_equivalent(local_keys.iter().map(String::as_str))
-                            .unwrap(),
+                        UniqueList::<EI::Entity>::build_equivalent(
+                            local_keys.iter().map(String::as_str),
+                        )
+                        .unwrap(),
                     )
                     .get()?
                     .ok_or(<EI::Error>::no_such_entity(
@@ -60,7 +65,8 @@ impl<EI: EntityInterface> Autogenerate<EI> {
                 let keys = EntityKey::to_string_vec::<EI>(&keys, ctx);
                 query_ctx
                     .keyed(
-                        UniqueList::<EI::Entity>::build_equivalent(keys.iter().map(String::as_str)).unwrap(),
+                        UniqueList::<EI::Entity>::build_equivalent(keys.iter().map(String::as_str))
+                            .unwrap(),
                     )
                     .delete()?;
             },
@@ -73,8 +79,10 @@ impl<EI: EntityInterface> Autogenerate<EI> {
                 let remote_keys = EntityKey::to_string_vec::<EI>(&remote_keys, ctx);
                 let outer_obj = query_ctx
                     .keyed(
-                        UniqueList::<EI::Entity>::build_equivalent(local_keys.iter().map(String::as_str))
-                            .unwrap(),
+                        UniqueList::<EI::Entity>::build_equivalent(
+                            local_keys.iter().map(String::as_str),
+                        )
+                        .unwrap(),
                     )
                     .get()?
                     .ok_or(<EI::Error>::no_such_entity(
@@ -107,14 +115,18 @@ impl<EI: EntityInterface> Autogenerate<EI> {
                     query_ctx.clone().count()?
                 );
                 for obj in query_ctx.get()?.into_iter() {
-                    println!(" - {}", EI::summarize(&obj).unwrap_or_else(|| format!("{:?}", obj)));
+                    println!(
+                        " - {}",
+                        EI::summarize(&obj).unwrap_or_else(|| format!("{:?}", obj))
+                    );
                 }
             },
             Verb::Inspect(keys) => {
                 let keys = EntityKey::to_string_vec::<EI>(&keys, ctx);
                 let obj = query_ctx
                     .keyed(
-                        UniqueList::<EI::Entity>::build_equivalent(keys.iter().map(String::as_str)).unwrap(),
+                        UniqueList::<EI::Entity>::build_equivalent(keys.iter().map(String::as_str))
+                            .unwrap(),
                     )
                     .get()?
                     .ok_or(<EI::Error>::no_such_entity(

+ 27 - 16
microrm/src/cli/parse.rs

@@ -1,23 +1,34 @@
+use super::{Autogenerate, EntityInterface, ValueRole};
 use crate::schema::{
     datum::{Datum, DatumDiscriminator},
     entity::{Entity, EntityPart, EntityPartList, EntityPartVisitor, EntityVisitor},
     relation::Relation,
 };
-use super::{Autogenerate, EntityInterface, ValueRole};
-use clap::{FromArgMatches,Subcommand};
+use clap::{FromArgMatches, Subcommand};
 
 #[derive(Debug, Clone)]
 pub enum EntityKey {
-    Placeholder { entity: &'static str, field: &'static str, role: ValueRole },
+    Placeholder {
+        entity: &'static str,
+        field: &'static str,
+        role: ValueRole,
+    },
     UserInput(String),
 }
 
 impl EntityKey {
-    pub(crate) fn to_string_vec<EI: EntityInterface>(vec: &[Self], ctx: &EI::Context) -> Vec<String> {
+    pub(crate) fn to_string_vec<EI: EntityInterface>(
+        vec: &[Self],
+        ctx: &EI::Context,
+    ) -> Vec<String> {
         vec.iter()
             .map(|v| match v {
                 EntityKey::UserInput(s) => s.to_owned(),
-                EntityKey::Placeholder { entity, field, role } => EI::override_for(ctx, entity, field, *role),
+                EntityKey::Placeholder {
+                    entity,
+                    field,
+                    role,
+                } => EI::override_for(ctx, entity, field, *role),
             })
             .collect()
     }
@@ -34,7 +45,7 @@ fn add_keys<E: Entity, EI: EntityInterface>(
     struct UVisitor<'a, E: Entity, EI: EntityInterface>(
         &'a mut clap::Command,
         ValueRole,
-        std::marker::PhantomData<(E,EI)>,
+        std::marker::PhantomData<(E, EI)>,
     );
     impl<'a, E: Entity, EI: EntityInterface> EntityPartVisitor for UVisitor<'a, E, EI> {
         type Entity = E;
@@ -65,7 +76,7 @@ fn collect_keys<E: Entity, EI: EntityInterface>(
         &'a clap::ArgMatches,
         &'a mut Vec<EntityKey>,
         ValueRole,
-        std::marker::PhantomData<(E,EI)>,
+        std::marker::PhantomData<(E, EI)>,
     );
     impl<'a, E: Entity, EI: EntityInterface> EntityPartVisitor for UVisitor<'a, E, EI> {
         type Entity = E;
@@ -160,9 +171,7 @@ impl<EI: EntityInterface> Verb<EI> {
         Ok((local_keys, subcommand.into(), remote_keys))
     }
 
-    fn from_matches(
-        parent_matches: &clap::ArgMatches,
-    ) -> Result<Self, clap::Error> {
+    fn from_matches(parent_matches: &clap::ArgMatches) -> Result<Self, clap::Error> {
         let (subcommand, matches) = parent_matches
             .subcommand()
             .ok_or(clap::Error::new(clap::error::ErrorKind::MissingSubcommand))?;
@@ -176,9 +185,10 @@ impl<EI: EntityInterface> Verb<EI> {
                     remote_keys,
                 }
             },
-            "delete" => {
-                Self::Delete(collect_keys::<EI::Entity, EI>(matches, ValueRole::BaseTarget))
-            },
+            "delete" => Self::Delete(collect_keys::<EI::Entity, EI>(
+                matches,
+                ValueRole::BaseTarget,
+            )),
             "detach" => {
                 let (local_keys, relation, remote_keys) = Self::parse_attachment(matches)?;
                 Self::Detach {
@@ -188,9 +198,10 @@ impl<EI: EntityInterface> Verb<EI> {
                 }
             },
             "list" => Verb::ListAll,
-            "inspect" => {
-                Self::Inspect(collect_keys::<EI::Entity, EI>(matches, ValueRole::BaseTarget))
-            },
+            "inspect" => Self::Inspect(collect_keys::<EI::Entity, EI>(
+                matches,
+                ValueRole::BaseTarget,
+            )),
             cmd => {
                 if EI::CustomCommand::has_subcommand(cmd) {
                     Self::Custom(EI::CustomCommand::from_arg_matches(parent_matches)?)