-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'rustdoc-v21' into cp_rustdoc-v21_e25d3dd
- Loading branch information
Showing
3 changed files
with
46 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use std::fs::read_to_string; | ||
|
||
use anyhow::Context; | ||
use rustdoc_types::Crate; | ||
|
||
#[derive(serde::Deserialize)] | ||
struct RustdocFormatVersion { | ||
format_version: u32, | ||
} | ||
|
||
pub(crate) fn detect_rustdoc_format_version(file_data: &str) -> anyhow::Result<u32> { | ||
let version = serde_json::from_str::<RustdocFormatVersion>(file_data) | ||
.with_context(|| "file does not appear to be a rustdoc JSON format".to_string())?; | ||
|
||
Ok(version.format_version) | ||
} | ||
|
||
pub(crate) fn load_pregenerated_rustdoc(crate_name: &str) -> Crate { | ||
let path = format!("./localdata/test_data/{crate_name}/rustdoc.json"); | ||
let content = read_to_string(&path) | ||
.with_context(|| format!("Could not load {path} file, did you forget to run ./scripts/regenerate_test_rustdocs.sh ?")) | ||
.expect("failed to load rustdoc"); | ||
serde_json::from_str(&content) | ||
.with_context(|| { | ||
let format_version = detect_rustdoc_format_version(&content); | ||
match format_version { | ||
Ok(format_version) => { | ||
format!( | ||
"Failed to parse {path} file: it is rustdoc version {format_version} but expected {}. \ | ||
Did you forget to run ./scripts/regenerate_test_rustdocs.sh ?", | ||
rustdoc_types::FORMAT_VERSION, | ||
) | ||
} | ||
Err(..) => { | ||
format!( | ||
"Failed to parse {path} file: it didn't seem to be valid rustdoc JSON. \ | ||
Did you forget to run ./scripts/regenerate_test_rustdocs.sh ?" | ||
) | ||
} | ||
} | ||
}).expect("failed to parse rustdoc JSON") | ||
} |