-
Notifications
You must be signed in to change notification settings - Fork 615
/
Copy pathdependency.rs
65 lines (59 loc) · 1.85 KB
/
dependency.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use diesel::deserialize::{self, FromSql};
use diesel::pg::Pg;
use diesel::sql_types::Integer;
use crate::models::{Crate, Version};
use crate::schema::*;
use cargo_registry_index::DependencyKind as IndexDependencyKind;
#[derive(Identifiable, Associations, Debug, Queryable, QueryableByName)]
#[belongs_to(Version)]
#[belongs_to(Crate)]
#[table_name = "dependencies"]
pub struct Dependency {
pub id: i32,
pub version_id: i32,
pub crate_id: i32,
pub req: String,
pub optional: bool,
pub default_features: bool,
pub features: Vec<String>,
pub target: Option<String>,
pub kind: DependencyKind,
}
#[derive(Debug, QueryableByName)]
pub struct ReverseDependency {
#[diesel(embed)]
pub dependency: Dependency,
#[sql_type = "::diesel::sql_types::Integer"]
pub crate_downloads: i32,
#[sql_type = "::diesel::sql_types::Text"]
#[column_name = "crate_name"]
pub name: String,
}
#[derive(Copy, Clone, Serialize, Deserialize, Debug, FromSqlRow)]
#[serde(rename_all = "lowercase")]
#[repr(u32)]
pub enum DependencyKind {
Normal = 0,
Build = 1,
Dev = 2,
// if you add a kind here, be sure to update `from_row` below.
}
impl From<DependencyKind> for IndexDependencyKind {
fn from(dk: DependencyKind) -> Self {
match dk {
DependencyKind::Normal => IndexDependencyKind::Normal,
DependencyKind::Build => IndexDependencyKind::Build,
DependencyKind::Dev => IndexDependencyKind::Dev,
}
}
}
impl FromSql<Integer, Pg> for DependencyKind {
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
match <i32 as FromSql<Integer, Pg>>::from_sql(bytes)? {
0 => Ok(DependencyKind::Normal),
1 => Ok(DependencyKind::Build),
2 => Ok(DependencyKind::Dev),
n => Err(format!("unknown kind: {n}").into()),
}
}
}