Bladeren bron

Improve error reporting interface for autogenerated CLI commands.

Kestrel 7 maanden geleden
bovenliggende
commit
56988d8d29
2 gewijzigde bestanden met toevoegingen van 45 en 6 verwijderingen
  1. 2 2
      microrm/src/cli.rs
  2. 43 4
      microrm/src/cli/clap_interface.rs

+ 2 - 2
microrm/src/cli.rs

@@ -9,11 +9,11 @@ mod clap_interface;
 pub use clap_interface::ClapInterface;
 
 pub trait CLIError: From<Error> {
-    fn no_such_entity() -> Self;
+    fn no_such_entity(ename: &'static str, query: String) -> Self;
 }
 
 impl CLIError for Error {
-    fn no_such_entity() -> Self {
+    fn no_such_entity(ename: &'static str, query: String) -> Self {
         Error::EmptyResult
     }
 }

+ 43 - 4
microrm/src/cli/clap_interface.rs

@@ -176,7 +176,17 @@ impl<'l, Error: CLIError> Attacher<'l, Error> {
                 }
             }
             Ok(None) => {
-                self.err = Some(Error::no_such_entity());
+                self.err = Some(Error::no_such_entity(
+                    E::entity_name(),
+                    format!(
+                        "{}",
+                        self.remote_keys
+                            .iter()
+                            .cloned()
+                            .reduce(|a, b| format!("{},{}", a, b))
+                            .unwrap()
+                    ),
+                ));
             }
             Err(e) => {
                 self.err = Some(e.into());
@@ -248,7 +258,17 @@ impl<O: CLIObject> ClapInterface<O> {
                             .unwrap(),
                     )
                     .get()?
-                    .ok_or(<O::Error>::no_such_entity())?;
+                    .ok_or(<O::Error>::no_such_entity(
+                        O::entity_name(),
+                        format!(
+                            "{}",
+                            local_keys
+                                .iter()
+                                .cloned()
+                                .reduce(|a, b| format!("{},{}", a, b))
+                                .unwrap()
+                        ),
+                    ))?;
 
                 let mut attacher = Attacher {
                     do_attach: true,
@@ -283,7 +303,17 @@ impl<O: CLIObject> ClapInterface<O> {
                             .unwrap(),
                     )
                     .get()?
-                    .ok_or(<O::Error>::no_such_entity())?;
+                    .ok_or(<O::Error>::no_such_entity(
+                        O::entity_name(),
+                        format!(
+                            "{}",
+                            local_keys
+                                .iter()
+                                .cloned()
+                                .reduce(|a, b| format!("{},{}", a, b))
+                                .unwrap()
+                        ),
+                    ))?;
 
                 let mut attacher = Attacher {
                     do_attach: false,
@@ -313,7 +343,16 @@ impl<O: CLIObject> ClapInterface<O> {
                         UniqueList::<O>::build_equivalent(keys.iter().map(String::as_str)).unwrap(),
                     )
                     .get()?
-                    .ok_or(<O::Error>::no_such_entity())?;
+                    .ok_or(<O::Error>::no_such_entity(
+                        O::entity_name(),
+                        format!(
+                            "{}",
+                            keys.iter()
+                                .cloned()
+                                .reduce(|a, b| format!("{},{}", a, b))
+                                .unwrap()
+                        ),
+                    ))?;
                 println!("{:#?}", obj.as_ref());
 
                 fn inspect_ai<AI: AssocInterface>(name: &'static str, ai: &AI) {