diff --git a/prost-build/src/ast.rs b/prost-build/src/ast.rs index 9b9988411..8815fc568 100644 --- a/prost-build/src/ast.rs +++ b/prost-build/src/ast.rs @@ -149,7 +149,7 @@ impl Comments { let mut s = RULE_URL.replace_all(line, r"<$0>").to_string(); s = RULE_BRACKETS.replace_all(&s, r"\$1$2\$3").to_string(); - if !s.is_empty() { + if !s.chars().next().map_or(false, |c| c == ' ') { s.insert(0, ' '); } s @@ -202,6 +202,40 @@ pub struct Method { mod tests { use super::*; + #[test] + fn test_comment_append_with_indent_leaves_prespaced_lines() { + struct TestCases { + name: &'static str, + input: String, + expected: String, + } + + let tests = vec![ + TestCases { + name: "existing_space", + input: " A line with a single leading space.".to_string(), + expected: "/// A line with a single leading space.\n".to_string(), + }, + TestCases { + name: "non_existing_space", + input: "A line without a single leading space.".to_string(), + expected: "/// A line without a single leading space.\n".to_string(), + }, + ]; + for t in tests { + let input = Comments { + leading_detached: vec![], + leading: vec![], + trailing: vec![t.input], + }; + + let mut actual = "".to_string(); + input.append_with_indent(0, &mut actual); + + assert_eq!(t.expected, actual, "failed {}", t.name); + } + } + #[test] fn test_comment_append_with_indent_sanitizes_comment_doc_url() { struct TestCases {