about summary refs log tree commit diff
path: root/src/librustpkg/package_path.rs
blob: 4ba9c8066e4f3ac9e88e2e57faae8f1648c8fa20 (plain)
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
66
67
68
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// rustpkg utilities having to do with local and remote paths

use std::clone::Clone;
use std::hash::Streaming;
use std::hash;
use std::option::Some;
use std::path::Path;
use std::rt::io::Writer;

/// Wrappers to prevent local and remote paths from getting confused
/// (These will go away after #6407)
pub struct RemotePath (Path);

impl Clone for RemotePath {
    fn clone(&self) -> RemotePath {
        RemotePath((**self).clone())
    }
}

pub struct LocalPath (Path);

impl Clone for LocalPath {
    fn clone(&self) -> LocalPath {
        LocalPath((**self).clone())
    }
}


// normalize should be the only way to construct a LocalPath
// (though this isn't enforced)
/// Replace all occurrences of '-' in the stem part of path with '_'
/// This is because we treat rust-foo-bar-quux and rust_foo_bar_quux
/// as the same name
pub fn normalize(p_: RemotePath) -> LocalPath {
    let RemotePath(p) = p_;
    match p.filestem() {
        None => LocalPath(p),
        Some(st) => {
            let replaced = st.replace("-", "_");
            if replaced != st {
                LocalPath(p.with_filestem(replaced))
            }
            else {
                LocalPath(p)
            }
        }
    }
}

pub fn write<W: Writer>(writer: &mut W, string: &str) {
    writer.write(string.as_bytes());
}

pub fn hash(data: ~str) -> ~str {
    let hasher = &mut hash::default_state();
    write(hasher, data);
    hasher.result_str()
}