fix clippy warnings

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2024-04-19 14:30:08 -07:00
parent a71e8206fb
commit 99ac578ebd
11 changed files with 88 additions and 89 deletions

View File

@@ -73,7 +73,7 @@ impl<'a> AccountLoginView<'a> {
.rect_filled(top_rect, Rounding::ZERO, top_background_color);
egui::CentralPanel::default()
.show(ui.ctx(), |ui: &mut egui::Ui| {})
.show(ui.ctx(), |_ui: &mut egui::Ui| {})
.response
}

View File

@@ -36,19 +36,19 @@ pub struct NoteRef {
pub created_at: u64,
}
impl PartialOrd for NoteRef {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
impl Ord for NoteRef {
fn cmp(&self, other: &Self) -> Ordering {
match self.created_at.cmp(&other.created_at) {
Ordering::Equal => self.key.cmp(&other.key).into(),
Ordering::Less => Some(Ordering::Greater),
Ordering::Greater => Some(Ordering::Less),
Ordering::Equal => self.key.cmp(&other.key),
Ordering::Less => Ordering::Greater,
Ordering::Greater => Ordering::Less,
}
}
}
impl Ord for NoteRef {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).unwrap()
impl PartialOrd for NoteRef {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
@@ -60,8 +60,7 @@ struct Timeline {
impl Timeline {
pub fn new(filter: Vec<Filter>) -> Self {
let mut notes: Vec<NoteRef> = vec![];
notes.reserve(1000);
let notes: Vec<NoteRef> = Vec::with_capacity(1000);
let subscription: Option<Subscription> = None;
Timeline {
@@ -215,47 +214,46 @@ fn get_unknown_note_ids<'a>(
let blocks = ndb.get_blocks_by_key(txn, note_key)?;
for block in blocks.iter(note) {
let _blocktype = block.blocktype();
match block.blocktype() {
BlockType::MentionBech32 => match block.as_mention().unwrap() {
Mention::Pubkey(npub) => {
if ndb.get_profile_by_pubkey(txn, npub.pubkey()).is_err() {
ids.insert(UnknownId::Pubkey(npub.pubkey()));
}
}
Mention::Profile(nprofile) => {
if ndb.get_profile_by_pubkey(txn, nprofile.pubkey()).is_err() {
ids.insert(UnknownId::Pubkey(nprofile.pubkey()));
}
}
Mention::Event(ev) => match ndb.get_note_by_id(txn, ev.id()) {
Err(_) => {
ids.insert(UnknownId::Id(ev.id()));
if let Some(pk) = ev.pubkey() {
if ndb.get_profile_by_pubkey(txn, pk).is_err() {
ids.insert(UnknownId::Pubkey(pk));
}
}
}
Ok(note) => {
if ndb.get_profile_by_pubkey(txn, note.pubkey()).is_err() {
ids.insert(UnknownId::Pubkey(note.pubkey()));
}
}
},
Mention::Note(note) => match ndb.get_note_by_id(txn, note.id()) {
Err(_) => {
ids.insert(UnknownId::Id(note.id()));
}
Ok(note) => {
if ndb.get_profile_by_pubkey(txn, note.pubkey()).is_err() {
ids.insert(UnknownId::Pubkey(note.pubkey()));
}
}
},
_ => {}
},
if block.blocktype() != BlockType::MentionBech32 {
continue;
}
match block.as_mention().unwrap() {
Mention::Pubkey(npub) => {
if ndb.get_profile_by_pubkey(txn, npub.pubkey()).is_err() {
ids.insert(UnknownId::Pubkey(npub.pubkey()));
}
}
Mention::Profile(nprofile) => {
if ndb.get_profile_by_pubkey(txn, nprofile.pubkey()).is_err() {
ids.insert(UnknownId::Pubkey(nprofile.pubkey()));
}
}
Mention::Event(ev) => match ndb.get_note_by_id(txn, ev.id()) {
Err(_) => {
ids.insert(UnknownId::Id(ev.id()));
if let Some(pk) = ev.pubkey() {
if ndb.get_profile_by_pubkey(txn, pk).is_err() {
ids.insert(UnknownId::Pubkey(pk));
}
}
}
Ok(note) => {
if ndb.get_profile_by_pubkey(txn, note.pubkey()).is_err() {
ids.insert(UnknownId::Pubkey(note.pubkey()));
}
}
},
Mention::Note(note) => match ndb.get_note_by_id(txn, note.id()) {
Err(_) => {
ids.insert(UnknownId::Id(note.id()));
}
Ok(note) => {
if ndb.get_profile_by_pubkey(txn, note.pubkey()).is_err() {
ids.insert(UnknownId::Pubkey(note.pubkey()));
}
}
},
_ => {}
}
}
@@ -275,15 +273,15 @@ fn poll_notes_for_timeline<'a>(
return Err(Error::NoActiveSubscription);
};
let new_note_ids = damus.ndb.poll_for_notes(&sub, 100);
if new_note_ids.len() > 0 {
let new_note_ids = damus.ndb.poll_for_notes(sub, 100);
if !new_note_ids.is_empty() {
debug!("{} new notes! {:?}", new_note_ids.len(), new_note_ids);
}
let new_refs = new_note_ids
let new_refs: Vec<NoteRef> = new_note_ids
.iter()
.map(|key| {
let note = damus.ndb.get_note_by_key(&txn, *key).expect("no note??");
let note = damus.ndb.get_note_by_key(txn, *key).expect("no note??");
let _ = get_unknown_note_ids(&damus.ndb, txn, &note, *key, ids);
@@ -310,7 +308,7 @@ fn setup_initial_nostrdb_subs(damus: &mut Damus) -> Result<()> {
let filters: Vec<nostrdb::Filter> = timeline
.filter
.iter()
.map(|f| crate::filter::convert_enostr_filter(f))
.map(crate::filter::convert_enostr_filter)
.collect();
timeline.subscription = Some(damus.ndb.subscribe(filters.clone())?);
let txn = Transaction::new(&damus.ndb)?;
@@ -357,7 +355,7 @@ fn process_event(damus: &mut Damus, _subid: &str, event: &str) {
puffin::profile_function!();
//info!("processing event {}", event);
if let Err(_err) = damus.ndb.process_event(&event) {
if let Err(_err) = damus.ndb.process_event(event) {
error!("error processing event {}", event);
}
}
@@ -370,7 +368,7 @@ fn get_unknown_ids<'a>(txn: &'a Transaction, damus: &Damus) -> Result<Vec<Unknow
for timeline in &damus.timelines {
for noteref in &timeline.notes {
let note = damus.ndb.get_note_by_key(&txn, noteref.key)?;
let note = damus.ndb.get_note_by_key(txn, noteref.key)?;
let _ = get_unknown_note_ids(&damus.ndb, txn, &note, note.key().unwrap(), &mut ids);
}
}
@@ -378,7 +376,7 @@ fn get_unknown_ids<'a>(txn: &'a Transaction, damus: &Damus) -> Result<Vec<Unknow
Ok(ids.into_iter().collect())
}
fn get_unknown_ids_filter<'a>(ids: &[UnknownId<'a>]) -> Option<Vec<Filter>> {
fn get_unknown_ids_filter(ids: &[UnknownId<'_>]) -> Option<Vec<Filter>> {
if ids.is_empty() {
return None;
}
@@ -427,11 +425,11 @@ fn handle_eose(damus: &mut Damus, subid: &str, relay_url: &str) -> Result<()> {
fn process_message(damus: &mut Damus, relay: &str, msg: &RelayMessage) {
match msg {
RelayMessage::Event(subid, ev) => process_event(damus, &subid, ev),
RelayMessage::Event(subid, ev) => process_event(damus, subid, ev),
RelayMessage::Notice(msg) => warn!("Notice from {}: {}", relay, msg),
RelayMessage::OK(cr) => info!("OK {:?}", cr),
RelayMessage::Eose(sid) => {
if let Err(err) = handle_eose(damus, &sid, relay) {
if let Err(err) = handle_eose(damus, sid, relay) {
error!("error handling eose: {}", err);
}
}
@@ -474,11 +472,11 @@ impl Damus {
let _initial_limit = 100;
if args.len() > 1 {
for arg in &args[1..] {
let filter = serde_json::from_str(&arg).unwrap();
let filter = serde_json::from_str(arg).unwrap();
timelines.push(Timeline::new(filter));
}
} else {
let filter = serde_json::from_str(&include_str!("../queries/global.json")).unwrap();
let filter = serde_json::from_str(include_str!("../queries/global.json")).unwrap();
timelines.push(Timeline::new(filter));
};
@@ -604,11 +602,12 @@ fn timeline_view(ui: &mut egui::Ui, app: &mut Damus, timeline: usize) {
}
fn top_panel(ctx: &egui::Context) -> egui::TopBottomPanel {
let mut top_margin = Margin::default();
top_margin.top = 4.0;
top_margin.left = 8.0;
top_margin.right = 8.0;
//top_margin.bottom = -20.0;
let top_margin = egui::Margin {
top: 4.0,
left: 8.0,
right: 8.0,
..Default::default()
};
let frame = Frame {
inner_margin: top_margin,
@@ -621,7 +620,7 @@ fn top_panel(ctx: &egui::Context) -> egui::TopBottomPanel {
.show_separator_line(false)
}
fn render_panel<'a>(ctx: &egui::Context, app: &'a mut Damus, timeline_ind: usize) {
fn render_panel(ctx: &egui::Context, app: &mut Damus, timeline_ind: usize) {
top_panel(ctx).show(ctx, |ui| {
ui.with_layout(egui::Layout::right_to_left(egui::Align::TOP), |ui| {
ui.visuals_mut().button_frame = false;

View File

@@ -7,14 +7,14 @@ pub enum NamedFontFamily {
}
impl NamedFontFamily {
pub fn as_str(self) -> &'static str {
pub fn as_str(&mut self) -> &'static str {
match self {
//Self::Bold => "bold",
Self::Medium => "medium",
}
}
pub fn as_family(self) -> egui::FontFamily {
pub fn as_family(&mut self) -> egui::FontFamily {
egui::FontFamily::Name(self.as_str().into())
}
}

View File

@@ -59,7 +59,7 @@ pub fn round_image(image: &mut ColorImage) {
}
fn process_pfp_bitmap(size: u32, image: &mut image::DynamicImage) -> ColorImage {
#[cfg(features = "profiling")]
#[cfg(feature = "profiling")]
puffin::profile_function!();
// Crop square
@@ -144,7 +144,7 @@ pub fn fetch_img(
size: u32,
) -> Promise<Result<TextureHandle>> {
let key = ImageCache::key(url);
let path = img_cache.cache_dir.join(&key);
let path = img_cache.cache_dir.join(key);
if path.exists() {
fetch_img_from_disk(ctx, url, &path)

View File

@@ -26,8 +26,12 @@ impl ImageCache {
}
pub fn write(cache_dir: &path::Path, url: &str, data: ColorImage) -> Result<()> {
let file_path = cache_dir.join(&Self::key(url));
let file = File::options().write(true).create(true).open(file_path)?;
let file_path = cache_dir.join(Self::key(url));
let file = File::options()
.write(true)
.create(true)
.truncate(true)
.open(file_path)?;
let encoder = image::codecs::webp::WebPEncoder::new_lossless(file);
encoder.encode(

View File

@@ -4,6 +4,7 @@ use poll_promise::Promise;
/// Helper storage object for retrieving the plaintext key from the user and converting it into a
/// nostr-sdk Keys object if possible.
#[derive(Default)]
pub struct LoginManager {
pub login_key: String,
pub promise: Option<Promise<Result<Keys, LoginError>>>,
@@ -13,11 +14,6 @@ pub struct LoginManager {
impl LoginManager {
pub fn new() -> Self {
LoginManager {
login_key: String::new(),
promise: None,
error: None,
key_on_error: None,
}
LoginManager::default()
}
}

View File

@@ -18,6 +18,6 @@ impl NoteCache {
}
pub fn reltime_str(&mut self) -> &str {
return &self.reltime.get();
return self.reltime.get();
}
}

View File

@@ -6,7 +6,7 @@ pub fn time_ago_since(timestamp: u64) -> String {
.duration_since(UNIX_EPOCH)
.expect("Time went backwards")
.as_secs();
let duration = now.checked_sub(timestamp).unwrap_or(0);
let duration = now.saturating_sub(timestamp);
let years = duration / 31_536_000; // seconds in a year
if years >= 1 {

View File

@@ -1,4 +1,4 @@
pub fn merge_sorted_vecs<T: Ord + Copy>(vec1: &Vec<T>, vec2: &Vec<T>) -> Vec<T> {
pub fn merge_sorted_vecs<T: Ord + Copy>(vec1: &[T], vec2: &[T]) -> Vec<T> {
let mut merged = Vec::with_capacity(vec1.len() + vec2.len());
let mut i = 0;
let mut j = 0;

View File

@@ -228,7 +228,7 @@ fn render_pfp(ui: &mut egui::Ui, damus: &mut Damus, url: &str) {
}
}
fn pfp_image<'a>(ui: &mut egui::Ui, img: &TextureHandle, size: f32) -> egui::Response {
fn pfp_image(ui: &mut egui::Ui, img: &TextureHandle, size: f32) -> egui::Response {
#[cfg(feature = "profiling")]
puffin::profile_function!();

View File

@@ -24,18 +24,18 @@ impl NoteOptions {
#[inline]
pub fn set_note_previews(&mut self, enable: bool) {
if enable {
*self = *self | NoteOptions::note_previews;
*self |= NoteOptions::note_previews;
} else {
*self = *self & !NoteOptions::note_previews;
*self &= !NoteOptions::note_previews;
}
}
#[inline]
pub fn set_actionbar(&mut self, enable: bool) {
if enable {
*self = *self | NoteOptions::actionbar;
*self |= NoteOptions::actionbar;
} else {
*self = *self & !NoteOptions::actionbar;
*self &= !NoteOptions::actionbar;
}
}
}