Debian Codegraph @

podcasts-data/src/models/mod.rs

raw symbols SCIP
1 // mod.rs
2 //
3 // Copyright 2017 Jordan Petridis <[email protected]>
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // SPDX-License-Identifier: GPL-3.0-or-later
19
20 mod new_episode;
21 mod new_show;
22 mod new_source;
23
24 mod discovery_settings;
25 mod episode;
26 mod show;
27 mod source;
28
29 pub(crate) use self::discovery_settings::DiscoverySetting;
30
31 pub(crate) use self::new_episode::{NewEpisode, NewEpisodeMinimal};
32 pub(crate) use self::new_show::NewShow;
33 pub(crate) use self::new_source::NewSource;
34
35 #[cfg(test)]
36 pub(crate) use self::new_episode::NewEpisodeBuilder;
37 #[cfg(test)]
38 pub(crate) use self::new_show::NewShowBuilder;
39
40 pub use self::episode::{
41 Episode, EpisodeCleanerModel, EpisodeId, EpisodeMinimal, EpisodeModel, EpisodeWidgetModel,
42 };
43 pub use self::show::{Show, ShowCoverModel, ShowId};
44 pub use self::source::{Source, SourceId};
45
46 #[derive(Debug, Clone, PartialEq)]
47 pub enum IndexState<T, ID> {
48 Index(T),
49 Update((T, ID)),
50 NotChanged,
51 }
52
53 pub(crate) trait Insert<T> {
54 type Error;
55
56 fn insert(&self) -> Result<T, Self::Error>;
57 }
58
59 pub trait Update<T, ID> {
60 type Error;
61
62 fn update(&self, _: ID) -> Result<T, Self::Error>;
63 }
64
65 // This might need to change in the future
66 pub trait Index<T, ID>: Insert<T> + Update<T, ID> {
67 type Error;
68
69 fn index(&self) -> Result<T, <Self as Index<T, ID>>::Error>;
70 }
71
72 /// FIXME: DOCS
73 pub trait Save<T> {
74 /// The Error type to be returned.
75 type Error;
76 /// Helper method to easily save/"sync" current state of a diesel model to
77 /// the Database.
78 fn save(&self) -> Result<T, Self::Error>;
79 }
80
81 /// Allows to use struct wrappers instead of i32 Id types.
82 #[macro_export]
83 macro_rules! make_id_wrapper {
84 ($type_name:ident) => {
85 use diesel::backend::Backend;
86 use diesel::deserialize::{self, FromSql};
87 use diesel::serialize::{self, Output, ToSql};
88 use diesel::sql_types::Integer;
89 use diesel::sqlite::Sqlite;
90 #[derive(AsExpression, FromSqlRow, Debug, PartialEq, Eq, Hash, Clone, Copy, Default)]
91 #[diesel(sql_type = diesel::sql_types::Integer)]
92 pub struct $type_name(pub i32);
93
94 impl<DB> FromSql<Integer, DB> for $type_name
95 where
96 DB: Backend,
97 i32: FromSql<Integer, DB>,
98 {
99 fn from_sql(bytes: DB::RawValue<'_>) -> deserialize::Result<Self> {
100 i32::from_sql(bytes).map($type_name)
101 }
102 }
103
104 impl ToSql<diesel::sql_types::Integer, Sqlite> for $type_name {
105 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
106 <i32 as ToSql<Integer, Sqlite>>::to_sql(&self.0, out)
107 }
108 }
109 };
110 }