Skip to content

Commit a232626

Browse files
committed
rustc: Extract --crate-type parsing to its own function
Helpful for users of rustc as a library.
1 parent 4ec97fb commit a232626

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

src/librustc/driver/config.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -593,24 +593,10 @@ fn parse_cfgspecs(cfgspecs: Vec<String> ) -> ast::CrateConfig {
593593
}
594594

595595
pub fn build_session_options(matches: &getopts::Matches) -> Options {
596-
let mut crate_types: Vec<CrateType> = Vec::new();
596+
597597
let unparsed_crate_types = matches.opt_strs("crate-type");
598-
for unparsed_crate_type in unparsed_crate_types.iter() {
599-
for part in unparsed_crate_type.as_slice().split(',') {
600-
let new_part = match part {
601-
"lib" => default_lib_output(),
602-
"rlib" => CrateTypeRlib,
603-
"staticlib" => CrateTypeStaticlib,
604-
"dylib" => CrateTypeDylib,
605-
"bin" => CrateTypeExecutable,
606-
_ => {
607-
early_error(format!("unknown crate type: `{}`",
608-
part).as_slice())
609-
}
610-
};
611-
crate_types.push(new_part)
612-
}
613-
}
598+
let crate_types = parse_crate_types_from_list(unparsed_crate_types)
599+
.unwrap_or_else(|e| early_error(e.as_slice()));
614600

615601
let parse_only = matches.opt_present("parse-only");
616602
let no_trans = matches.opt_present("no-trans");
@@ -804,6 +790,29 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
804790
}
805791
}
806792

793+
pub fn parse_crate_types_from_list(crate_types_list_list: Vec<String>) -> Result<Vec<CrateType>, String> {
794+
795+
let mut crate_types: Vec<CrateType> = Vec::new();
796+
for unparsed_crate_type in crate_types_list_list.iter() {
797+
for part in unparsed_crate_type.as_slice().split(',') {
798+
let new_part = match part {
799+
"lib" => default_lib_output(),
800+
"rlib" => CrateTypeRlib,
801+
"staticlib" => CrateTypeStaticlib,
802+
"dylib" => CrateTypeDylib,
803+
"bin" => CrateTypeExecutable,
804+
_ => {
805+
return Err(format!("unknown crate type: `{}`",
806+
part));
807+
}
808+
};
809+
crate_types.push(new_part)
810+
}
811+
}
812+
813+
return Ok(crate_types);
814+
}
815+
807816
impl fmt::Show for CrateType {
808817
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
809818
match *self {

0 commit comments

Comments
 (0)