Skip to content

Commit 0f964d3

Browse files
committed
refactor(embedded): Allow detecting missing doc comment
1 parent f9335ba commit 0f964d3

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/cargo/util/toml/embedded.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn expand_manifest(
1616
config: &Config,
1717
) -> CargoResult<String> {
1818
let comment = match extract_comment(content) {
19-
Ok(comment) => Some(comment),
19+
Ok(comment) => comment,
2020
Err(err) => {
2121
tracing::trace!("failed to extract doc comment: {err}");
2222
None
@@ -160,7 +160,7 @@ fn sanitize_name(name: &str) -> String {
160160
}
161161

162162
/// Locates a "code block manifest" in Rust source.
163-
fn extract_comment(input: &str) -> CargoResult<String> {
163+
fn extract_comment(input: &str) -> CargoResult<Option<String>> {
164164
let mut doc_fragments = Vec::new();
165165
let file = syn::parse_file(input)?;
166166
// HACK: `syn` doesn't tell us what kind of comment was used, so infer it from how many
@@ -181,7 +181,7 @@ fn extract_comment(input: &str) -> CargoResult<String> {
181181
}
182182
}
183183
if doc_fragments.is_empty() {
184-
anyhow::bail!("no doc-comment found");
184+
return Ok(None);
185185
}
186186
unindent_doc_fragments(&mut doc_fragments);
187187

@@ -190,7 +190,7 @@ fn extract_comment(input: &str) -> CargoResult<String> {
190190
add_doc_fragment(&mut doc_comment, frag);
191191
}
192192

193-
Ok(doc_comment)
193+
Ok(Some(doc_comment))
194194
}
195195

196196
/// A `#[doc]`
@@ -561,38 +561,38 @@ mod test_comment {
561561

562562
macro_rules! ec {
563563
($s:expr) => {
564-
extract_comment($s).unwrap_or_else(|err| panic!("{}", err))
564+
extract_comment($s)
565+
.unwrap_or_else(|err| panic!("{}", err))
566+
.unwrap()
565567
};
566568
}
567569

568570
#[test]
569571
fn test_no_comment() {
570-
snapbox::assert_eq(
571-
"no doc-comment found",
572+
assert_eq!(
573+
None,
572574
extract_comment(
573575
r#"
574576
fn main () {
575577
}
576578
"#,
577579
)
578-
.unwrap_err()
579-
.to_string(),
580+
.unwrap()
580581
);
581582
}
582583

583584
#[test]
584585
fn test_no_comment_she_bang() {
585-
snapbox::assert_eq(
586-
"no doc-comment found",
586+
assert_eq!(
587+
None,
587588
extract_comment(
588589
r#"#!/usr/bin/env cargo-eval
589590
590591
fn main () {
591592
}
592593
"#,
593594
)
594-
.unwrap_err()
595-
.to_string(),
595+
.unwrap()
596596
);
597597
}
598598

0 commit comments

Comments
 (0)