notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

commit 0138186a005690a85ebb3f1e95bee37d595b0714
parent 72c44bdf2d7ebbf8cdfb52ce99778b88e4e0d9fc
Author: kernelkind <kernelkind@gmail.com>
Date:   Thu,  5 Dec 2024 20:22:15 -0500

remove unnecesary serializations

Signed-off-by: kernelkind <kernelkind@gmail.com>

Diffstat:
Msrc/column.rs | 80++-----------------------------------------------------------------------------
Msrc/route.rs | 3+--
Dsrc/storage/columns.rs | 48------------------------------------------------
Msrc/timeline/kind.rs | 4++--
Msrc/timeline/mod.rs | 22+---------------------
Msrc/timeline/route.rs | 2+-
Msrc/ui/add_column.rs | 3+--
7 files changed, 8 insertions(+), 154 deletions(-)

diff --git a/src/column.rs b/src/column.rs @@ -1,11 +1,9 @@ use crate::route::{Route, Router}; -use crate::timeline::{SerializableTimeline, Timeline, TimelineId, TimelineRoute}; +use crate::timeline::{Timeline, TimelineId}; use indexmap::IndexMap; -use nostrdb::Ndb; -use serde::{Deserialize, Deserializer, Serialize}; use std::iter::Iterator; use std::sync::atomic::{AtomicU32, Ordering}; -use tracing::{error, warn}; +use tracing::warn; #[derive(Clone)] pub struct Column { @@ -27,28 +25,6 @@ impl Column { } } -impl serde::Serialize for Column { - fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> - where - S: serde::Serializer, - { - self.router.routes().serialize(serializer) - } -} - -impl<'de> Deserialize<'de> for Column { - fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> - where - D: Deserializer<'de>, - { - let routes = Vec::<Route>::deserialize(deserializer)?; - - Ok(Column { - router: Router::new(routes), - }) - } -} - #[derive(Default)] pub struct Columns { /// Columns are simply routers into settings, timelines, etc @@ -235,58 +211,6 @@ impl Columns { self.new_column_picker(); } } - - pub fn as_serializable_columns(&self) -> SerializableColumns { - SerializableColumns { - columns: self.columns.values().cloned().collect(), - timelines: self - .timelines - .values() - .map(|t| t.as_serializable_timeline()) - .collect(), - } - } -} - -#[derive(Serialize, Deserialize)] -pub struct SerializableColumns { - pub columns: Vec<Column>, - pub timelines: Vec<SerializableTimeline>, -} - -impl SerializableColumns { - pub fn into_columns(self, ndb: &Ndb, deck_pubkey: Option<&[u8; 32]>) -> Columns { - let mut columns = Columns::default(); - - for column in self.columns { - let id = Columns::get_new_id(); - let mut routes = Vec::new(); - for route in column.router.routes() { - match route { - Route::Timeline(TimelineRoute::Timeline(timeline_id)) => { - if let Some(serializable_tl) = - self.timelines.iter().find(|tl| tl.id == *timeline_id) - { - let tl = serializable_tl.clone().into_timeline(ndb, deck_pubkey); - if let Some(tl) = tl { - routes.push(Route::Timeline(TimelineRoute::Timeline(tl.id))); - columns.timelines.insert(id, tl); - } else { - error!("Problem deserializing timeline {:?}", serializable_tl); - } - } - } - Route::Timeline(TimelineRoute::Thread(_thread)) => { - // TODO: open thread before pushing route - } - _ => routes.push(*route), - } - } - columns.add_column_at(Column::new(routes), id); - } - - columns - } } pub enum IntermediaryRoute { diff --git a/src/route.rs b/src/route.rs @@ -1,5 +1,4 @@ use enostr::{NoteId, Pubkey}; -use serde::{Deserialize, Serialize}; use std::{ borrow::Cow, fmt::{self}, @@ -13,7 +12,7 @@ use crate::{ }; /// App routing. These describe different places you can go inside Notedeck. -#[derive(Clone, Copy, Eq, PartialEq, Debug, Serialize, Deserialize)] +#[derive(Clone, Copy, Eq, PartialEq, Debug)] pub enum Route { Timeline(TimelineRoute), Accounts(AccountsRoute), diff --git a/src/storage/columns.rs b/src/storage/columns.rs @@ -1,48 +0,0 @@ -use tracing::{error, info}; - -use crate::column::SerializableColumns; - -use super::{write_file, DataPath, DataPathType, Directory}; - -static COLUMNS_FILE: &str = "columns.json"; - -pub fn save_columns(path: &DataPath, columns: SerializableColumns) { - let serialized_columns = match serde_json::to_string(&columns) { - Ok(s) => s, - Err(e) => { - error!("Could not serialize columns: {}", e); - return; - } - }; - - let data_path = path.path(DataPathType::Setting); - - if let Err(e) = write_file(&data_path, COLUMNS_FILE.to_string(), &serialized_columns) { - error!("Could not write columns to file {}: {}", COLUMNS_FILE, e); - } else { - info!("Successfully wrote columns to {}", COLUMNS_FILE); - } -} - -pub fn load_columns(path: &DataPath) -> Option<SerializableColumns> { - let data_path = path.path(DataPathType::Setting); - - let columns_string = match Directory::new(data_path).get_file(COLUMNS_FILE.to_owned()) { - Ok(s) => s, - Err(e) => { - error!("Could not read columns from file {}: {}", COLUMNS_FILE, e); - return None; - } - }; - - match serde_json::from_str::<SerializableColumns>(&columns_string) { - Ok(s) => { - info!("Successfully loaded columns from {}", COLUMNS_FILE); - Some(s) - } - Err(e) => { - error!("Could not deserialize columns: {}", e); - None - } - } -} diff --git a/src/timeline/kind.rs b/src/timeline/kind.rs @@ -14,7 +14,7 @@ pub enum PubkeySource { DeckAuthor, } -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum ListKind { Contact(PubkeySource), } @@ -44,7 +44,7 @@ impl ListKind { /// - filter /// - ... etc /// -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum TimelineKind { List(ListKind), diff --git a/src/timeline/mod.rs b/src/timeline/mod.rs @@ -17,7 +17,6 @@ use std::sync::atomic::{AtomicU32, Ordering}; use egui_virtual_list::VirtualList; use enostr::{Relay, RelayPool}; use nostrdb::{Filter, Ndb, Note, Subscription, Transaction}; -use serde::{Deserialize, Serialize}; use std::cell::RefCell; use std::hash::Hash; use std::rc::Rc; @@ -30,7 +29,7 @@ pub mod route; pub use kind::{PubkeySource, TimelineKind}; pub use route::TimelineRoute; -#[derive(Debug, Hash, Copy, Clone, Eq, PartialEq, serde::Serialize, serde::Deserialize)] +#[derive(Debug, Hash, Copy, Clone, Eq, PartialEq)] pub struct TimelineId(u32); impl TimelineId { @@ -186,18 +185,6 @@ pub struct Timeline { pub subscription: Option<Subscription>, } -#[derive(Serialize, Deserialize, Clone, Debug)] -pub struct SerializableTimeline { - pub id: TimelineId, - pub kind: TimelineKind, -} - -impl SerializableTimeline { - pub fn into_timeline(self, ndb: &Ndb, deck_user_pubkey: Option<&[u8; 32]>) -> Option<Timeline> { - self.kind.into_timeline(ndb, deck_user_pubkey) - } -} - impl Timeline { /// Create a timeline from a contact list pub fn contact_list(contact_list: &Note, pk_src: PubkeySource) -> Result<Self> { @@ -351,13 +338,6 @@ impl Timeline { Ok(()) } - - pub fn as_serializable_timeline(&self) -> SerializableTimeline { - SerializableTimeline { - id: self.id, - kind: self.kind.clone(), - } - } } pub enum MergeKind { diff --git a/src/timeline/route.rs b/src/timeline/route.rs @@ -21,7 +21,7 @@ use crate::{ use enostr::{NoteId, Pubkey}; use nostrdb::{Ndb, Transaction}; -#[derive(Debug, Eq, PartialEq, Clone, Copy, serde::Serialize, serde::Deserialize)] +#[derive(Debug, Eq, PartialEq, Clone, Copy)] pub enum TimelineRoute { Timeline(TimelineId), Thread(NoteId), diff --git a/src/ui/add_column.rs b/src/ui/add_column.rs @@ -1,5 +1,4 @@ use core::f32; -use serde::{Deserialize, Serialize}; use std::collections::HashMap; use egui::{ @@ -43,7 +42,7 @@ enum AddColumnOption { Hashtag(String), } -#[derive(Clone, Copy, Eq, PartialEq, Debug, Serialize, Deserialize)] +#[derive(Clone, Copy, Eq, PartialEq, Debug)] pub enum AddColumnRoute { Base, UndecidedNotification,