/src/dependency.rs

https://gitlab.com/alx741/crates.io · Rust · 122 lines · 109 code · 10 blank · 3 comment · 2 complexity · a49eaf68e1cc10e33c6ba45911a16423 MD5 · raw file

  1. use pg::GenericConnection;
  2. use pg::rows::Row;
  3. use semver;
  4. use Model;
  5. use git;
  6. use util::{CargoResult};
  7. pub struct Dependency {
  8. pub id: i32,
  9. pub version_id: i32,
  10. pub crate_id: i32,
  11. pub req: semver::VersionReq,
  12. pub optional: bool,
  13. pub default_features: bool,
  14. pub features: Vec<String>,
  15. pub target: Option<String>,
  16. pub kind: Kind,
  17. }
  18. #[derive(RustcEncodable, RustcDecodable)]
  19. pub struct EncodableDependency {
  20. pub id: i32,
  21. pub version_id: i32,
  22. pub crate_id: String,
  23. pub req: String,
  24. pub optional: bool,
  25. pub default_features: bool,
  26. pub features: String,
  27. pub target: Option<String>,
  28. pub kind: Kind,
  29. }
  30. #[derive(Copy, Clone)]
  31. // NB: this order is important, it must be retained! The database stores an
  32. // integer corresponding to each variant.
  33. pub enum Kind {
  34. Normal,
  35. Build,
  36. Dev,
  37. // if you add a kind here, be sure to update `from_row` below.
  38. }
  39. impl Dependency {
  40. pub fn insert(conn: &GenericConnection, version_id: i32, crate_id: i32,
  41. req: &semver::VersionReq, kind: Kind,
  42. optional: bool, default_features: bool,
  43. features: &[String], target: &Option<String>)
  44. -> CargoResult<Dependency> {
  45. let req = req.to_string();
  46. let features = features.join(",");
  47. let stmt = try!(conn.prepare("INSERT INTO dependencies
  48. (version_id, crate_id, req, optional,
  49. default_features, features, target, kind)
  50. VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
  51. RETURNING *"));
  52. let rows = try!(stmt.query(&[&version_id, &crate_id, &req,
  53. &optional, &default_features,
  54. &features, target, &(kind as i32)]));
  55. Ok(Model::from_row(&rows.iter().next().unwrap()))
  56. }
  57. pub fn git_encode(&self, crate_name: &str) -> git::Dependency {
  58. let Dependency { id: _, version_id: _, crate_id: _, ref req,
  59. optional, default_features, ref features,
  60. ref target, kind } = *self;
  61. git::Dependency {
  62. name: crate_name.to_string(),
  63. req: req.to_string(),
  64. features: features.clone(),
  65. optional: optional,
  66. default_features: default_features,
  67. target: target.clone(),
  68. kind: Some(kind),
  69. }
  70. }
  71. pub fn encodable(self, crate_name: &str) -> EncodableDependency {
  72. let Dependency { id, version_id, crate_id: _, req, optional,
  73. default_features, features, target, kind } = self;
  74. EncodableDependency {
  75. id: id,
  76. version_id: version_id,
  77. crate_id: crate_name.to_string(),
  78. req: req.to_string(),
  79. optional: optional,
  80. default_features: default_features,
  81. features: features.join(","),
  82. target: target,
  83. kind: kind,
  84. }
  85. }
  86. }
  87. impl Model for Dependency {
  88. fn from_row(row: &Row) -> Dependency {
  89. let features: String = row.get("features");
  90. let req: String = row.get("req");
  91. let kind: Option<i32> = row.get("kind");
  92. Dependency {
  93. id: row.get("id"),
  94. version_id: row.get("version_id"),
  95. crate_id: row.get("crate_id"),
  96. req: semver::VersionReq::parse(&req).unwrap(),
  97. optional: row.get("optional"),
  98. default_features: row.get("default_features"),
  99. features: features.split(',').filter(|s| !s.is_empty())
  100. .map(|s| s.to_string())
  101. .collect(),
  102. target: row.get("target"),
  103. kind: match kind.unwrap_or(0) {
  104. 0 => Kind::Normal,
  105. 1 => Kind::Build,
  106. 2 => Kind::Dev,
  107. n => panic!("unknown kind: {}", n),
  108. }
  109. }
  110. }
  111. fn table_name(_: Option<Dependency>) -> &'static str { "dependencies" }
  112. }