aconfig: use bail! and ensure! where applicable

The Android Rust style guide recommends anyhow's bail! and ensure!
macros where possible. Update the code accordingly.

Bug: 279485059
Test: atest aconfig.test
Change-Id: I7db7cc2d8ec15e1d3450657ad000e879adb19e08
diff --git a/tools/aconfig/src/aconfig.rs b/tools/aconfig/src/aconfig.rs
index b09648d..b9fa324 100644
--- a/tools/aconfig/src/aconfig.rs
+++ b/tools/aconfig/src/aconfig.rs
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-use anyhow::{anyhow, Context, Error, Result};
+use anyhow::{anyhow, bail, Context, Error, Result};
 use protobuf::{Enum, EnumOrUnknown};
 use serde::{Deserialize, Serialize};
 
@@ -98,10 +98,10 @@
 
     fn try_from(proto: ProtoFlagDeclaration) -> Result<Self, Self::Error> {
         let Some(name) = proto.name else {
-            return Err(anyhow!("missing 'name' field"));
+            bail!("missing 'name' field");
         };
         let Some(description) = proto.description else {
-            return Err(anyhow!("missing 'description' field"));
+            bail!("missing 'description' field");
         };
         Ok(FlagDeclaration { name, description })
     }
@@ -118,7 +118,7 @@
         let proto: ProtoFlagDeclarations = crate::protos::try_from_text_proto(text_proto)
             .with_context(|| text_proto.to_owned())?;
         let Some(namespace) = proto.namespace else {
-            return Err(anyhow!("missing 'namespace' field"));
+            bail!("missing 'namespace' field");
         };
         let mut flags = vec![];
         for proto_flag in proto.flag.into_iter() {
@@ -154,17 +154,17 @@
 
     fn try_from(proto: ProtoFlagValue) -> Result<Self, Self::Error> {
         let Some(namespace) = proto.namespace else {
-            return Err(anyhow!("missing 'namespace' field"));
+            bail!("missing 'namespace' field");
         };
         let Some(name) = proto.name else {
-            return Err(anyhow!("missing 'name' field"));
+            bail!("missing 'name' field");
         };
         let Some(proto_state) = proto.state else {
-            return Err(anyhow!("missing 'state' field"));
+            bail!("missing 'state' field");
         };
         let state = proto_state.try_into()?;
         let Some(proto_permission) = proto.permission else {
-            return Err(anyhow!("missing 'permission' field"));
+            bail!("missing 'permission' field");
         };
         let permission = proto_permission.try_into()?;
         Ok(FlagValue { namespace, name, state, permission })
diff --git a/tools/aconfig/src/cache.rs b/tools/aconfig/src/cache.rs
index 3ecadc9..7b6edc5 100644
--- a/tools/aconfig/src/cache.rs
+++ b/tools/aconfig/src/cache.rs
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-use anyhow::{anyhow, bail, ensure, Result};
+use anyhow::{bail, ensure, Result};
 use serde::{Deserialize, Serialize};
 use std::io::{Read, Write};
 
@@ -69,13 +69,12 @@
         source: Source,
         declaration: FlagDeclaration,
     ) -> Result<()> {
-        if self.items.iter().any(|item| item.name == declaration.name) {
-            return Err(anyhow!(
-                "failed to declare flag {} from {}: flag already declared",
-                declaration.name,
-                source,
-            ));
-        }
+        ensure!(
+            self.items.iter().all(|item| item.name != declaration.name),
+            "failed to declare flag {} from {}: flag already declared",
+            declaration.name,
+            source
+        );
         self.items.push(Item {
             namespace: self.namespace.clone(),
             name: declaration.name.clone(),