Skip to content

Commit b40c0b1

Browse files
authored
chore: cleaning up examples of is_some() + unwrap() (#362)
1 parent 1ae77e1 commit b40c0b1

12 files changed

Lines changed: 107 additions & 105 deletions

File tree

crates/admin-cli/src/extension_service/cmds.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,24 @@ pub async fn handle_update(
9393
) -> CarbideCliResult<()> {
9494
let is_json = output_format == OutputFormat::Json;
9595

96-
let credential =
97-
if args.username.is_some() || args.password.is_some() || args.registry_url.is_some() {
98-
if args.username.is_none() || args.password.is_none() || args.registry_url.is_none() {
99-
return Err(CarbideCliError::GenericError(
100-
"All of username, password and registry URL are required to create credential"
101-
.to_string(),
102-
));
103-
}
104-
96+
let credential = match (args.username, args.password, args.registry_url) {
97+
(Some(username), Some(password), Some(registry_url)) => {
10598
Some(::rpc::forge::DpuExtensionServiceCredential {
106-
registry_url: args.registry_url.unwrap(),
99+
registry_url,
107100
r#type: Some(Type::UsernamePassword(rpc::forge::UsernamePassword {
108-
username: args.username.unwrap(),
109-
password: args.password.unwrap(),
101+
username,
102+
password,
110103
})),
111104
})
112-
} else {
113-
None
114-
};
105+
}
106+
(None, None, None) => None,
107+
_ => {
108+
return Err(CarbideCliError::GenericError(
109+
"All of username, password and registry URL are required to create credential"
110+
.to_string(),
111+
));
112+
}
113+
};
115114

