Skip to content

Commit

Permalink
refactor: drop model module
Browse files Browse the repository at this point in the history
  • Loading branch information
SeaDve committed Jan 27, 2024
1 parent ec0744e commit 39ac43a
Show file tree
Hide file tree
Showing 26 changed files with 64 additions and 88 deletions.
5 changes: 3 additions & 2 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use crate::{
database::{self, EnvExt, Migrations},
database_error_window::DatabaseErrorWindow,
inspector_page::InspectorPage,
model::SongList,
preferences_window::PreferencesWindow,
recognizer::Recordings,
settings::Settings,
song_list::SongList,
window::Window,
};

Expand Down Expand Up @@ -251,7 +251,8 @@ fn init_env() -> Result<(heed::Env, SongList, Recordings)> {

use crate::{
database::SONG_LIST_DB_NAME,
model::{Song, Uid, UidCodec},
song::Song,
uid::{Uid, UidCodec},
};

if let Some(db) = env.open_database::<SerdeBincode<Uid>, SerdeBincode<Song>>(
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,20 @@ mod config;
mod database;
mod database_error_window;
mod date_time;
mod external_link;
mod external_links;
mod i18n;
mod inspector_page;
mod model;
mod player;
mod preferences_window;
mod recognizer;
mod serde_helpers;
mod settings;
mod song;
mod song_filter;
mod song_list;
mod song_sorter;
mod uid;
mod utils;
mod window;

Expand Down
22 changes: 0 additions & 22 deletions src/model/mod.rs

This file was deleted.

6 changes: 1 addition & 5 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ use mpris_server::{

use std::cell::{Cell, OnceCell, RefCell};

use crate::{
config::APP_ID,
model::{Song, Uid},
utils,
};
use crate::{config::APP_ID, song::Song, uid::Uid, utils};

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, glib::Enum)]
#[enum_type(name = "MsaiPlayerState")]
Expand Down
2 changes: 1 addition & 1 deletion src/recognizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use crate::{
audio_device::{self, AudioDeviceClass},
cancelled::Cancelled,
date_time::DateTime,
model::Song,
settings::PreferredAudioSource,
song::Song,
utils,
};

Expand Down
9 changes: 3 additions & 6 deletions src/recognizer/provider/aud_d/mod.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
mod mock;
mod response;

use std::time::Duration;

use async_trait::async_trait;
use gtk::{gio, glib};
use serde_json::json;
use soup::prelude::*;

use std::time::Duration;

pub use self::mock::AudDMock;
use self::response::Response;
use super::{Provider, RecognizeError, RecognizeErrorKind};
use crate::{
model::{ExternalLinkKey, Song, Uid},
utils,
};
use crate::{external_links::ExternalLinkKey, song::Song, uid::Uid, utils};

#[derive(Debug)]
pub struct AudD {
Expand Down
2 changes: 1 addition & 1 deletion src/recognizer/provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use self::{
error::{RecognizeError, RecognizeErrorKind},
settings::{ProviderSettings, ProviderType, TestProviderMode},
};
use crate::model::Song;
use crate::song::Song;

#[async_trait(?Send)]
pub trait Provider: fmt::Debug {
Expand Down
2 changes: 1 addition & 1 deletion src/recognizer/recording.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use std::cell::{Cell, OnceCell, RefCell};

use super::RecognizeError;
use crate::{date_time::DateTime, model::Song, serde_helpers};
use crate::{date_time::DateTime, serde_helpers, song::Song};

#[derive(Debug, Clone, PartialEq, Eq, glib::Boxed, Serialize, Deserialize)]
#[boxed_type(name = "MsaiBoxedRecognizeResult", nullable)]
Expand Down
7 changes: 2 additions & 5 deletions src/recognizer/recordings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::{
use super::Recording;
use crate::{
database::{EnvExt, RECORDINGS_DB_NAME},
model::{Uid, UidCodec},
uid::{Uid, UidCodec},
utils,
};

Expand Down Expand Up @@ -249,10 +249,7 @@ mod tests {
use std::rc::Rc;

use crate::{
database,
date_time::DateTime,
model::{Song, Uid},
recognizer::recording::BoxedRecognizeResult,
database, date_time::DateTime, recognizer::recording::BoxedRecognizeResult, song::Song,
};

fn new_test_recording(bytes: &'static [u8]) -> Recording {
Expand Down
24 changes: 17 additions & 7 deletions src/model/song.rs → src/song.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use anyhow::Result;
use gtk::{glib, prelude::*, subclass::prelude::*};
use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
use gtk::{
glib::{self, once_cell::sync::Lazy},
prelude::*,
subclass::prelude::*,
};
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};

use std::{
Expand All @@ -10,8 +15,10 @@ use std::{
use crate::{
album_art::AlbumArt,
date_time::DateTime,
model::{ExternalLinkKey, ExternalLinks, Uid},
serde_helpers, utils,
external_links::{ExternalLinkKey, ExternalLinks},
serde_helpers,
uid::Uid,
utils,
};

mod imp {
Expand Down Expand Up @@ -105,9 +112,12 @@ impl Song {
SongBuilder::new(id, title, artist, album)
}

/// String to match to when searching for self.
pub fn search_term(&self) -> String {
format!("{} {}", self.artist(), self.title())
/// Returns the score of song against the pattern.
pub fn fuzzy_match(&self, pattern: &str) -> Option<i64> {
static FUZZY_MATCHER: Lazy<SkimMatcherV2> = Lazy::new(SkimMatcherV2::default);

let choice = format!("{} {}", self.artist(), self.title());
FUZZY_MATCHER.fuzzy_match(&choice, pattern)
}

/// String copied to clipboard when copying self.
Expand Down Expand Up @@ -235,7 +245,7 @@ impl SongBuilder {
mod test {
use super::*;

use crate::model::ExternalLink;
use crate::external_link::ExternalLink;

#[test]
fn id_ref() {
Expand Down
13 changes: 5 additions & 8 deletions src/model/song_filter.rs → src/song_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
// SPDX-FileCopyrightText: 2023 Dave Patrick Caberto
// SPDX-License-Identifier: GPL-3.0-or-later

use fuzzy_matcher::FuzzyMatcher;
use gtk::{glib, prelude::*, subclass::prelude::*};

use std::cell::RefCell;

use super::{Song, FUZZY_MATCHER};
use gtk::{glib, prelude::*, subclass::prelude::*};

use crate::song::Song;

mod imp {
use super::*;
Expand Down Expand Up @@ -49,9 +48,7 @@ mod imp {
if search.is_empty() {
true
} else {
FUZZY_MATCHER
.fuzzy_match(&song.search_term(), &search)
.is_some()
song.fuzzy_match(&search).is_some()
}
}
}
Expand Down Expand Up @@ -107,7 +104,7 @@ mod tests {

use std::{cell::RefCell, rc::Rc};

use crate::model::Uid;
use crate::uid::Uid;

#[gtk::test]
fn strictness() {
Expand Down
5 changes: 3 additions & 2 deletions src/model/song_list.rs → src/song_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ use std::{
time::Instant,
};

use super::{Song, Uid, UidCodec};
use crate::{
database::{EnvExt, SONG_LIST_DB_NAME},
song::Song,
uid::{Uid, UidCodec},
utils,
};

Expand Down Expand Up @@ -295,7 +296,7 @@ fn unbind_song_from_db(song: &Song) {

/// Migrate from the old memory list of Mousai v0.6.6 and earlier.
fn migrate_from_memory_list(song_list: &SongList) -> Result<()> {
use crate::{date_time::DateTime, model::ExternalLinkKey, settings::Settings};
use crate::{date_time::DateTime, external_links::ExternalLinkKey, settings::Settings};

let settings = Settings::default();
let memory_list = settings.memory_list();
Expand Down
15 changes: 7 additions & 8 deletions src/model/song_sorter.rs → src/song_sorter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
// SPDX-FileCopyrightText: 2023 Dave Patrick Caberto
// SPDX-License-Identifier: GPL-3.0-or-later

use fuzzy_matcher::FuzzyMatcher;
use gtk::{glib, prelude::*, subclass::prelude::*};

use std::cell::RefCell;

use super::{Song, FUZZY_MATCHER};
use gtk::{glib, prelude::*, subclass::prelude::*};

use crate::song::Song;

mod imp {
use super::*;
Expand Down Expand Up @@ -44,9 +43,9 @@ mod imp {
if search.is_empty() {
song_2.last_heard().cmp(&song_1.last_heard()).into()
} else {
let song_1_score = FUZZY_MATCHER.fuzzy_match(&song_1.search_term(), &search);
let song_2_score = FUZZY_MATCHER.fuzzy_match(&song_2.search_term(), &search);
song_2_score.cmp(&song_1_score).into()
let score_1 = song_1.fuzzy_match(&search);
let score_2 = song_2.fuzzy_match(&search);
score_2.cmp(&score_1).into()
}
}

Expand Down Expand Up @@ -92,7 +91,7 @@ impl Default for SongSorter {
mod tests {
use super::*;

use crate::{date_time::DateTime, model::Uid};
use crate::{date_time::DateTime, uid::Uid};

fn new_test_song(last_heard: DateTime, search_term: &str) -> Song {
let song = Song::builder(&Uid::from(""), search_term, search_term, "").build();
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/window/album_cover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use gtk::{

use std::cell::RefCell;

use crate::{model::Song, utils};
use crate::{song::Song, utils};

const DEFAULT_ENABLE_CROSSFADE: bool = true;

Expand Down
2 changes: 1 addition & 1 deletion src/window/crossfade_paintable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use gtk::{

use std::cell::{Cell, OnceCell, RefCell};

use crate::{model::Song, utils};
use crate::{song::Song, utils};

const FADE_ANIMATION_DURATION_MS: u32 = 800;
const INITIAL_FADE_PROGRESS: f64 = 1.0;
Expand Down
6 changes: 1 addition & 5 deletions src/window/external_link_tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ use gtk::{gio, glib, prelude::*, subclass::prelude::*};

use std::{cell::OnceCell, str::FromStr};

use crate::{
i18n::gettext_f,
model::{ExternalLink, ExternalLinkKey},
utils,
};
use crate::{external_link::ExternalLink, external_links::ExternalLinkKey, i18n::gettext_f, utils};

mod imp {
use super::*;
Expand Down
8 changes: 2 additions & 6 deletions src/window/history_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ use super::{
song_tile::SongTile, AdaptiveMode,
};
use crate::{
config::APP_ID,
i18n::ngettext_f,
model::{Song, SongFilter, SongList, SongSorter, Uid},
player::Player,
recognizer::Recognizer,
utils,
config::APP_ID, i18n::ngettext_f, player::Player, recognizer::Recognizer, song::Song,
song_filter::SongFilter, song_list::SongList, song_sorter::SongSorter, uid::Uid, utils,
};

// FIXME Missing global navigation shortcuts
Expand Down
3 changes: 2 additions & 1 deletion src/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ use std::cell::OnceCell;
use self::{history_view::HistoryView, recognizer_view::RecognizerView, song_bar::SongBar};
use crate::{
config::PROFILE,
model::{Song, SongList},
player::{Player, PlayerState},
preferences_window::PreferencesWindow,
recognizer::{RecognizeError, RecognizeErrorKind, Recognizer, RecognizerState, Recordings},
song::Song,
song_list::SongList,
utils, Application,
};

Expand Down
2 changes: 1 addition & 1 deletion src/window/recognized_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use gtk::glib::{self, clone, closure_local};
use std::cell::{Cell, OnceCell, RefCell};

use super::{recognized_page_tile::RecognizedPageTile, AdaptiveMode};
use crate::{i18n::ngettext_f, model::Song, player::Player};
use crate::{i18n::ngettext_f, player::Player, song::Song};

mod imp {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion src/window/recognized_page_tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use gtk::{
use std::cell::RefCell;

use super::song_tile::SongTile;
use crate::{date_time::DateTime, model::Song, player::Player};
use crate::{date_time::DateTime, player::Player, song::Song};

mod imp {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion src/window/song_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use super::{
playback_button::{PlaybackButton, PlaybackButtonMode},
};
use crate::{
model::Song,
player::{Player, PlayerState},
song::Song,
};

const BACKGROUND_BLUR_RADIUS: f64 = 80.0;
Expand Down
3 changes: 2 additions & 1 deletion src/window/song_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ use super::{
AdaptiveMode,
};
use crate::{
model::{Song, SongList},
player::{Player, PlayerState},
song::Song,
song_list::SongList,
utils,
};

Expand Down
Loading

0 comments on commit 39ac43a

Please sign in to comment.