(explicit: Option<usize>, rustfmt_config: &Path)
| 185 | } |
| 186 | |
| 187 | fn comment_width(explicit: Option<usize>, rustfmt_config: &Path) -> Result<usize> { |
| 188 | if let Some(width) = explicit { |
| 189 | return validate_width(width); |
| 190 | } |
| 191 | |
| 192 | let source = match fs_err::read_to_string(rustfmt_config) { |
| 193 | Ok(source) => source, |
| 194 | Err(err) if err.kind() == ErrorKind::NotFound => return Ok(100), |
| 195 | Err(err) => { |
| 196 | return Err(err).with_context(|| format!("reading {}", rustfmt_config.display())); |
| 197 | }, |
| 198 | }; |
| 199 | |
| 200 | let config = toml::from_str::<toml::Value>(&source) |
| 201 | .context("parsing rustfmt config for comment_width")?; |
| 202 | let width = config |
| 203 | .get("comment_width") |
| 204 | .and_then(toml::Value::as_integer) |
| 205 | .and_then(|width| usize::try_from(width).ok()) |
| 206 | .unwrap_or(100); |
| 207 | |
| 208 | validate_width(width) |
| 209 | } |
| 210 | |
| 211 | fn validate_width(width: usize) -> Result<usize> { |
| 212 | ensure!(width >= 20, "comment width must be at least 20 columns"); |
no test coverage detected