116115
let observability = if let Some(r) = args.observability {
117116
serde_json::from_str(&r)?

crates/agent/src/duppet/sync.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,9 @@ pub fn maybe_update_file_permissions(
315315
) -> io::Result<bool> {
316316
// If not configured to manage the mode,
317317
// just return nothing was changed.
318-
if expected_mode.is_none() {
318+
let Some(expected_mode) = expected_mode else {
319319
return Ok(false);
320-
}
321-
322-
let expected_mode = expected_mode.unwrap();
320+
};
323321
let file = File::open(path)?;
324322
let metadata = file.metadata()?;
325323
let mut perms = metadata.permissions();

crates/agent/src/ethernet_virtualization.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,10 +1420,13 @@ fn parse_fdb(fdb_json: &str) -> eyre::Result<HashMap<u32, Vec<Fdb>>> {
14201420
let all_fdb: Vec<Fdb> = serde_json::from_str(fdb_json)?;
14211421
let mut out: HashMap<u32, Vec<Fdb>> = HashMap::new();
14221422
for fdb in all_fdb.into_iter() {
1423-
if fdb.vlan.is_none() || fdb.state == "permanent" {
1423+
let Some(vlan) = fdb.vlan else {
1424+
continue;
1425+
};
1426+
if fdb.state == "permanent" {
14241427
continue;
14251428
}
1426-
out.entry(fdb.vlan.unwrap())
1429+
out.entry(vlan)
14271430
.and_modify(|v| v.push(fdb.clone()))
14281431
.or_insert_with(|| vec![fdb]);
14291432
}

crates/api-db/src/instance.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,37 @@ pub async fn find_ids(
6464
let mut builder = sqlx::QueryBuilder::new("SELECT id FROM instances WHERE TRUE "); // The TRUE will be optimized away.
6565

6666
if let Some(label) = filter.label {
67-
if label.key.is_empty() && label.value.is_some() {
68-
builder.push(
69-
" AND EXISTS (
67+
match (label.key.is_empty(), label.value) {
68+
// Label key is empty, label value is set.
69+
(true, Some(value)) => {
70+
builder.push(
71+
" AND EXISTS (
7072
SELECT 1
7173
FROM jsonb_each_text(labels) AS kv
7274
WHERE kv.value = ",
73-
);
74-
builder.push_bind(label.value.unwrap());
75-
builder.push(")");
76-
} else if label.key.is_empty() && label.value.is_none() {
77-
return Err(DatabaseError::InvalidArgument(
78-
"finding instances based on label needs either key or a value.".to_string(),
79-
));
80-
} else if !label.key.is_empty() && label.value.is_none() {
81-
builder.push(" AND labels ->> ");
82-
builder.push_bind(label.key);
83-
builder.push(" IS NOT NULL");
84-
} else if !label.key.is_empty() && label.value.is_some() {
85-
builder.push(" AND labels ->> ");
86-
builder.push_bind(label.key);
87-
builder.push(" = ");
88-
builder.push_bind(label.value.unwrap());
75+
);
76+
builder.push_bind(value);
77+
builder.push(")");
78+
}
79+
// Label key is empty, label value is not set.
80+
(true, None) => {
81+
return Err(DatabaseError::InvalidArgument(
82+
"finding instances based on label needs either key or a value.".to_string(),
83+
));
84+
}
85+
// Label key is not empty, label value is not set.
86+
(false, None) => {
87+
builder.push(" AND labels ->> ");
88+
builder.push_bind(label.key);
89+
builder.push(" IS NOT NULL");
90+
}
91+
// Label key is not empty, label value is set.
92+
(false, Some(value)) => {
93+
builder.push(" AND labels ->> ");
94+
builder.push_bind(label.key);
95+
builder.push(" = ");
96+
builder.push_bind(value);
97+
}
8998
}
9099
}
91100

crates/api-db/src/machine.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ pub async fn get_or_create(
9292
) -> DatabaseResult<Machine> {
9393
let existing_machine =
9494
find_one(&mut *txn, stable_machine_id, MachineSearchConfig::default()).await?;
95-
if interface.machine_id.is_some() {
96-
let machine_id = interface.machine_id.as_ref().unwrap();
95+
if let Some(machine_id) = interface.machine_id.as_ref() {
9796
if machine_id != stable_machine_id {
9897
return Err(DatabaseError::internal(format!(
9998
"Database inconsistency: MachineId {} on interface {} does not match stable machine ID {} which now uses this interface",
@@ -1628,10 +1627,9 @@ pub async fn apply_agent_upgrade_policy(
16281627
None => Ok(false),
16291628
Some(obs) => {
16301629
let carbide_api_version = carbide_version::v!(build_version);
1631-
if obs.agent_version.is_none() {
1630+
let Some(agent_version) = obs.agent_version.clone() else {
16321631
return Ok(false);
1633-
}
1634-
let agent_version = obs.agent_version.as_ref().cloned().unwrap();
1632+
};
16351633
let should_upgrade = policy.should_upgrade(&agent_version, carbide_api_version);
16361634
if should_upgrade != machine.needs_agent_upgrade() {
16371635
set_dpu_agent_upgrade_requested(

crates/api-db/src/vpc.rs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -107,31 +107,40 @@ pub async fn find_ids(
107107
if has_filter {
108108
builder.push(" AND ");
109109
}
110-
if label.key.is_empty() && label.value.is_some() {
111-
builder.push(
112-
" EXISTS (
110+
match (label.key.is_empty(), label.value) {
111+
// Label key is empty, label value is set.
112+
(true, Some(value)) => {
113+
builder.push(
114+
" EXISTS (
113115
SELECT 1
114116
FROM jsonb_each_text(labels) AS kv
115117
WHERE kv.value = ",
116-
);
117-
builder.push_bind(label.value.unwrap());
118-
builder.push(")");
119-
has_filter = true;
120-
} else if label.key.is_empty() && label.value.is_none() {
121-
return Err(DatabaseError::InvalidArgument(
122-
"finding VPCs based on label needs either key or a value.".to_string(),
123-
));
124-
} else if !label.key.is_empty() && label.value.is_none() {
125-
builder.push(" labels ->> ");
126-
builder.push_bind(label.key);
127-
builder.push(" IS NOT NULL");
128-
has_filter = true;
129-
} else if !label.key.is_empty() && label.value.is_some() {
130-
builder.push(" labels ->> ");
131-
builder.push_bind(label.key);
132-
builder.push(" = ");
133-
builder.push_bind(label.value.unwrap());
134-
has_filter = true;
118+
);
119+
builder.push_bind(value);
120+
builder.push(")");
121+
has_filter = true;
122+
}
123+
// Label key is empty, label value is not set.
124+
(true, None) => {
125+
return Err(DatabaseError::InvalidArgument(
126+
"finding VPCs based on label needs either key or a value.".to_string(),
127+
));
128+
}
129+
// Label key is not empty, label value is not set.
130+
(false, None) => {
131+
builder.push(" labels ->> ");
132+
builder.push_bind(label.key);
133+
builder.push(" IS NOT NULL");
134+
has_filter = true;
135+
}
136+
// Label key is not empty, label value is set.
137+
(false, Some(value)) => {
138+
builder.push(" labels ->> ");
139+
builder.push_bind(label.key);
140+
builder.push(" = ");
141+
builder.push_bind(value);
142+
has_filter = true;
143+
}
135144
}
136145
}
137146
if has_filter {

crates/api/src/cfg/file.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -654,32 +654,27 @@ impl CarbideConfig {
654654
}
655655

656656
pub fn is_dpa_enabled(&self) -> bool {
657-
if self.dpa_config.is_none() {
657+
let Some(conf) = &self.dpa_config else {
658658
return false;
659-
}
660-
661-
let conf = self.dpa_config.clone().unwrap();
659+
};
662660

663661
conf.enabled
664662
}
665663

666664
pub fn get_dpa_subnet_ip(&self) -> Result<Ipv4Addr, eyre::Report> {
667-
if self.dpa_config.is_none() {
665+
let Some(conf) = &self.dpa_config else {
668666
tracing::error!("get_dpa_subnet_ip: DPA config missing");
669667
return Err(eyre::eyre!("get_dpa_subnet_ip: DPA config missing"));
670-
}
668+
};
671669

672-
let conf = self.dpa_config.clone().unwrap();
673670
Ok(conf.subnet_ip)
674671
}
675672

676673
pub fn get_dpa_subnet_mask(&self) -> Result<i32, eyre::Report> {
677-
if self.dpa_config.is_none() {
674+
let Some(conf) = &self.dpa_config else {
678675
tracing::error!("get_dpa_subnet_mask: DPA config missing");
679676
return Err(eyre::eyre!("get_dpa_subnet_mask: DPA config missing"));
680-
}
681-
682-
let conf = self.dpa_config.clone().unwrap();
677+
};
683678

684679
Ok(conf.subnet_mask)
685680
}

crates/api/src/handlers/dpa.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,8 @@ async fn process_mlx_observation(
502502
firmware_report: None,
503503
});
504504

505-
if obs.lock_status.is_some() {
506-
let ls = match DpaLockMode::try_from(obs.lock_status.unwrap()) {
505+
if let Some(lock_status) = obs.lock_status {
506+
let ls = match DpaLockMode::try_from(lock_status) {
507507
Ok(ls) => ls,
508508
Err(e) => {
509509
tracing::error!("process_mlx_observation Error from LockStatus::try_from {e}");
@@ -572,11 +572,9 @@ pub(crate) async fn publish_mlx_device_report(
572572
);
573573

574574
// Without a machine_id, we can't create dpa interfaces
575-
if report.machine_id.is_some() {
575+
if let Some(machine_id) = report.machine_id {
576576
let mut spx_nics: i32 = 0;
577577

578-
let machine_id = report.machine_id.unwrap();
579-
580578
// Go over each of the MlxDeviceInfo reports from the
581579
// MlxDeviceReport. Each MlxDeviceInfo corresponds to
582580
// an individual device reported by `mlxfwmanager`, with
@@ -587,8 +585,9 @@ pub(crate) async fn publish_mlx_device_report(
587585
// Change this to base device detection using part numbers rather
588586
// than device description.
589587
// XXX TODO XXX
590-
if device_info.device_description.is_some() && device_info.base_mac.is_some() {
591-
let descr = device_info.device_description.clone().unwrap();
588+
if let (Some(descr), Some(mac_address)) =
589+
(device_info.device_description.clone(), device_info.base_mac)
590+
{
592591
if descr.contains("SuperNIC") {
593592
spx_nics += 1;
594593

@@ -597,7 +596,6 @@ pub(crate) async fn publish_mlx_device_report(
597596
// the TODO(chet) below about create_internal behavior.
598597
// I think we can refactor all of this so these hoops
599598
// aren't needed.
600-
let mac_address = device_info.base_mac.unwrap();
601599
let pci_name = device_info.pci_name.clone();
602600
let device_type = device_info.device_type.clone();
603601
let dpa_info = NewDpaInterface {

crates/measured-boot/src/site.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,7 @@ impl MachineAttestationSummaryList {
180180
.iter()
181181
.map(|e| MachineAttestationSummaryPb {
182182
machine_id: e.machine_id.to_string(),
183-
bundle_id: if e.bundle_id.is_none() {
184-
None
185-
} else {
186-
Some(e.bundle_id.unwrap())
187-
},
183+
bundle_id: e.bundle_id,
188184
profile_name: e.profile_name.clone(),
189185
ts: Some(e.ts.into()),
190186
})
@@ -204,14 +200,13 @@ impl MachineAttestationSummaryList {
204200
})?,
205201
bundle_id: pb.bundle_id,
206202
profile_name: pb.profile_name.clone(),
207-
ts: if pb.ts.is_none() {
208-
chrono::DateTime::<Utc>::default()
209-
} else {
210-
chrono::DateTime::<Utc>::try_from(pb.ts.unwrap()).map_err(|err| {
203+
ts: match pb.ts {
204+
Some(ts) => chrono::DateTime::<Utc>::try_from(ts).map_err(|err| {
211205
super::Error::RpcConversion(format!(
212206
"Could not deserialize ListAttestationSummaryResponse(timestamp): {err}"
213207
))
214-
})?
208+
})?,
209+
None => chrono::DateTime::<Utc>::default(),
215210
},
216211
});
217212
}

crates/scout/src/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,10 @@ async fn handle_mlxreport_action(
441441
machine_id: &MachineId,
442442
data: Option<ForgeAgentControlExtraInfo>,
443443
) {
444-
if data.is_none() {
444+
let Some(ed) = data else {
445445
tracing::error!("handle_mlxreport_action Did not expect extra data to be empty");
446446
return;
447-
}
448-
let ed = data.unwrap();
447+
};
449448

450449
// The Extra data is an array of key value pairs.
451450
// The key is the pci_name of a DPA NIC.

0 commit comments

Comments
 (0)