|
| 1 | +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +pub use package_path::{RemotePath, LocalPath, normalize, hash}; |
| 12 | +use extra::semver; |
| 13 | +use core::prelude::*; |
| 14 | +use core::result; |
| 15 | + |
| 16 | +/// Placeholder |
| 17 | +pub fn default_version() -> Version { ExactRevision(0.1) } |
| 18 | + |
| 19 | +/// Path-fragment identifier of a package such as |
| 20 | +/// 'github.com/graydon/test'; path must be a relative |
| 21 | +/// path with >=1 component. |
| 22 | +pub struct PkgId { |
| 23 | + /// Remote path: for example, github.com/mozilla/quux-whatever |
| 24 | + remote_path: RemotePath, |
| 25 | + /// Local path: for example, /home/quux/github.com/mozilla/quux_whatever |
| 26 | + /// Note that '-' normalizes to '_' when mapping a remote path |
| 27 | + /// onto a local path |
| 28 | + /// Also, this will change when we implement #6407, though we'll still |
| 29 | + /// need to keep track of separate local and remote paths |
| 30 | + local_path: LocalPath, |
| 31 | + /// Short name. This is the local path's filestem, but we store it |
| 32 | + /// redundantly so as to not call get() everywhere (filestem() returns an |
| 33 | + /// option) |
| 34 | + short_name: ~str, |
| 35 | + version: Version |
| 36 | +} |
| 37 | + |
| 38 | +pub impl PkgId { |
| 39 | + fn new(s: &str) -> PkgId { |
| 40 | + use conditions::bad_pkg_id::cond; |
| 41 | + |
| 42 | + let p = Path(s); |
| 43 | + if p.is_absolute { |
| 44 | + return cond.raise((p, ~"absolute pkgid")); |
| 45 | + } |
| 46 | + if p.components.len() < 1 { |
| 47 | + return cond.raise((p, ~"0-length pkgid")); |
| 48 | + } |
| 49 | + let remote_path = RemotePath(p); |
| 50 | + let local_path = normalize(copy remote_path); |
| 51 | + let short_name = (copy local_path).filestem().expect(fmt!("Strange path! %s", s)); |
| 52 | + PkgId { |
| 53 | + local_path: local_path, |
| 54 | + remote_path: remote_path, |
| 55 | + short_name: short_name, |
| 56 | + version: default_version() |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + fn hash(&self) -> ~str { |
| 61 | + fmt!("%s-%s-%s", self.remote_path.to_str(), |
| 62 | + hash(self.remote_path.to_str() + self.version.to_str()), |
| 63 | + self.version.to_str()) |
| 64 | + } |
| 65 | + |
| 66 | + fn short_name_with_version(&self) -> ~str { |
| 67 | + fmt!("%s-%s", self.short_name, self.version.to_str()) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl ToStr for PkgId { |
| 72 | + fn to_str(&self) -> ~str { |
| 73 | + // should probably use the filestem and not the whole path |
| 74 | + fmt!("%s-%s", self.local_path.to_str(), self.version.to_str()) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/// A version is either an exact revision, |
| 79 | +/// or a semantic version |
| 80 | +pub enum Version { |
| 81 | + ExactRevision(float), |
| 82 | + SemVersion(semver::Version) |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +impl Ord for Version { |
| 87 | + fn lt(&self, other: &Version) -> bool { |
| 88 | + match (self, other) { |
| 89 | + (&ExactRevision(f1), &ExactRevision(f2)) => f1 < f2, |
| 90 | + (&SemVersion(ref v1), &SemVersion(ref v2)) => v1 < v2, |
| 91 | + _ => false // incomparable, really |
| 92 | + } |
| 93 | + } |
| 94 | + fn le(&self, other: &Version) -> bool { |
| 95 | + match (self, other) { |
| 96 | + (&ExactRevision(f1), &ExactRevision(f2)) => f1 <= f2, |
| 97 | + (&SemVersion(ref v1), &SemVersion(ref v2)) => v1 <= v2, |
| 98 | + _ => false // incomparable, really |
| 99 | + } |
| 100 | + } |
| 101 | + fn ge(&self, other: &Version) -> bool { |
| 102 | + match (self, other) { |
| 103 | + (&ExactRevision(f1), &ExactRevision(f2)) => f1 > f2, |
| 104 | + (&SemVersion(ref v1), &SemVersion(ref v2)) => v1 > v2, |
| 105 | + _ => false // incomparable, really |
| 106 | + } |
| 107 | + } |
| 108 | + fn gt(&self, other: &Version) -> bool { |
| 109 | + match (self, other) { |
| 110 | + (&ExactRevision(f1), &ExactRevision(f2)) => f1 >= f2, |
| 111 | + (&SemVersion(ref v1), &SemVersion(ref v2)) => v1 >= v2, |
| 112 | + _ => false // incomparable, really |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | +} |
| 117 | + |
| 118 | +impl ToStr for Version { |
| 119 | + fn to_str(&self) -> ~str { |
| 120 | + match *self { |
| 121 | + ExactRevision(ref n) => n.to_str(), |
| 122 | + SemVersion(ref v) => v.to_str() |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +pub fn parse_vers(vers: ~str) -> result::Result<semver::Version, ~str> { |
| 128 | + match semver::parse(vers) { |
| 129 | + Some(vers) => result::Ok(vers), |
| 130 | + None => result::Err(~"could not parse version: invalid") |
| 131 | + } |
| 132 | +} |
0 commit comments