π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Remove avoidable unwraps from Rust application code
Manav Rathi7f9808d232dd
diff --git a/rust/apps/cli/src/commands/export.rs b/rust/apps/cli/src/commands/export.rsindex 3a96031666..2e48282e85 100644--- a/rust/apps/cli/src/commands/export.rs+++ b/rust/apps/cli/src/commands/export.rs@@ -12,7 +12,7 @@ use crate::storage::Storage;use crate::sync::SyncEngine;use ente_core::b64;use ente_core::crypto;-use std::collections::HashMap;+use std::collections::{HashMap, hash_map::Entry};use std::path::{Path, PathBuf};use tokio::fs;use tokio::io::AsyncWriteExt;@@ -476,17 +476,18 @@ async fn export_account(storage: &Storage, account: &Account, filter: &ExportFil"Uncategorized".to_string()};- if !album_existing_files.contains_key(&album_folder) {- let existing = load_album_metadata(export_path, &album_folder).await?;- log::debug!(- "Loaded {} existing files for album {}",- existing.len(),- album_folder- );- album_existing_files.insert(album_folder.clone(), existing);- }-- let existing_files = album_existing_files.get_mut(&album_folder).unwrap();+ let existing_files = match album_existing_files.entry(album_folder.clone()) {+ Entry::Occupied(entry) => entry.into_mut(),+ Entry::Vacant(entry) => {+ let existing = load_album_metadata(export_path, entry.key()).await?;+ log::debug!(+ "Loaded {} existing files for album {}",+ existing.len(),+ entry.key()+ );+ entry.insert(existing)+ }+ };// Remove current files so only deleted files remain after the loop.if let Some(existing) = existing_files.remove(&file.id) {if existing.file_path == file_path {@@ -932,11 +933,13 @@ fn decrypt_file_metadata(file: &crate::api::models::File,file_key: &[u8],) -> Result<Option<FileMetadata>> {- if file.metadata.encrypted_data.is_none() || file.metadata.decryption_header.is_empty() {+ let Some(encrypted_data) = &file.metadata.encrypted_data else {+ return Ok(None);+ };+ if file.metadata.decryption_header.is_empty() {return Ok(None);}- let encrypted_data = file.metadata.encrypted_data.as_ref().unwrap();let encrypted_bytes = b64::decode(encrypted_data)?;let header_bytes = b64::decode(&file.metadata.decryption_header)?;diff --git a/rust/apps/cli/src/models/error.rs b/rust/apps/cli/src/models/error.rsindex cc0e4d128d..4117cc8bef 100644--- a/rust/apps/cli/src/models/error.rs+++ b/rust/apps/cli/src/models/error.rs@@ -6,6 +6,9 @@ pub enum Error {#[error("IO error: {0}")]Io(#[from] std::io::Error),+ #[error("System clock error: {0}")]+ SystemTime(#[from] std::time::SystemTimeError),+#[error("Serialization error: {0}")]Serialization(#[from] serde_json::Error),diff --git a/rust/apps/cli/src/storage/account.rs b/rust/apps/cli/src/storage/account.rsindex ad84e407ae..ec559bfdf2 100644--- a/rust/apps/cli/src/storage/account.rs+++ b/rust/apps/cli/src/storage/account.rs@@ -15,7 +15,7 @@ impl<'a> AccountStore<'a> {}pub fn add(&self, account: &Account) -> Result<()> {- let now = current_timestamp();+ let now = current_timestamp()?;self.conn.execute("INSERT INTO accounts (user_id, app, email, endpoint, export_dir, created_at, updated_at)@@ -63,7 +63,7 @@ impl<'a> AccountStore<'a> {}pub fn update_export_dir(&self, email: &str, app: App, export_dir: &str) -> Result<()> {- let now = current_timestamp();+ let now = current_timestamp()?;let rows_affected = self.conn.execute("UPDATE accounts SET export_dir = ?1, updated_at = ?2@@ -96,7 +96,7 @@ impl<'a> AccountStore<'a> {}pub fn store_secrets(&self, user_id: i64, app: App, secrets: &AccountSecrets) -> Result<()> {- let now = current_timestamp();+ let now = current_timestamp()?;self.conn.execute("INSERT OR REPLACE INTO secrets@@ -155,9 +155,6 @@ fn row_to_account(row: &Row) -> rusqlite::Result<Account> {})}-fn current_timestamp() -> i64 {- SystemTime::now()- .duration_since(UNIX_EPOCH)- .unwrap()- .as_secs() as i64+fn current_timestamp() -> Result<i64> {+ Ok(SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() as i64)}diff --git a/rust/apps/cli/src/storage/config.rs b/rust/apps/cli/src/storage/config.rsindex 275fb524cf..a4e8eb202b 100644--- a/rust/apps/cli/src/storage/config.rs+++ b/rust/apps/cli/src/storage/config.rs@@ -12,10 +12,7 @@ impl<'a> ConfigStore<'a> {}pub fn set(&self, key: &str, value: &str) -> Result<()> {- let now = SystemTime::now()- .duration_since(UNIX_EPOCH)- .unwrap()- .as_secs() as i64;+ let now = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() as i64;self.conn.execute("INSERT OR REPLACE INTO config (key, value, updated_at)diff --git a/rust/apps/cli/src/storage/sync.rs b/rust/apps/cli/src/storage/sync.rsindex 491b147d4d..bf570a9949 100644--- a/rust/apps/cli/src/storage/sync.rs+++ b/rust/apps/cli/src/storage/sync.rs@@ -42,14 +42,9 @@ impl<'a> SyncStore<'a> {ORDER BY collection_id",)?;- let collections = stmt- .query_map(params![user_id], |row| {- let metadata: String = row.get(0)?;- Ok(serde_json::from_str::<Collection>(&metadata).unwrap())- })?- .collect::<std::result::Result<Vec<_>, _>>()?;-- Ok(collections)+ stmt.query_map(params![user_id], |row| row.get::<_, String>(0))?+ .map(|metadata| Ok(serde_json::from_str(&metadata?)?))+ .collect()}pub fn upsert_file(&self, file: &RemoteFile) -> Result<()> {@@ -379,3 +374,25 @@ impl<'a> SyncStore<'a> {Ok(path)}}++#[cfg(test)]+mod tests {+ use crate::{Error, Result, storage::Storage};++ #[test]+ fn malformed_collection_metadata_returns_error() -> Result<()> {+ let storage = Storage::new_in_memory()?;+ storage.conn().execute(+ "INSERT INTO collections+ (collection_id, owner, name, type, metadata, updated_at)+ VALUES (1, 1, 'album', 'album', '{', 0)",+ [],+ )?;++ assert!(matches!(+ storage.sync().get_collections(1),+ Err(Error::Serialization(_))+ ));+ Ok(())+ }+}diff --git a/rust/crates/exif/src/png.rs b/rust/crates/exif/src/png.rsindex e8543f3373..1072c05d7c 100644--- a/rust/crates/exif/src/png.rs+++ b/rust/crates/exif/src/png.rs@@ -147,7 +147,7 @@ fn text<R: Read + Seek>(if language.is_some_and(|value| value.len() > state.limits.value_bytes) {return Err(Error::Limit("PNG language"));}- xmp::store(state, "urn:png:text", &key, &text, language, None)+ xmp::store(state, "urn:png:text", &key, &text, language, None, None)}fn profile(key: &[u8], text: &[u8], state: &mut State) -> Result<(), Error> {diff --git a/rust/crates/exif/src/read.rs b/rust/crates/exif/src/read.rsindex c4ef6ab818..a277d8236e 100644--- a/rust/crates/exif/src/read.rs+++ b/rust/crates/exif/src/read.rs@@ -135,11 +135,29 @@ pub(crate) struct State<'a> {pub mode: Mode,pub limits: Limits,pub structure: Option<&'a mut crate::XmpStructure>,+ pub output: OutputBudget,entries: usize,- output: usize,expanded: usize,}+pub(crate) struct OutputBudget {+ used: usize,+ limit: usize,+}++impl OutputBudget {+ pub fn retain(&mut self, count: usize) -> Result<(), Error> {+ self.used = self+ .used+ .checked_add(count)+ .ok_or(Error::Limit("output bytes"))?;+ if self.used > self.limit {+ return Err(Error::Limit("output bytes"));+ }+ Ok(())+ }+}+impl State<'_> {pub fn new(mode: Mode, limits: Limits) -> Self {Self {@@ -147,8 +165,11 @@ impl State<'_> {mode,limits,structure: None,+ output: OutputBudget {+ used: 0,+ limit: limits.output_bytes,+ },entries: 0,- output: 0,expanded: 0,}}@@ -165,14 +186,7 @@ impl State<'_> {}pub fn retain(&mut self, count: usize) -> Result<(), Error> {- self.output = self- .output- .checked_add(count)- .ok_or(Error::Limit("output bytes"))?;- if self.output > self.limits.output_bytes {- return Err(Error::Limit("output bytes"));- }- Ok(())+ self.output.retain(count)}pub fn expanded(&mut self, count: usize) -> Result<(), Error> {diff --git a/rust/crates/exif/src/xmp.rs b/rust/crates/exif/src/xmp.rsindex 0ef6e5dcdd..7909bd2ea5 100644--- a/rust/crates/exif/src/xmp.rs+++ b/rust/crates/exif/src/xmp.rs@@ -1,5 +1,5 @@use crate::Mode;-use crate::read::{Error, State};+use crate::read::{Error, OutputBudget, State};use quick_xml::events::Event;use quick_xml::name::ResolveResult;use quick_xml::{NsReader, XmlVersion};@@ -171,17 +171,12 @@ pub(crate) fn read(bytes: &[u8], state: &mut State) -> Result<(), Error> {.decoded_and_normalized_value(XmlVersion::Implicit1_0, reader.decoder()).map_err(|_| Error::Malformed("XML attribute value"))?;let language = stack.last().and_then(|f| f.language.as_deref());- store(state, ans, aname, &value, language, item)?;- if state.structure.is_some() {- let parent = path(&mut paths, state)?;- *state- .structure- .as_mut()- .unwrap()- .parents- .last_mut()- .unwrap() = parent;- }+ let parent = if let Some(structure) = &mut state.structure {+ path(&mut paths, &mut structure.nodes, &mut state.output)?+ } else {+ None+ };+ store(state, ans, aname, &value, language, item, parent)?;}if empty {finish(&mut stack, &mut paths, state)?;@@ -275,6 +270,7 @@ pub(crate) fn store(value: &str,language: Option<&str>,item: Option<u32>,+ parent: Option<u32>,) -> Result<(), Error> {if !wanted(state.mode, ns, name) {return Ok(());@@ -289,9 +285,9 @@ pub(crate) fn store(+ value.len()+ language.map_or(0, str::len),)?;- if state.structure.is_some() {- state.retain(std::mem::size_of::<Option<u32>>())?;- state.structure.as_mut().unwrap().parents.push(None);+ if let Some(structure) = &mut state.structure {+ state.output.retain(std::mem::size_of::<Option<u32>>())?;+ structure.parents.push(parent);}state.metadata.xmp.push(Property {namespace: ns.to_owned(),@@ -360,21 +356,24 @@ struct PathFrame {ready: bool,}-fn path(stack: &mut [PathFrame], state: &mut State) -> Result<Option<u32>, Error> {+fn path(+ stack: &mut [PathFrame],+ nodes: &mut Vec<XmpNode>,+ output: &mut OutputBudget,+) -> Result<Option<u32>, Error> {let Some((frame, rest)) = stack.split_last_mut() else {return Ok(None);};if frame.ready {return Ok(frame.node);}- let parent = path(rest, state)?;+ let parent = path(rest, nodes, output)?;frame.ready = true;let Some((ns, name)) = &frame.key else {frame.node = parent;return Ok(parent);};- state.retain(std::mem::size_of::<XmpNode>() + ns.len() + name.len())?;- let nodes = &mut state.structure.as_mut().unwrap().nodes;+ output.retain(std::mem::size_of::<XmpNode>() + ns.len() + name.len())?;let index = u32::try_from(nodes.len()).map_err(|_| Error::Limit("XMP nodes"))?;nodes.push(XmpNode {namespace: ns.to_owned(),@@ -410,6 +409,16 @@ fn finish(if let Some((ns, name)) = frame.key&& !frame.text.trim().is_empty(){+ let parent = if let Some(structure) = &mut state.structure {+ let is_item = paths+ .last()+ .and_then(|p| p.key.as_ref())+ .is_some_and(|(ns, n)| ns == namespace::RDF && n == "li");+ let len = paths.len() - usize::from(!is_item);+ path(&mut paths[..len], &mut structure.nodes, &mut state.output)?+ } else {+ None+ };store(state,&ns,@@ -417,22 +426,8 @@ fn finish(frame.text.trim(),frame.language.as_deref(),frame.item,+ parent,)?;- if state.structure.is_some() {- let is_item = paths- .last()- .and_then(|p| p.key.as_ref())- .is_some_and(|(ns, n)| ns == namespace::RDF && n == "li");- let len = paths.len() - usize::from(!is_item);- let parent = path(&mut paths[..len], state)?;- *state- .structure- .as_mut()- .unwrap()- .parents- .last_mut()- .unwrap() = parent;- }}paths.pop();Ok(())diff --git a/rust/crates/ml/src/pet/detect.rs b/rust/crates/ml/src/pet/detect.rsindex ffe00fbafd..a307caa687 100644--- a/rust/crates/ml/src/pet/detect.rs+++ b/rust/crates/ml/src/pet/detect.rs@@ -81,8 +81,8 @@ fn postprocess_pet_face_tensor<T: onnx::FloatTensorData>(output_data: T,input: &YoloInput,) -> MlResult<Vec<PetFaceDetection>> {- let row_len = if output_shape.len() >= 2 {- *output_shape.last().unwrap() as usize+ let row_len = if let [_, .., last] = output_shape {+ *last as usize} else if output_shape.len() == 1 {let total = output_data.len();if total.is_multiple_of(13) {