Skip to content

Commit 8a8be49

Browse files
committed
feat: Allow usage of MagnetLink in sea_orm::Model
1 parent 31753da commit 8a8be49

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ sha256 = "1.5"
2626
rustc-hex = "2.1"
2727
serde = { version = "1", features = [ "derive" ] }
2828
fluent-uri = { version = "0.4", features = [ "serde" ] }
29+
# For Sea-ORM integration
30+
sea-orm = { version = "2.0.0-rc.18", optional = true }
2931

3032
[dev-dependencies]
3133
serde_json = "1"
3234

3335
[features]
3436
magnet_force_name = []
3537
unknown_tracker_scheme = []
38+
sea_orm = [ "dep:sea-orm" ]
39+
test_sea_orm = [ "dep:sea-orm", "sea-orm/sqlx-sqlite" ]
3640

3741
[[test]]
3842
name = "magnet_force_name"
@@ -45,3 +49,9 @@ name = "unknown_tracker_scheme"
4549
path = "tests/unknown_tracker_scheme.rs"
4650
required-features = [ "unknown_tracker_scheme" ]
4751
test = true
52+
53+
[[test]]
54+
name = "sea_orm"
55+
path = "tests/sea_orm.rs"
56+
required-features = [ "sea_orm" ]
57+
test = true

src/magnet.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ impl std::error::Error for MagnetLinkError {
119119
/// More information is specified in [BEP-0009](https://bittorrent.org/beps/bep_0009.html), and
120120
/// even more appears in the wild, as explained [on Wikipedia](https://en.wikipedia.org/wiki/Magnet_URI_scheme).
121121
#[derive(Clone, Debug)]
122+
// #[derive(sea_orm::DeriveValueType)]
123+
// #[sea_orm(value_type = "String")]
122124
pub struct MagnetLink {
123125
/// Only mandatory field for magnet link parsing, unless the
124126
/// `magnet_force_name` crate feature is enabled.
@@ -322,6 +324,70 @@ impl std::fmt::Display for MagnetLink {
322324
}
323325
}
324326

327+
impl PartialEq for MagnetLink {
328+
fn eq(&self, other: &Self) -> bool {
329+
self.query == other.query
330+
}
331+
}
332+
333+
#[cfg(feature = "sea_orm")]
334+
impl From<MagnetLink> for sea_orm::sea_query::Value {
335+
fn from(m: MagnetLink) -> Self {
336+
Self::String(Some(m.to_string()))
337+
}
338+
}
339+
340+
#[cfg(feature = "sea_orm")]
341+
impl sea_orm::TryGetable for MagnetLink {
342+
fn try_get_by<I: sea_orm::ColIdx>(
343+
res: &sea_orm::QueryResult,
344+
index: I,
345+
) -> Result<Self, sea_orm::error::TryGetError> {
346+
let val: String = res.try_get_by(index)?;
347+
MagnetLink::new(&val).map_err(|e| {
348+
sea_orm::error::TryGetError::DbErr(sea_orm::DbErr::TryIntoErr {
349+
from: "String",
350+
into: "MagnetLink",
351+
source: std::sync::Arc::new(e),
352+
})
353+
})
354+
}
355+
}
356+
357+
#[cfg(feature = "sea_orm")]
358+
impl sea_orm::sea_query::ValueType for MagnetLink {
359+
fn try_from(v: sea_orm::Value) -> Result<Self, sea_orm::sea_query::ValueTypeErr> {
360+
match v {
361+
// TODO: What to do with None String?
362+
// This should probably work with Option<MagnetLink> but not with MagnetLink
363+
// but i have no idea how sea orm works...
364+
sea_orm::Value::String(Some(s)) => {
365+
MagnetLink::new(&s).map_err(|_e| sea_orm::sea_query::ValueTypeErr)
366+
}
367+
_ => Err(sea_orm::sea_query::ValueTypeErr),
368+
}
369+
}
370+
371+
fn type_name() -> String {
372+
"MagnetLink".to_string()
373+
}
374+
375+
fn array_type() -> sea_orm::sea_query::ArrayType {
376+
sea_orm::sea_query::ArrayType::String
377+
}
378+
379+
fn column_type() -> sea_orm::sea_query::ColumnType {
380+
sea_orm::sea_query::ColumnType::String(sea_orm::sea_query::table::StringLen::None)
381+
}
382+
}
383+
384+
#[cfg(feature = "sea_orm")]
385+
impl sea_orm::sea_query::Nullable for MagnetLink {
386+
fn null() -> sea_orm::sea_query::Value {
387+
sea_orm::sea_query::Value::String(None)
388+
}
389+
}
390+
325391
#[cfg(test)]
326392
mod tests {
327393
use super::*;

tests/sea_orm.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use hightorrent::MagnetLink;
2+
use sea_orm::entity::prelude::*;
3+
4+
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
5+
#[sea_orm(table_name = "magnet")]
6+
pub struct Model {
7+
#[sea_orm(primary_key)]
8+
pub id: i32,
9+
pub torrent_id: String,
10+
pub magnet: MagnetLink,
11+
pub name: String,
12+
pub resolved: bool,
13+
}
14+
15+
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
16+
pub enum Relation {}
17+
18+
#[async_trait::async_trait]
19+
impl ActiveModelBehavior for ActiveModel {}

0 commit comments

Comments
 (0)