Skip to content

Commit 28384f3

Browse files
committed
Replace u64 with i64 in wp_mobile_cache and wp_mobile crates
Eliminates unnecessary casting between rusqlite's `i64` and our `u64` types. Since rusqlite consistently uses `i64` for all integer operations, aligning our types reduces friction and improves code clarity. Changes: - Change `RowId` inner type from `u64` to `i64` - Remove casting in `RowId`'s `ToSql`, `FromSql`, and conversion implementations - Update migration functions to use `i64` instead of `u64` - Change `FetchResult.total_items` from `Option<u64>` to `Option<i64>` - Remove unnecessary casts in repository and service code - Update type casts in test code and helper functions
1 parent bc05ec2 commit 28384f3

File tree

6 files changed

+22
-23
lines changed

6 files changed

+22
-23
lines changed

wp_mobile/src/collection/fetch_result.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub struct FetchResult {
1010
pub entity_ids: Vec<EntityId>,
1111

1212
/// Total number of items matching the query (from API)
13-
pub total_items: Option<u64>,
13+
pub total_items: Option<i64>,
1414

1515
/// Total number of pages available (from API)
1616
pub total_pages: Option<u32>,

wp_mobile/src/service/posts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl PostService {
9898

9999
Ok(FetchResult {
100100
entity_ids,
101-
total_items: response.header_map.wp_total().map(|n| n as u64),
101+
total_items: response.header_map.wp_total().map(|n| n as i64),
102102
total_pages: response.header_map.wp_total_pages(),
103103
current_page: page,
104104
})
@@ -305,7 +305,7 @@ mod tests {
305305

306306
// Get the table and rowid from the entity_id
307307
let table = entity_id.table;
308-
let rowid = entity_id.rowid.0 as i64;
308+
let rowid = entity_id.rowid.0;
309309

310310
// Test: Create UpdateHook that matches this entity
311311
let matching_hook = UpdateHook {

wp_mobile_cache/src/entity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ mod tests {
153153
/// Uses the same value for both row_id and mapped_site_id for convenience.
154154
/// In real data, these would be different (row_id is from db_sites table,
155155
/// mapped_site_id is from the type-specific table like self_hosted_sites).
156-
fn make_db_site(id: u64) -> DbSite {
156+
fn make_db_site(id: i64) -> DbSite {
157157
DbSite {
158158
row_id: RowId(id),
159159
site_type: DbSiteType::SelfHosted,

wp_mobile_cache/src/lib.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -112,32 +112,31 @@ impl TryFrom<&str> for DbTable {
112112
}
113113
}
114114

115-
uniffi::custom_newtype!(RowId, u64);
115+
uniffi::custom_newtype!(RowId, i64);
116116

117117
/// Represents a database row ID (autoincrement field).
118-
/// SQLite rowids are guaranteed to be non-negative, so we use u64.
119118
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
120-
pub struct RowId(pub u64);
119+
pub struct RowId(pub i64);
121120

122121
impl ToSql for RowId {
123122
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
124-
Ok(ToSqlOutput::from(self.0 as i64))
123+
Ok(ToSqlOutput::from(self.0))
125124
}
126125
}
127126

128127
impl FromSql for RowId {
129128
fn column_result(value: rusqlite::types::ValueRef<'_>) -> FromSqlResult<Self> {
130129
i64::column_result(value).map(|i| {
131130
debug_assert!(i >= 0, "RowId should be non-negative, got: {}", i);
132-
RowId(i as u64)
131+
RowId(i)
133132
})
134133
}
135134
}
136135

137136
impl From<i64> for RowId {
138137
fn from(value: i64) -> Self {
139138
debug_assert!(value >= 0, "RowId should be non-negative, got: {}", value);
140-
RowId(value as u64)
139+
RowId(value)
141140
}
142141
}
143142

@@ -164,7 +163,7 @@ impl RowId {
164163

165164
impl From<RowId> for i64 {
166165
fn from(row_id: RowId) -> Self {
167-
row_id.0 as i64
166+
row_id.0
168167
}
169168
}
170169

@@ -247,7 +246,7 @@ impl WpApiCache {
247246
})
248247
}
249248

250-
pub fn perform_migrations(&self) -> Result<u64, SqliteDbError> {
249+
pub fn perform_migrations(&self) -> Result<i64, SqliteDbError> {
251250
self.execute(|connection| {
252251
let mut mgr = MigrationManager::new(connection)?;
253252
mgr.perform_migrations().map_err(SqliteDbError::from)
@@ -379,7 +378,7 @@ impl<'a> MigrationManager<'a> {
379378
Ok(result > 0)
380379
}
381380

382-
pub fn perform_migrations(&mut self) -> SqliteResult<u64> {
381+
pub fn perform_migrations(&mut self) -> SqliteResult<i64> {
383382
if !self.has_migrations_table()? {
384383
self.create_migrations_table()?;
385384
}
@@ -394,10 +393,10 @@ impl<'a> MigrationManager<'a> {
394393
}
395394

396395
// `.enumerate` will start the indexes from 0, so we need to add `next_migration_id`
397-
self.insert_migration((next_migration_id + index + 1) as u64)?;
396+
self.insert_migration((next_migration_id + index + 1) as i64)?;
398397
}
399398

400-
Ok(MIGRATION_QUERIES[next_migration_id..].len() as u64)
399+
Ok(MIGRATION_QUERIES[next_migration_id..].len() as i64)
401400
}
402401

403402
pub fn create_migrations_table(&self) -> SqliteResult<()> {
@@ -408,7 +407,7 @@ impl<'a> MigrationManager<'a> {
408407
Ok(())
409408
}
410409

411-
pub fn insert_migration(&mut self, migration_id: u64) -> SqliteResult<()> {
410+
pub fn insert_migration(&mut self, migration_id: i64) -> SqliteResult<()> {
412411
let mut insert_migration_query = self
413412
.connection
414413
.prepare("INSERT INTO _migrations (migration_id) VALUES (?)")?;
@@ -508,14 +507,14 @@ mod tests {
508507
let mut stmt = connection
509508
.prepare("SELECT migration_id FROM _migrations ORDER BY migration_id")
510509
.unwrap();
511-
let migration_ids: Vec<u64> = stmt
512-
.query_map([], |row| row.get::<_, u64>(0))
510+
let migration_ids: Vec<i64> = stmt
511+
.query_map([], |row| row.get::<_, i64>(0))
513512
.unwrap()
514513
.collect::<Result<Vec<_>, _>>()
515514
.unwrap();
516515

517516
// Verify migration IDs are sequential and complete
518-
let expected_ids: Vec<u64> = (1..=MIGRATION_QUERIES.len() as u64).collect();
517+
let expected_ids: Vec<i64> = (1..=MIGRATION_QUERIES.len() as i64).collect();
519518
assert_eq!(
520519
migration_ids,
521520
expected_ids,

wp_mobile_cache/src/repository/posts.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ impl PostRepository<EditContext> {
696696
|row| row.get(0),
697697
)
698698
.map_err(SqliteDbError::from)?;
699-
let post_rowid = RowId(post_rowid as u64);
699+
let post_rowid = RowId(post_rowid);
700700

701701
// Sync term relationships
702702
let term_repo = TermRelationshipRepository;
@@ -829,7 +829,7 @@ impl PostRepository<ViewContext> {
829829
|row| row.get(0),
830830
)
831831
.map_err(SqliteDbError::from)?;
832-
let post_rowid = RowId(post_rowid as u64);
832+
let post_rowid = RowId(post_rowid);
833833

834834
// Sync term relationships (ViewContext has categories and tags)
835835
let term_repo = TermRelationshipRepository;
@@ -930,7 +930,7 @@ impl PostRepository<EmbedContext> {
930930
|row| row.get(0),
931931
)
932932
.map_err(SqliteDbError::from)?;
933-
let post_rowid = RowId(post_rowid as u64);
933+
let post_rowid = RowId(post_rowid);
934934

935935
// No term relationships for EmbedContext (no categories or tags)
936936

wp_mobile_cache/src/repository/term_relationships.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ impl TermRelationshipRepository {
271271
HashMap::new(),
272272
|mut acc: HashMap<i64, Vec<DbTermRelationship>>, row_result| {
273273
let relationship = row_result.map_err(SqliteDbError::from)?;
274-
acc.entry(relationship.object_id.0 as i64)
274+
acc.entry(relationship.object_id.0)
275275
.or_default()
276276
.push(relationship);
277277
Ok::<_, SqliteDbError>(acc)

0 commit comments

Comments
 (0)