Skip to content

Commit dcecc66

Browse files
Cleanup librustpkg a little bit.
Mostly I did simple transformations from imperative style loops to more functional iterator based transformations.
1 parent f08851e commit dcecc66

File tree

3 files changed

+77
-74
lines changed

3 files changed

+77
-74
lines changed

src/librustpkg/package_source.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,19 @@ impl PkgSrc {
5252
use conditions::nonexistent_package::cond;
5353

5454
debug!("Pushing onto root: %s | %s", self.id.path.to_str(), self.root.to_str());
55-
let dir;
55+
5656
let dirs = pkgid_src_in_workspace(&self.id, &self.root);
5757
debug!("Checking dirs: %?", dirs);
5858
let path = dirs.iter().find(|&d| os::path_exists(d));
59-
match path {
60-
Some(d) => dir = (*d).clone(),
61-
None => dir = match self.fetch_git() {
59+
60+
let dir = match path {
61+
Some(d) => (*d).clone(),
62+
None => match self.fetch_git() {
63+
Some(d) => d,
6264
None => cond.raise((self.id.clone(), ~"supplied path for package dir does not \
63-
exist, and couldn't interpret it as a URL fragment")),
64-
Some(d) => d
65+
exist, and couldn't interpret it as a URL fragment"))
6566
}
66-
}
67+
};
6768
if !os::path_is_dir(&dir) {
6869
cond.raise((self.id.clone(), ~"supplied path for package dir is a \
6970
non-directory"));
@@ -145,26 +146,26 @@ impl PkgSrc {
145146
let prefix = dir.components.len();
146147
debug!("Matching against %?", self.id.short_name);
147148
do os::walk_dir(&dir) |pth| {
148-
match pth.filename() {
149-
Some(~"lib.rs") => PkgSrc::push_crate(&mut self.libs,
150-
prefix,
151-
pth),
152-
Some(~"main.rs") => PkgSrc::push_crate(&mut self.mains,
153-
prefix,
154-
pth),
155-
Some(~"test.rs") => PkgSrc::push_crate(&mut self.tests,
156-
prefix,
157-
pth),
158-
Some(~"bench.rs") => PkgSrc::push_crate(&mut self.benchs,
159-
prefix,
160-
pth),
161-
_ => ()
149+
let maybe_known_crate_set = match pth.filename() {
150+
Some(filename) => match filename {
151+
~"lib.rs" => Some(&mut self.libs),
152+
~"main.rs" => Some(&mut self.mains),
153+
~"test.rs" => Some(&mut self.tests),
154+
~"bench.rs" => Some(&mut self.benchs),
155+
_ => None
156+
},
157+
_ => None
158+
};
159+
160+
match maybe_known_crate_set {
161+
Some(crate_set) => PkgSrc::push_crate(crate_set, prefix, pth),
162+
None => ()
162163
}
163164
true
164165
};
165166

166-
if self.libs.is_empty() && self.mains.is_empty()
167-
&& self.tests.is_empty() && self.benchs.is_empty() {
167+
let crate_sets = [&self.libs, &self.mains, &self.tests, &self.benchs];
168+
if crate_sets.iter().all(|crate_set| crate_set.is_empty()) {
168169

169170
note("Couldn't infer any crates to build.\n\
170171
Try naming a crate `main.rs`, `lib.rs`, \

src/librustpkg/path_util.rs

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use std::libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR};
1919
use std::os::mkdir_recursive;
2020
use std::os;
2121
use messages::*;
22-
use package_id::*;
2322

2423
pub fn default_workspace() -> Path {
2524
let p = rust_path();
@@ -51,35 +50,34 @@ pub fn make_dir_rwx(p: &Path) -> bool { os::make_dir(p, U_RWX) }
5150
/// pkgid's short name
5251
pub fn workspace_contains_package_id(pkgid: &PkgId, workspace: &Path) -> bool {
5352
let src_dir = workspace.push("src");
53+
5454
let mut found = false;
5555
do os::walk_dir(&src_dir) |p| {
5656
debug!("=> p = %s", p.to_str());
57-
if os::path_is_dir(p) {
57+
58+
let was_found = os::path_is_dir(p) && {
5859
debug!("p = %s, path = %s [%s]", p.to_str(), pkgid.path.to_str(),
59-
src_dir.push_rel(&pkgid.path).to_str());
60+
src_dir.push_rel(&pkgid.path).to_str());
6061

61-
if *p == src_dir.push_rel(&pkgid.path) {
62-
found = true;
63-
}
64-
else {
62+
*p == src_dir.push_rel(&pkgid.path) || {
6563
let pf = p.filename();
66-
for pf in pf.iter() {
67-
let f_ = (*pf).clone();
68-
let g = f_.to_str();
64+
do pf.iter().any |pf| {
65+
let g = pf.to_str();
6966
match split_version_general(g, '-') {
67+
None => false,
7068
Some((ref might_match, ref vers)) => {
7169
debug!("might_match = %s, vers = %s", *might_match,
72-
vers.to_str());
73-
if *might_match == pkgid.short_name
74-
&& (*vers == pkgid.version || pkgid.version == NoVersion)
75-
{
76-
found = true;
77-
}
70+
vers.to_str());
71+
*might_match == pkgid.short_name
72+
&& (pkgid.version == *vers || pkgid.version == NoVersion)
7873
}
79-
None => ()
80-
}
74+
}
8175
}
8276
}
77+
};
78+
79+
if was_found {
80+
found = true
8381
}
8482
true
8583
};
@@ -102,12 +100,9 @@ pub fn pkgid_src_in_workspace(pkgid: &PkgId, workspace: &Path) -> ~[Path] {
102100
/// Returns a src for pkgid that does exist -- None if none of them do
103101
pub fn first_pkgid_src_in_workspace(pkgid: &PkgId, workspace: &Path) -> Option<Path> {
104102
let rs = pkgid_src_in_workspace(pkgid, workspace);
105-
for p in rs.iter() {
106-
if os::path_exists(p) {
107-
return Some((*p).clone());
108-
}
109-
}
110-
None
103+
do rs.iter().find |&p| {
104+
os::path_exists(p)
105+
}.map(|p| (**p).clone())
111106
}
112107

113108
/// Figure out what the executable name for <pkgid> in <workspace>'s build
@@ -195,22 +190,31 @@ pub fn library_in_workspace(path: &Path, short_name: &str, where: Target,
195190

196191
debug!("lib_prefix = %s and lib_filetype = %s", lib_prefix, lib_filetype);
197192

198-
let mut result_filename = None;
199-
for p in dir_contents.iter() {
200-
let mut which = 0;
201-
let mut hash = None;
202-
let p_path = Path((*p).clone());
203-
let extension = p_path.filetype();
193+
// Find a filename that matches the pattern:
194+
// (lib_prefix)-hash-(version)(lib_suffix)
195+
let paths = do dir_contents.iter().map |p| {
196+
Path((*p).clone())
197+
};
198+
199+
let mut libraries = do paths.filter |p| {
200+
let extension = p.filetype();
204201
debug!("p = %s, p's extension is %?", p.to_str(), extension);
205202
match extension {
206-
Some(ref s) if lib_filetype == *s => (),
207-
_ => loop
203+
None => false,
204+
Some(ref s) => lib_filetype == *s
208205
}
206+
};
207+
208+
let mut result_filename = None;
209+
for p_path in libraries {
209210
// Find a filename that matches the pattern: (lib_prefix)-hash-(version)(lib_suffix)
210211
// and remember what the hash was
211212
let f_name = match p_path.filename() {
212213
Some(s) => s, None => loop
213214
};
215+
216+
let mut hash = None;
217+
let mut which = 0;
214218
for piece in f_name.split_iter('-') {
215219
debug!("a piece = %s", piece);
216220
if which == 0 && piece != lib_prefix {
@@ -229,26 +233,27 @@ pub fn library_in_workspace(path: &Path, short_name: &str, where: Target,
229233
break;
230234
}
231235
}
236+
232237
if hash.is_some() {
233238
result_filename = Some(p_path);
234239
break;
235240
}
236241
}
237242

243+
if result_filename.is_none() {
244+
warn(fmt!("library_in_workspace didn't find a library in %s for %s",
245+
dir_to_search.to_str(), short_name));
246+
}
247+
238248
// Return the filename that matches, which we now know exists
239249
// (if result_filename != None)
240-
match result_filename {
241-
None => {
242-
warn(fmt!("library_in_workspace didn't find a library in %s for %s",
243-
dir_to_search.to_str(), short_name));
244-
None
245-
}
246-
Some(result_filename) => {
247-
let absolute_path = dir_to_search.push_rel(&result_filename);
248-
debug!("result_filename = %s", absolute_path.to_str());
249-
Some(absolute_path)
250-
}
251-
}
250+
let abs_path = do result_filename.map |result_filename| {
251+
let absolute_path = dir_to_search.push_rel(result_filename);
252+
debug!("result_filename = %s", absolute_path.to_str());
253+
absolute_path
254+
};
255+
256+
abs_path
252257
}
253258

254259
/// Returns the executable that would be installed for <pkgid>

src/librustpkg/search.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@ use path_util::installed_library_in_workspace;
1313
/// If a library with path `p` matching pkg_id's name exists under sroot_opt,
1414
/// return Some(p). Return None if there's no such path or if sroot_opt is None.
1515
pub fn find_library_in_search_path(sroot_opt: Option<@Path>, short_name: &str) -> Option<Path> {
16-
match sroot_opt {
17-
Some(sroot) => {
18-
debug!("Will search for a library with short name %s in \
19-
%s", short_name, (sroot.push("lib")).to_str());
20-
installed_library_in_workspace(short_name, sroot)
21-
}
22-
None => None
16+
do sroot_opt.chain |sroot| {
17+
debug!("Will search for a library with short name %s in \
18+
%s", short_name, (sroot.push("lib")).to_str());
19+
installed_library_in_workspace(short_name, sroot)
2320
}
2421
}

0 commit comments

Comments
 (0)