From 1e1dd7b877100c14129fed8d9ee50a4687f6cf6d Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Wed, 18 Dec 2024 22:14:09 +0000 Subject: [PATCH] Apply more lints --- .../reconcile/src/{diffs/mod.rs => diffs.rs} | 0 backend/reconcile/src/diffs/myers.rs | 2 +- backend/reconcile/src/diffs/raw_operation.rs | 6 ++-- .../mod.rs => operation_transformation.rs} | 8 ++--- .../operation_transformation/edited_text.rs | 30 ++++++++-------- .../operation_transformation/merge_context.rs | 4 +-- .../src/operation_transformation/operation.rs | 34 +++++++++---------- .../src/{tokenizer/mod.rs => tokenizer.rs} | 0 backend/reconcile/src/tokenizer/token.rs | 4 +-- .../reconcile/src/tokenizer/word_tokenizer.rs | 2 +- .../reconcile/src/{utils/mod.rs => utils.rs} | 0 .../reconcile/src/utils/common_prefix_len.rs | 4 +-- .../reconcile/src/utils/common_suffix_len.rs | 4 +-- backend/reconcile/src/utils/merge_iters.rs | 2 +- backend/reconcile/src/utils/string_builder.rs | 4 +-- backend/sync_server/src/config.rs | 2 +- backend/sync_server/src/config/user_config.rs | 2 +- backend/sync_server/src/database.rs | 4 +-- backend/sync_server/src/errors.rs | 2 +- backend/sync_server/src/main.rs | 2 +- backend/sync_server/src/server.rs | 2 +- .../sync_server/src/server/create_document.rs | 2 +- .../sync_server/src/server/delete_document.rs | 2 +- .../sync_server/src/server/update_document.rs | 2 +- 24 files changed, 61 insertions(+), 63 deletions(-) rename backend/reconcile/src/{diffs/mod.rs => diffs.rs} (100%) rename backend/reconcile/src/{operation_transformation/mod.rs => operation_transformation.rs} (96%) rename backend/reconcile/src/{tokenizer/mod.rs => tokenizer.rs} (100%) rename backend/reconcile/src/{utils/mod.rs => utils.rs} (100%) diff --git a/backend/reconcile/src/diffs/mod.rs b/backend/reconcile/src/diffs.rs similarity index 100% rename from backend/reconcile/src/diffs/mod.rs rename to backend/reconcile/src/diffs.rs diff --git a/backend/reconcile/src/diffs/myers.rs b/backend/reconcile/src/diffs/myers.rs index 31f79e7..e7af851 100644 --- a/backend/reconcile/src/diffs/myers.rs +++ b/backend/reconcile/src/diffs/myers.rs @@ -1,4 +1,4 @@ -//! Taken from https://github.com/mitsuhiko/similar/blob/7e15c44de11a1cd61e1149189929e189ef977fd8/src/algorithms/myers.rs +//! Taken from //! Myers' diff algorithm. //! //! * time: `O((N+M)D)` diff --git a/backend/reconcile/src/diffs/raw_operation.rs b/backend/reconcile/src/diffs/raw_operation.rs index b616995..280460f 100644 --- a/backend/reconcile/src/diffs/raw_operation.rs +++ b/backend/reconcile/src/diffs/raw_operation.rs @@ -23,12 +23,10 @@ where } pub fn original_text_length(&self) -> usize { - self.tokens().iter().map(|t| t.get_original_length()).sum() + self.tokens().iter().map(Token::get_original_length).sum() } - pub fn get_original_text(self) -> String { - self.tokens().iter().map(|t| t.original()).collect() - } + pub fn get_original_text(self) -> String { self.tokens().iter().map(Token::original).collect() } /// Extends the operation with another operation if returning the new /// operation. Only operations of the same type can be used to extend. diff --git a/backend/reconcile/src/operation_transformation/mod.rs b/backend/reconcile/src/operation_transformation.rs similarity index 96% rename from backend/reconcile/src/operation_transformation/mod.rs rename to backend/reconcile/src/operation_transformation.rs index a0c0843..e4f1d7c 100644 --- a/backend/reconcile/src/operation_transformation/mod.rs +++ b/backend/reconcile/src/operation_transformation.rs @@ -7,17 +7,17 @@ pub use operation::Operation; use crate::tokenizer::Tokenizer; -pub fn reconcile(original: &str, left: &str, right: &str) -> String { +#[must_use] pub fn reconcile(original: &str, left: &str, right: &str) -> String { if left == right { - return left.to_string(); + return left.to_owned(); } if original == left { - return right.to_string(); + return right.to_owned(); } if original == right { - return left.to_string(); + return left.to_owned(); } let left_operations = EditedText::from_strings(original, left); diff --git a/backend/reconcile/src/operation_transformation/edited_text.rs b/backend/reconcile/src/operation_transformation/edited_text.rs index 9358d36..38402d0 100644 --- a/backend/reconcile/src/operation_transformation/edited_text.rs +++ b/backend/reconcile/src/operation_transformation/edited_text.rs @@ -1,4 +1,4 @@ -use std::iter; +use core::iter; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -9,17 +9,17 @@ use crate::{ operation_transformation::merge_context::MergeContext, tokenizer::{word_tokenizer::word_tokenizer, Tokenizer}, utils::{ - merge_iters::MergeSorted, ordered_operation::OrderedOperation, side::Side, + merge_iters::MergeSorted as _, ordered_operation::OrderedOperation, side::Side, string_builder::StringBuilder, }, }; /// A sequence of operations that can be applied to a text document. -/// EditedText supports merging two sequences of operations using the +/// `EditedText` supports merging two sequences of operations using the /// principle of Operational Transformation. /// -/// It's mainly created through the from_strings method, then merged with -/// another EditedText derived from the same original text and then applied to +/// It's mainly created through the `from_strings` method, then merged with +/// another `EditedText` derived from the same original text and then applied to /// the original text to get the reconciled text of concurrent edits. #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, PartialEq, Default)] @@ -32,13 +32,13 @@ where } impl<'a> EditedText<'a, String> { - /// Create an EditedText from the given original (old) and updated (new) - /// strings. The returned EditedText represents the changes from the + /// Create an `EditedText` from the given original (old) and updated (new) + /// strings. The returned `EditedText` represents the changes from the /// original to the updated text. When the return value is applied to /// the original text, it will result in the updated text. The default /// word tokenizer is used to tokenize the text which splits the text on /// whitespaces. - pub fn from_strings(original: &'a str, updated: &str) -> Self { + #[must_use] pub fn from_strings(original: &'a str, updated: &str) -> Self { Self::from_strings_with_tokenizer(original, updated, &word_tokenizer) } } @@ -47,8 +47,8 @@ impl<'a, T> EditedText<'a, T> where T: PartialEq + Clone, { - /// Create an EditedText from the given original (old) and updated (new) - /// strings. The returned EditedText represents the changes from the + /// Create an `EditedText` from the given original (old) and updated (new) + /// strings. The returned `EditedText` represents the changes from the /// original to the updated text. When the return value is applied to /// the original text, it will result in the updated text. The tokenizer /// function is used to tokenize the text. @@ -167,7 +167,7 @@ where result } - /// Create a new EditedText with the given operations. + /// Create a new `EditedText` with the given operations. /// The operations must be in the order in which they are meant to be /// applied. The operations must not overlap. fn new(text: &'a str, operations: Vec>) -> Self { @@ -186,7 +186,7 @@ where Self { text, operations } } - pub fn merge(self, other: Self) -> Self { + #[must_use] pub fn merge(self, other: Self) -> Self { debug_assert_eq!( self.text, other.text, "EditedTexts must be derived from the same text to be mergable" @@ -207,7 +207,7 @@ where operation.order, // Operations on left and right must come in the same order so that // inserts can be merged with other inserts and deletes with deletes. - matches!(operation.operation, Operation::Delete { .. }) as usize, + usize::from(matches!(operation.operation, Operation::Delete { .. })), ) }, ) @@ -233,9 +233,9 @@ where /// /// # Errors /// - /// Returns an SyncLibError::OperationError if the operations cannot be + /// Returns an `SyncLibError::OperationError` if the operations cannot be /// applied to the text. - pub fn apply(&self) -> String { + #[must_use] pub fn apply(&self) -> String { let mut builder: StringBuilder<'_> = StringBuilder::new(self.text); for OrderedOperation { operation, .. } in &self.operations { diff --git a/backend/reconcile/src/operation_transformation/merge_context.rs b/backend/reconcile/src/operation_transformation/merge_context.rs index 61df685..0bc3c34 100644 --- a/backend/reconcile/src/operation_transformation/merge_context.rs +++ b/backend/reconcile/src/operation_transformation/merge_context.rs @@ -1,4 +1,4 @@ -use std::fmt::Debug; +use core::fmt::Debug; use crate::operation_transformation::Operation; @@ -27,7 +27,7 @@ impl Debug for MergeContext where T: PartialEq + Clone, { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { f.debug_struct("MergeContext") .field("last_operation", &self.last_operation) .field("shift", &self.shift) diff --git a/backend/reconcile/src/operation_transformation/operation.rs b/backend/reconcile/src/operation_transformation/operation.rs index 021734b..d83d2d2 100644 --- a/backend/reconcile/src/operation_transformation/operation.rs +++ b/backend/reconcile/src/operation_transformation/operation.rs @@ -1,4 +1,4 @@ -use std::{ +use core::{ fmt::{Debug, Display}, ops::Range, }; @@ -13,8 +13,8 @@ use crate::{ }; /// Represents a change that can be applied to a text document. -/// Operation is tied to a ropey::Rope and is mainly expected to be -/// created by EditedText. +/// Operation is tied to a `ropey::Rope` and is mainly expected to be +/// created by `EditedText`. #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Clone, PartialEq)] pub enum Operation @@ -81,12 +81,12 @@ where }) } - /// Tries to apply the operation to the given ropey::Rope text, returning + /// Tries to apply the operation to the given `ropey::Rope` text, returning /// the modified text. /// /// # Errors /// - /// Returns a SyncLibError::OperationApplicationError if the operation + /// Returns a `SyncLibError::OperationApplicationError` if the operation /// cannot be applied. /// /// # Panics @@ -99,7 +99,7 @@ where self.start_index(), &text .iter() - .map(|token| token.original()) + .map(super::super::tokenizer::token::Token::original) .collect::(), ), Operation::Delete { @@ -111,8 +111,7 @@ where debug_assert!( deleted_text .as_ref() - .map(|text| builder.get_slice(self.range()) == *text) - .unwrap_or(true), + .map_or(true, |text| builder.get_slice(self.range()) == *text), "Text to delete does not match the text in the rope" ); @@ -141,15 +140,16 @@ where } /// Returns the range of indices of characters that the operation affects. - pub fn range(&self) -> Range { self.start_index()..self.end_index() + 1 } + pub fn range(&self) -> Range { self.start_index()..self.end_index() - 1 } /// Returns the number of affected characters. It is always greater than 0 /// because empty operations cannot be created. pub fn len(&self) -> usize { match self { - Operation::Insert { text, .. } => { - text.iter().map(|token| token.get_original_length()).sum() - } + Operation::Insert { text, .. } => text + .iter() + .map(super::super::tokenizer::token::Token::get_original_length) + .sum(), Operation::Delete { deleted_character_count, .. @@ -223,7 +223,7 @@ where let trimmed_length = previous_inserted_text .iter() .skip(offset_in_tokens) - .map(|token| token.get_original_length()) + .map(super::super::tokenizer::token::Token::get_original_length) .sum::(); let trimmed_operation = Operation::create_insert(index, text[trimmed_length_in_tokens..].to_vec()); @@ -231,7 +231,7 @@ where affecting_context.shift -= trimmed_length as i64; produced_context.shift += trimmed_operation .as_ref() - .map(|op| op.len()) + .map(Operation::len) .unwrap_or_default() as i64; produced_context.consume_and_replace_last_operation(trimmed_operation.clone()); @@ -305,14 +305,14 @@ impl Display for Operation where T: PartialEq + Clone, { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { match self { Operation::Insert { index, text } => { write!( f, "", text.iter() - .map(|token| token.original()) + .map(super::super::tokenizer::token::Token::original) .collect::(), index ) @@ -351,7 +351,7 @@ impl Debug for Operation where T: PartialEq + Clone, { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self) } + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "{self}") } } #[cfg(test)] diff --git a/backend/reconcile/src/tokenizer/mod.rs b/backend/reconcile/src/tokenizer.rs similarity index 100% rename from backend/reconcile/src/tokenizer/mod.rs rename to backend/reconcile/src/tokenizer.rs diff --git a/backend/reconcile/src/tokenizer/token.rs b/backend/reconcile/src/tokenizer/token.rs index e08e0ba..f723a2c 100644 --- a/backend/reconcile/src/tokenizer/token.rs +++ b/backend/reconcile/src/tokenizer/token.rs @@ -17,8 +17,8 @@ where impl From<&str> for Token { fn from(s: &str) -> Self { Token { - normalised: s.to_string(), - original: s.to_string(), + normalised: s.to_owned(), + original: s.to_owned(), } } } diff --git a/backend/reconcile/src/tokenizer/word_tokenizer.rs b/backend/reconcile/src/tokenizer/word_tokenizer.rs index 1e4ac6d..3449cba 100644 --- a/backend/reconcile/src/tokenizer/word_tokenizer.rs +++ b/backend/reconcile/src/tokenizer/word_tokenizer.rs @@ -2,6 +2,6 @@ use super::token::Token; pub fn word_tokenizer(text: &str) -> Vec> { text.split_inclusive(char::is_whitespace) - .map(|s| Token::new(s.to_string(), s.to_string())) + .map(|s| Token::new(s.to_owned(), s.to_owned())) .collect() } diff --git a/backend/reconcile/src/utils/mod.rs b/backend/reconcile/src/utils.rs similarity index 100% rename from backend/reconcile/src/utils/mod.rs rename to backend/reconcile/src/utils.rs diff --git a/backend/reconcile/src/utils/common_prefix_len.rs b/backend/reconcile/src/utils/common_prefix_len.rs index 8290d01..5c1a5c1 100644 --- a/backend/reconcile/src/utils/common_prefix_len.rs +++ b/backend/reconcile/src/utils/common_prefix_len.rs @@ -1,7 +1,7 @@ -use std::ops::{Index, Range}; +use core::ops::{Index, Range}; /// Given two lookups and ranges calculates the length of the common prefix. -/// Copied from https://github.com/mitsuhiko/similar/blob/7e15c44de11a1cd61e1149189929e189ef977fd8/src/algorithms/utils.rs +/// Copied from pub fn common_prefix_len( old: &Old, old_range: Range, diff --git a/backend/reconcile/src/utils/common_suffix_len.rs b/backend/reconcile/src/utils/common_suffix_len.rs index f63d955..c17a979 100644 --- a/backend/reconcile/src/utils/common_suffix_len.rs +++ b/backend/reconcile/src/utils/common_suffix_len.rs @@ -1,7 +1,7 @@ -use std::ops::{Index, Range}; +use core::ops::{Index, Range}; /// Given two lookups and ranges calculates the length of common suffix. -/// Copied from https://github.com/mitsuhiko/similar/blob/7e15c44de11a1cd61e1149189929e189ef977fd8/src/algorithms/utils.rs +/// Copied from pub fn common_suffix_len( old: &Old, old_range: Range, diff --git a/backend/reconcile/src/utils/merge_iters.rs b/backend/reconcile/src/utils/merge_iters.rs index 6408c55..c7b7334 100644 --- a/backend/reconcile/src/utils/merge_iters.rs +++ b/backend/reconcile/src/utils/merge_iters.rs @@ -1,4 +1,4 @@ -use std::{cmp::Ordering, iter::Peekable}; +use core::{cmp::Ordering, iter::Peekable}; pub struct MergeAscending where diff --git a/backend/reconcile/src/utils/string_builder.rs b/backend/reconcile/src/utils/string_builder.rs index f3880f8..3394fac 100644 --- a/backend/reconcile/src/utils/string_builder.rs +++ b/backend/reconcile/src/utils/string_builder.rs @@ -1,4 +1,4 @@ -use std::ops::Range; +use core::ops::Range; /// A helper for building a string in order based on an original string and a /// series of insertions and deletions applied to it. It is safe to use with @@ -28,7 +28,7 @@ impl StringBuilder<'_> { /// Delete a string at the given index after copying the original string up /// to that index from the last insertion or deletion. - pub fn delete(&mut self, range: std::ops::Range) { + pub fn delete(&mut self, range: core::ops::Range) { self.copy_until(range.start); self.last_old_char_index += range.len(); } diff --git a/backend/sync_server/src/config.rs b/backend/sync_server/src/config.rs index a6b7258..829375d 100644 --- a/backend/sync_server/src/config.rs +++ b/backend/sync_server/src/config.rs @@ -1,6 +1,6 @@ use std::path::Path; -use anyhow::{Context, Result}; +use anyhow::{Context as _, Result}; use database_config::DatabaseConfig; use log::{info, warn}; use serde::{Deserialize, Serialize}; diff --git a/backend/sync_server/src/config/user_config.rs b/backend/sync_server/src/config/user_config.rs index bf82d9b..bb7d8e5 100644 --- a/backend/sync_server/src/config/user_config.rs +++ b/backend/sync_server/src/config/user_config.rs @@ -1,4 +1,4 @@ -use rand::{distributions::Alphanumeric, thread_rng, Rng}; +use rand::{distributions::Alphanumeric, thread_rng, Rng as _}; use serde::{Deserialize, Serialize}; #[derive(Debug, Deserialize, Serialize, Clone)] diff --git a/backend/sync_server/src/database.rs b/backend/sync_server/src/database.rs index 77c3c0f..fdd0401 100644 --- a/backend/sync_server/src/database.rs +++ b/backend/sync_server/src/database.rs @@ -1,6 +1,6 @@ -use core::{str::FromStr, time::Duration}; +use core::{str::FromStr as _, time::Duration}; -use anyhow::{Context, Result}; +use anyhow::{Context as _, Result}; use models::{ DocumentId, DocumentVersionWithoutContent, StoredDocumentVersion, VaultId, VaultUpdateId, }; diff --git a/backend/sync_server/src/errors.rs b/backend/sync_server/src/errors.rs index 778a9c5..13a35aa 100644 --- a/backend/sync_server/src/errors.rs +++ b/backend/sync_server/src/errors.rs @@ -26,7 +26,7 @@ pub enum SyncServerError { #[error("Unauthorized: {0}")] Unauthorized(#[source] anyhow::Error), - #[allow(dead_code)] + #[expect(dead_code)] #[error("Permission denied error: {0}")] PermissionDeniedError(#[source] anyhow::Error), } diff --git a/backend/sync_server/src/main.rs b/backend/sync_server/src/main.rs index f27ef5d..00866c4 100644 --- a/backend/sync_server/src/main.rs +++ b/backend/sync_server/src/main.rs @@ -5,7 +5,7 @@ mod database; mod errors; mod server; -use anyhow::{Context, Result}; +use anyhow::{Context as _, Result}; use app_state::AppState; use errors::{init_error, SyncServerError}; use server::create_server; diff --git a/backend/sync_server/src/server.rs b/backend/sync_server/src/server.rs index d5c7be1..e668447 100644 --- a/backend/sync_server/src/server.rs +++ b/backend/sync_server/src/server.rs @@ -6,7 +6,7 @@ use aide::{ openapi::{Info, OpenApi}, scalar::Scalar, }; -use anyhow::{Context, Result}; +use anyhow::{Context as _, Result}; use axum::{ extract::DefaultBodyLimit, http::{self, HeaderValue, Method}, diff --git a/backend/sync_server/src/server/create_document.rs b/backend/sync_server/src/server/create_document.rs index 0e1bf8b..18ecffa 100644 --- a/backend/sync_server/src/server/create_document.rs +++ b/backend/sync_server/src/server/create_document.rs @@ -1,4 +1,4 @@ -use anyhow::Context; +use anyhow::Context as _; use axum::{ extract::{Path, State}, Json, diff --git a/backend/sync_server/src/server/delete_document.rs b/backend/sync_server/src/server/delete_document.rs index 40b87bb..fd56ada 100644 --- a/backend/sync_server/src/server/delete_document.rs +++ b/backend/sync_server/src/server/delete_document.rs @@ -1,4 +1,4 @@ -use anyhow::Context; +use anyhow::Context as _; use axum::{ extract::{Path, State}, Json, diff --git a/backend/sync_server/src/server/update_document.rs b/backend/sync_server/src/server/update_document.rs index 0648017..31358a3 100644 --- a/backend/sync_server/src/server/update_document.rs +++ b/backend/sync_server/src/server/update_document.rs @@ -1,4 +1,4 @@ -use anyhow::{anyhow, Context}; +use anyhow::{anyhow, Context as _}; use axum::{ extract::{Path, State}, Json,