Skip to content

Commit 5114942

Browse files
Clean code a bit
1 parent a8df3f6 commit 5114942

File tree

1 file changed

+161
-166
lines changed

1 file changed

+161
-166
lines changed

src/docbuilder/rustwide_builder.rs

Lines changed: 161 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -476,31 +476,19 @@ impl RustwideBuilder {
476476
Ok(())
477477
}
478478

479-
fn get_coverage(
479+
fn make_build_object<T, F: Fn(Vec<&str>, Vec<String>, LogStorage) -> Result<T>>(
480480
&self,
481481
target: &str,
482-
build: &Build,
483482
metadata: &Metadata,
484483
limits: &Limits,
485-
) -> Option<(i32, i32)> {
486-
let rustdoc_flags: Vec<String> = vec![
487-
"-Z".to_string(),
488-
"unstable-options".to_string(),
489-
"--static-root-path".to_string(),
490-
"/".to_string(),
491-
"--cap-lints".to_string(),
492-
"warn".to_string(),
493-
"--output-format".to_string(),
494-
"json".to_string(),
495-
"--show-coverage".to_string(),
496-
];
497-
484+
f: F,
485+
) -> Result<T> {
498486
let mut cargo_args = vec!["doc", "--lib", "--no-deps"];
499487
if target != HOST_TARGET {
500488
// If the explicit target is not a tier one target, we need to install it.
501489
if !TARGETS.contains(&target) {
502490
// This is a no-op if the target is already installed.
503-
self.toolchain.add_target(&self.workspace, target).ok()?;
491+
self.toolchain.add_target(&self.workspace, target)?;
504492
}
505493
cargo_args.push("--target");
506494
cargo_args.push(target);
@@ -528,52 +516,84 @@ impl RustwideBuilder {
528516
let mut storage = LogStorage::new(LevelFilter::Info);
529517
storage.set_max_size(limits.max_log_size());
530518

531-
let mut json = String::new();
532-
if build
533-
.cargo()
534-
.timeout(Some(limits.timeout()))
535-
.no_output_timeout(None)
536-
.env(
537-
"RUSTFLAGS",
538-
metadata
539-
.rustc_args
540-
.as_ref()
541-
.map(|args| args.join(" "))
542-
.unwrap_or_default(),
543-
)
544-
.env("RUSTDOCFLAGS", rustdoc_flags.join(" "))
545-
// For docs.rs detection from build script:
546-
// https://github.com/rust-lang/docs.rs/issues/147
547-
.env("DOCS_RS", "1")
548-
.args(&cargo_args)
549-
.log_output(false)
550-
.process_lines(&mut |line, _| {
551-
if line.starts_with('{') && line.ends_with('}') {
552-
json = line.to_owned();
553-
}
554-
})
555-
.run()
556-
.is_ok()
557-
{
558-
match serde_json::from_str(&json).expect("conversion failed...") {
559-
Value::Object(m) => {
560-
let mut total = 0;
561-
let mut documented = 0;
562-
for entry in m.values() {
563-
if let Some(Value::Number(n)) = entry.get("total") {
564-
total += n.as_i64().unwrap_or(0) as i32;
519+
let rustdoc_flags: Vec<String> = vec![
520+
"-Z".to_string(),
521+
"unstable-options".to_string(),
522+
"--static-root-path".to_string(),
523+
"/".to_string(),
524+
"--cap-lints".to_string(),
525+
"warn".to_string(),
526+
];
527+
528+
f(cargo_args, rustdoc_flags, storage)
529+
}
530+
531+
fn get_coverage(
532+
&self,
533+
target: &str,
534+
build: &Build,
535+
metadata: &Metadata,
536+
limits: &Limits,
537+
) -> Result<Option<(i32, i32)>> {
538+
self.make_build_object(
539+
target,
540+
metadata,
541+
limits,
542+
|cargo_args, mut rustdoc_flags, _| {
543+
rustdoc_flags.extend(vec![
544+
"--output-format".to_string(),
545+
"json".to_string(),
546+
"--show-coverage".to_string(),
547+
]);
548+
549+
let mut json = String::new();
550+
if build
551+
.cargo()
552+
.timeout(Some(limits.timeout()))
553+
.no_output_timeout(None)
554+
.env(
555+
"RUSTFLAGS",
556+
metadata
557+
.rustc_args
558+
.as_ref()
559+
.map(|args| args.join(" "))
560+
.unwrap_or_default(),
561+
)
562+
.env("RUSTDOCFLAGS", rustdoc_flags.join(" "))
563+
// For docs.rs detection from build script:
564+
// https://github.com/rust-lang/docs.rs/issues/147
565+
.env("DOCS_RS", "1")
566+
.args(&cargo_args)
567+
.log_output(false)
568+
.process_lines(&mut |line, _| {
569+
if line.starts_with('{') && line.ends_with('}') {
570+
json = line.to_owned();
565571
}
566-
if let Some(Value::Number(n)) = entry.get("with_docs") {
567-
documented += n.as_i64().unwrap_or(0) as i32;
572+
})
573+
.run()
574+
.is_ok()
575+
{
576+
match serde_json::from_str(&json).expect("conversion failed...") {
577+
Value::Object(m) => {
578+
let mut total = 0;
579+
let mut documented = 0;
580+
for entry in m.values() {
581+
if let Some(Value::Number(n)) = entry.get("total") {
582+
total += n.as_i64().unwrap_or(0) as i32;
583+
}
584+
if let Some(Value::Number(n)) = entry.get("with_docs") {
585+
documented += n.as_i64().unwrap_or(0) as i32;
586+
}
587+
}
588+
Ok(Some((total, documented)))
568589
}
590+
_ => Ok(None),
569591
}
570-
Some((total, documented))
592+
} else {
593+
Ok(None)
571594
}
572-
_ => None,
573-
}
574-
} else {
575-
None
576-
}
595+
},
596+
)
577597
}
578598

579599
fn execute_build(
@@ -584,118 +604,93 @@ impl RustwideBuilder {
584604
limits: &Limits,
585605
metadata: &Metadata,
586606
) -> Result<FullBuildResult> {
587-
let cargo_metadata =
588-
CargoMetadata::load(&self.workspace, &self.toolchain, &build.host_source_dir())?;
589-
590-
let mut rustdoc_flags: Vec<String> = vec![
591-
"-Z".to_string(),
592-
"unstable-options".to_string(),
593-
"--resource-suffix".to_string(),
594-
format!("-{}", parse_rustc_version(&self.rustc_version)?),
595-
"--static-root-path".to_string(),
596-
"/".to_string(),
597-
"--cap-lints".to_string(),
598-
"warn".to_string(),
599-
];
600-
for dep in &cargo_metadata.root_dependencies() {
601-
rustdoc_flags.push("--extern-html-root-url".to_string());
602-
rustdoc_flags.push(format!(
603-
"{}=https://docs.rs/{}/{}",
604-
dep.name.replace("-", "_"),
605-
dep.name,
606-
dep.version
607-
));
608-
}
609-
if let Some(package_rustdoc_args) = &metadata.rustdoc_args {
610-
rustdoc_flags.append(&mut package_rustdoc_args.iter().map(|s| s.to_owned()).collect());
611-
}
612-
let mut cargo_args = vec!["doc", "--lib", "--no-deps"];
613-
if target != HOST_TARGET {
614-
// If the explicit target is not a tier one target, we need to install it.
615-
if !TARGETS.contains(&target) {
616-
// This is a no-op if the target is already installed.
617-
self.toolchain.add_target(&self.workspace, target)?;
618-
}
619-
cargo_args.push("--target");
620-
cargo_args.push(target);
621-
};
622-
623-
let tmp_jobs;
624-
if let Some(cpu_limit) = self.cpu_limit {
625-
tmp_jobs = format!("-j{}", cpu_limit);
626-
cargo_args.push(&tmp_jobs);
627-
}
628-
629-
let tmp;
630-
if let Some(features) = &metadata.features {
631-
cargo_args.push("--features");
632-
tmp = features.join(" ");
633-
cargo_args.push(&tmp);
634-
}
635-
if metadata.all_features {
636-
cargo_args.push("--all-features");
637-
}
638-
if metadata.no_default_features {
639-
cargo_args.push("--no-default-features");
640-
}
607+
self.make_build_object(
608+
target,
609+
metadata,
610+
limits,
611+
|cargo_args, mut rustdoc_flags, storage| {
612+
rustdoc_flags.extend(vec![
613+
"--resource-suffix".to_string(),
614+
format!("-{}", parse_rustc_version(&self.rustc_version)?),
615+
]);
616+
let cargo_metadata = CargoMetadata::load(
617+
&self.workspace,
618+
&self.toolchain,
619+
&build.host_source_dir(),
620+
)?;
641621

642-
let mut storage = LogStorage::new(LevelFilter::Info);
643-
storage.set_max_size(limits.max_log_size());
622+
for dep in &cargo_metadata.root_dependencies() {
623+
rustdoc_flags.push("--extern-html-root-url".to_string());
624+
rustdoc_flags.push(format!(
625+
"{}=https://docs.rs/{}/{}",
626+
dep.name.replace("-", "_"),
627+
dep.name,
628+
dep.version
629+
));
630+
}
631+
if let Some(package_rustdoc_args) = &metadata.rustdoc_args {
632+
rustdoc_flags
633+
.append(&mut package_rustdoc_args.iter().map(|s| s.to_owned()).collect());
634+
}
644635

645-
let successful = logging::capture(&storage, || {
646-
build
647-
.cargo()
648-
.timeout(Some(limits.timeout()))
649-
.no_output_timeout(None)
650-
.env(
651-
"RUSTFLAGS",
652-
metadata
653-
.rustc_args
654-
.as_ref()
655-
.map(|args| args.join(" "))
656-
.unwrap_or_default(),
657-
)
658-
.env("RUSTDOCFLAGS", rustdoc_flags.join(" "))
659-
// For docs.rs detection from build script:
660-
// https://github.com/rust-lang/docs.rs/issues/147
661-
.env("DOCS_RS", "1")
662-
.args(&cargo_args)
663-
.run()
664-
.is_ok()
665-
});
666-
let mut total_items = None;
667-
let mut documented_items = None;
668-
if successful {
669-
if let Some((total, documented)) = self.get_coverage(target, build, metadata, limits) {
670-
total_items = Some(total);
671-
documented_items = Some(documented);
672-
}
673-
}
674-
// If we're passed a default_target which requires a cross-compile,
675-
// cargo will put the output in `target/<target>/doc`.
676-
// However, if this is the default build, we don't want it there,
677-
// we want it in `target/doc`.
678-
if target != HOST_TARGET && is_default_target {
679-
// mv target/$target/doc target/doc
680-
let target_dir = build.host_target_dir();
681-
let old_dir = target_dir.join(target).join("doc");
682-
let new_dir = target_dir.join("doc");
683-
debug!("rename {} to {}", old_dir.display(), new_dir.display());
684-
std::fs::rename(old_dir, new_dir)?;
685-
}
636+
let successful = logging::capture(&storage, || {
637+
build
638+
.cargo()
639+
.timeout(Some(limits.timeout()))
640+
.no_output_timeout(None)
641+
.env(
642+
"RUSTFLAGS",
643+
metadata
644+
.rustc_args
645+
.as_ref()
646+
.map(|args| args.join(" "))
647+
.unwrap_or_default(),
648+
)
649+
.env("RUSTDOCFLAGS", rustdoc_flags.join(" "))
650+
// For docs.rs detection from build script:
651+
// https://github.com/rust-lang/docs.rs/issues/147
652+
.env("DOCS_RS", "1")
653+
.args(&cargo_args)
654+
.run()
655+
.is_ok()
656+
});
657+
let mut total_items = None;
658+
let mut documented_items = None;
659+
if successful {
660+
if let Some((total, documented)) =
661+
self.get_coverage(target, build, metadata, limits)?
662+
{
663+
total_items = Some(total);
664+
documented_items = Some(documented);
665+
}
666+
}
667+
// If we're passed a default_target which requires a cross-compile,
668+
// cargo will put the output in `target/<target>/doc`.
669+
// However, if this is the default build, we don't want it there,
670+
// we want it in `target/doc`.
671+
if target != HOST_TARGET && is_default_target {
672+
// mv target/$target/doc target/doc
673+
let target_dir = build.host_target_dir();
674+
let old_dir = target_dir.join(target).join("doc");
675+
let new_dir = target_dir.join("doc");
676+
debug!("rename {} to {}", old_dir.display(), new_dir.display());
677+
std::fs::rename(old_dir, new_dir)?;
678+
}
686679

687-
Ok(FullBuildResult {
688-
result: BuildResult {
689-
build_log: storage.to_string(),
690-
rustc_version: self.rustc_version.clone(),
691-
docsrs_version: format!("docsrs {}", crate::BUILD_VERSION),
692-
successful,
693-
total_items,
694-
documented_items,
680+
Ok(FullBuildResult {
681+
result: BuildResult {
682+
build_log: storage.to_string(),
683+
rustc_version: self.rustc_version.clone(),
684+
docsrs_version: format!("docsrs {}", crate::BUILD_VERSION),
685+
successful,
686+
total_items,
687+
documented_items,
688+
},
689+
cargo_metadata,
690+
target: target.to_string(),
691+
})
695692
},
696-
cargo_metadata,
697-
target: target.to_string(),
698-
})
693+
)
699694
}
700695

701696
fn copy_docs(

0 commit comments

Comments
 (0)