Merge "aconfig: use bail! and ensure! where applicable" am: 7890fa3e84

Original change: https://android-review.googlesource.com/c/platform/build/+/2588765

Change-Id: Iaec315775f38b17825d4c98f077773a6023a1259
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Mårten Kongstad
2023-05-15 14:48:02 +00:00
committed by Automerger Merge Worker
2 changed files with 15 additions and 16 deletions

View File

@@ -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 @@ impl TryFrom<ProtoFlagDeclaration> for FlagDeclaration {
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 @@ impl FlagDeclarations {
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 @@ impl TryFrom<ProtoFlagValue> for FlagValue {
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 })

View File

@@ -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 @@ impl Cache {
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(),