Skip to content

Commit 8242fee

Browse files
authored
Merge pull request steveklabnik#117 from euclio/fixes
Miscellaneous fixes
2 parents 84c6d8c + 9113ca1 commit 8242fee

File tree

5 files changed

+41
-47
lines changed

5 files changed

+41
-47
lines changed

src/cargo.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use error::*;
1111
///
1212
/// ## Arguments
1313
///
14-
/// - manifest_path: The path containing the `Cargo.toml` of the crate
14+
/// - `manifest_path`: The path containing the `Cargo.toml` of the crate
1515
pub fn generate_analysis(manifest_path: &Path) -> Result<()> {
1616
// FIXME: Here we assume that we are documenting a library. This could be wrong, but it's the
1717
// common case, and it ensures that we are documenting the right target in the case that the
@@ -45,7 +45,7 @@ pub fn generate_analysis(manifest_path: &Path) -> Result<()> {
4545
///
4646
/// ## Arguments
4747
///
48-
/// - manifest_path: The path to the location of `Cargo.toml` of the crate being documented
48+
/// - `manifest_path`: The path to the location of `Cargo.toml` of the crate being documented
4949
pub fn crate_name_from_manifest_path(manifest_path: &Path) -> Result<String> {
5050
let mut command = Command::new("cargo");
5151

@@ -76,7 +76,7 @@ pub fn crate_name_from_manifest_path(manifest_path: &Path) -> Result<String> {
7676
///
7777
/// ## Arguments
7878
///
79-
/// - metadata: The JSON metadata of the crate.
79+
/// - `metadata`: The JSON metadata of the crate.
8080
fn crate_name_from_metadata(metadata: &serde_json::Value) -> Result<String> {
8181
let targets = match metadata["packages"][0]["targets"].as_array() {
8282
Some(targets) => targets,

src/json.rs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::collections::HashMap;
55
// Sizes for the HashMaps to avoid reallocation and large HashMap sizes when we know
66
// the upper limit for them
77

8-
/// Number of possible values in the enum item::Metadata
8+
/// Number of possible values in the enum `analysis::DefKind`
99
pub const METADATA_SIZE: usize = 14;
1010

1111
/// Under each item in data.relationships how many fields are there. For now we only have one which
@@ -44,48 +44,44 @@ const DATA_SIZE: usize = 1;
4444
/// }
4545
/// }
4646
/// ]
47-
///}
48-
///```
49-
///
50-
/// ## Fields
51-
///
52-
/// - `data`: The top level crate information and documentation
53-
/// - `included`: The rest of the crate information and documentation
54-
#[derive(Serialize, Debug)]
47+
/// }
48+
/// ```
49+
#[derive(Debug, Default, Serialize)]
5550
pub struct Documentation {
51+
/// The top level crate information and documentation
5652
data: Option<Document>,
53+
54+
/// The rest of the crate information and documentation
5755
included: Option<Vec<Document>>,
5856
}
5957

6058
/// A sub type of the `Documentation` struct. It contains the majority of the data. It can be used
6159
/// for both the `data` and `included` field in the serialized JSON.
62-
///
63-
/// ## Fields
64-
///
65-
/// - `ty`: The type of the the item (e.g. "crate", "function", "enum", etc.)
66-
/// - `id`: The unique identifier associated with this item
67-
/// - `attributes`: The attributes associated with the item like documentation or it's name
68-
/// - `relationships`: An optional field used to show the relationship between the crate to the
69-
/// otheritems in the crate
70-
#[derive(Serialize, Debug)]
60+
#[derive(Debug, Default, Serialize)]
7161
pub struct Document {
7262
#[serde(rename = "type")]
63+
/// The type of the item (e.g. "crate", "function", "enum", etc.)
7364
ty: String,
65+
66+
/// The unique identifier associated with this item
7467
id: String,
68+
69+
/// The attributes associated with the item, like documentation or its name
7570
attributes: HashMap<String, String>,
71+
72+
/// An optional field used to show the relationship between the crate to the other items in the
73+
/// crate
7674
relationships: Option<HashMap<String, HashMap<String, Vec<Data>>>>,
7775
}
7876

7977
/// Used to populate the `relationships` `data` field in the serialized JSON
80-
///
81-
/// ## Fields
82-
///
83-
/// - `ty`: The type of the the item (e.g. "crate", "function", "enum", etc.)
84-
/// - `id`: The unique identifier associated with this item
85-
#[derive(Serialize, Debug)]
78+
#[derive(Debug, Default, Serialize)]
8679
pub struct Data {
8780
#[serde(rename = "type")]
81+
/// The type of the item (e.g. "crate", "function", "enum", etc.)
8882
ty: String,
83+
84+
/// The unique identifier associated with this item
8985
id: String,
9086
}
9187

src/lib.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,15 @@ use json::*;
3636
pub use error::{Error, ErrorKind};
3737

3838
/// A structure that contains various fields that hold data in order to generate doc output.
39-
///
40-
/// ## Fields
41-
///
42-
/// - `manifest_path`: Path to the directory with the `Cargo.toml` file for the crate being analyzed
43-
/// - `host`: Contains the Cargo analysis output for the crate being documented
44-
/// - `assets`: Contains all of the `Asset`s that will be output at the end (e.g. JSON, CSS, HTML
45-
/// and/or JS)
4639
pub struct Config {
40+
/// Path to the directory with the `Cargo.toml` file for the crate being analyzed
4741
manifest_path: PathBuf,
42+
43+
/// Contains the Cargo analysis output for the crate being documented
4844
host: analysis::AnalysisHost,
45+
46+
/// Contains all of the `Asset`s that will be output at the end (e.g. JSON, CSS, HTML and/or
47+
/// JS)
4948
assets: Vec<Asset>,
5049
}
5150

@@ -55,7 +54,7 @@ impl Config {
5554
///
5655
/// ## Arguments
5756
///
58-
/// - manifest_path: The path to the location of `Cargo.toml` of the crate being documented
57+
/// - `manifest_path`: The path to the location of `Cargo.toml` of the crate being documented
5958
pub fn new(manifest_path: PathBuf, assets: Vec<Asset>) -> Result<Config> {
6059
let host = analysis::AnalysisHost::new(analysis::Target::Debug);
6160

@@ -72,8 +71,8 @@ impl Config {
7271
///
7372
/// ## Arguments
7473
///
75-
/// - config: The `Config` struct that contains the data needed to generate the documentation
76-
/// - artifacts: A slice containing what assets should be output at the end
74+
/// - `config`: The `Config` struct that contains the data needed to generate the documentation
75+
/// - `artifacts`: A slice containing what assets should be output at the end
7776
pub fn build(config: &Config, artifacts: &[&str]) -> Result<()> {
7877
generate_and_load_analysis(config)?;
7978

@@ -122,8 +121,8 @@ pub fn build(config: &Config, artifacts: &[&str]) -> Result<()> {
122121
///
123122
/// ## Arguments:
124123
///
125-
/// - config: Contains data for what needs to be output or used. In this case the path to the
126-
/// `Cargo.toml` file
124+
/// - `config`: Contains data for what needs to be output or used. In this case the path to the
125+
/// `Cargo.toml` file
127126
fn generate_and_load_analysis(config: &Config) -> Result<()> {
128127
let manifest_path = &config.manifest_path;
129128

@@ -147,11 +146,11 @@ fn generate_and_load_analysis(config: &Config) -> Result<()> {
147146
Ok(())
148147
}
149148

150-
/// This creates the JSON documentation from the given AnalysisHost.
149+
/// This creates the JSON documentation from the given `AnalysisHost`.
151150
pub fn create_json(host: &AnalysisHost, crate_name: &str) -> Result<String> {
152151
let roots = host.def_roots()?;
153152

154-
let id = roots.iter().find(|&&(_, ref name)| name == &crate_name);
153+
let id = roots.iter().find(|&&(_, ref name)| name == crate_name);
155154
let root_id = match id {
156155
Some(&(id, _)) => id,
157156
_ => return Err(ErrorKind::CrateErr(crate_name.to_string()).into()),
@@ -168,7 +167,7 @@ pub fn create_json(host: &AnalysisHost, crate_name: &str) -> Result<String> {
168167

169168
let child_defs: Vec<analysis::Def> = ids.into_par_iter()
170169
.map(|id: analysis::Id| recur(&id, host))
171-
.reduce(|| Vec::new(), |mut a: Vec<analysis::Def>,
170+
.reduce(Vec::default, |mut a: Vec<analysis::Def>,
172171
b: Vec<analysis::Def>| {
173172
a.extend(b);
174173
a
@@ -209,7 +208,7 @@ pub fn create_json(host: &AnalysisHost, crate_name: &str) -> Result<String> {
209208
.attributes(String::from("docs"), root_def.docs);
210209

211210
// Insert all of the different types of relationships into this `Document` type only
212-
for (ty, data) in relationships.into_iter() {
211+
for (ty, data) in relationships {
213212
data_document.relationships(ty, data);
214213
}
215214

tests/source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ fn generate_analysis(source_file: &Path, tempdir: &Path) -> Result<AnalysisHost>
138138
Ok(())
139139
}
140140

141-
workaround(&tempdir)?;
141+
workaround(tempdir)?;
142142

143143
let host = AnalysisHost::new(Target::Debug);
144144
host.reload(tempdir, tempdir)?;

tests/validation.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ mod validation_tests {
3333
panic!("Error: {}", err);
3434
});
3535

36-
match doc.validate() {
37-
Some(errors) => panic!("Error: {:?}", errors),
38-
None => (),
36+
if let Some(errors) = doc.validate() {
37+
panic!("Error: {:?}", errors);
3938
}
4039
}
4140
}

0 commit comments

Comments
 (0)