11use crate :: utils:: error:: Result ;
2- use std:: path:: Path ;
3- use std:: io:: { BufRead , BufReader } ;
42use std:: fs:: File ;
3+ use std:: io:: { BufRead , BufReader } ;
4+ use std:: path:: Path ;
55
6- use super :: types:: { InlineCodeownersEntry , Owner , Tag } ;
76use super :: parser:: parse_owner;
7+ use super :: types:: { InlineCodeownersEntry , Owner , Tag } ;
88
99/// Detects inline CODEOWNERS declaration in the first 50 lines of a file
1010pub fn detect_inline_codeowners ( file_path : & Path ) -> Result < Option < InlineCodeownersEntry > > {
@@ -32,18 +32,16 @@ pub fn detect_inline_codeowners(file_path: &Path) -> Result<Option<InlineCodeown
3232
3333/// Parse a single line for inline CODEOWNERS declaration
3434fn parse_inline_codeowners_line (
35- line : & str ,
36- line_number : usize ,
37- file_path : & Path ,
35+ line : & str , line_number : usize , file_path : & Path ,
3836) -> Result < Option < InlineCodeownersEntry > > {
3937 // Look for !!!CODEOWNERS marker
4038 if let Some ( marker_pos) = line. find ( "!!!CODEOWNERS" ) {
4139 // Extract everything after the marker
4240 let after_marker = & line[ marker_pos + "!!!CODEOWNERS" . len ( ) ..] ;
43-
41+
4442 // Split by whitespace to get tokens
4543 let tokens: Vec < & str > = after_marker. split_whitespace ( ) . collect ( ) ;
46-
44+
4745 if tokens. is_empty ( ) {
4846 return Ok ( None ) ;
4947 }
@@ -68,16 +66,20 @@ fn parse_inline_codeowners_line(
6866 } else {
6967 // Extract tag name, but check if this might be a comment
7068 let tag_part = & token[ 1 ..] ;
71-
69+
7270 // If the tag part is empty, it's probably a comment marker
7371 if tag_part. is_empty ( ) {
7472 break ;
7573 }
76-
74+
7775 // Special handling for common comment patterns
7876 // If the next token looks like end of comment (like "-->"), still treat as tag
79- let next_token = if i + 1 < tokens. len ( ) { Some ( tokens[ i + 1 ] ) } else { None } ;
80-
77+ let next_token = if i + 1 < tokens. len ( ) {
78+ Some ( tokens[ i + 1 ] )
79+ } else {
80+ None
81+ } ;
82+
8183 match next_token {
8284 Some ( "-->" ) | Some ( "*/" ) => {
8385 // This is likely the end of a comment block, so the tag is valid
@@ -97,7 +99,10 @@ fn parse_inline_codeowners_line(
9799 // Next token doesn't start with # and isn't a comment ender
98100 // This could be a comment, but we'll be conservative and treat as tag
99101 // if it looks like a valid tag name (alphanumeric + common chars)
100- if tag_part. chars ( ) . all ( |c| c. is_alphanumeric ( ) || c == '-' || c == '_' ) {
102+ if tag_part
103+ . chars ( )
104+ . all ( |c| c. is_alphanumeric ( ) || c == '-' || c == '_' )
105+ {
101106 tags. push ( Tag ( tag_part. to_string ( ) ) ) ;
102107 #[ allow( unused_assignments) ]
103108 {
@@ -145,7 +150,7 @@ mod tests {
145150 fn test_detect_inline_codeowners_rust_comment ( ) -> Result < ( ) > {
146151 let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
147152 let file_path = temp_dir. path ( ) . join ( "test.rs" ) ;
148-
153+
149154 let content = r#"// This is a Rust file
150155// !!!CODEOWNERS @user1 @org/team2 #tag1 #tag2
151156fn main() {
@@ -156,7 +161,7 @@ fn main() {
156161
157162 let result = detect_inline_codeowners ( & file_path) ?;
158163 assert ! ( result. is_some( ) ) ;
159-
164+
160165 let entry = result. unwrap ( ) ;
161166 assert_eq ! ( entry. file_path, file_path) ;
162167 assert_eq ! ( entry. line_number, 2 ) ;
@@ -174,7 +179,7 @@ fn main() {
174179 fn test_detect_inline_codeowners_javascript_comment ( ) -> Result < ( ) > {
175180 let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
176181 let file_path = temp_dir. path ( ) . join ( "test.js" ) ;
177-
182+
178183 let content = r#"/*
179184 * !!!CODEOWNERS @frontend-team #javascript
180185 */
@@ -186,7 +191,7 @@ function hello() {
186191
187192 let result = detect_inline_codeowners ( & file_path) ?;
188193 assert ! ( result. is_some( ) ) ;
189-
194+
190195 let entry = result. unwrap ( ) ;
191196 assert_eq ! ( entry. owners. len( ) , 1 ) ;
192197 assert_eq ! ( entry. owners[ 0 ] . identifier, "@frontend-team" ) ;
@@ -200,7 +205,7 @@ function hello() {
200205 fn test_detect_inline_codeowners_python_comment ( ) -> Result < ( ) > {
201206 let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
202207 let file_path = temp_dir. path ( ) . join ( "test.py" ) ;
203-
208+
204209 let content = r#"#!/usr/bin/env python3
205210# !!!CODEOWNERS @python-team @user1 #backend #critical
206211"""
@@ -214,7 +219,7 @@ def main():
214219
215220 let result = detect_inline_codeowners ( & file_path) ?;
216221 assert ! ( result. is_some( ) ) ;
217-
222+
218223 let entry = result. unwrap ( ) ;
219224 assert_eq ! ( entry. line_number, 2 ) ;
220225 assert_eq ! ( entry. owners. len( ) , 2 ) ;
@@ -231,7 +236,7 @@ def main():
231236 fn test_detect_inline_codeowners_html_comment ( ) -> Result < ( ) > {
232237 let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
233238 let file_path = temp_dir. path ( ) . join ( "test.html" ) ;
234-
239+
235240 let content = r#"<!DOCTYPE html>
236241<html>
237242<!-- !!!CODEOWNERS @web-team #frontend -->
@@ -244,7 +249,7 @@ def main():
244249
245250 let result = detect_inline_codeowners ( & file_path) ?;
246251 assert ! ( result. is_some( ) ) ;
247-
252+
248253 let entry = result. unwrap ( ) ;
249254 assert_eq ! ( entry. owners. len( ) , 1 ) ;
250255 assert_eq ! ( entry. owners[ 0 ] . identifier, "@web-team" ) ;
@@ -258,7 +263,7 @@ def main():
258263 fn test_detect_inline_codeowners_no_marker ( ) -> Result < ( ) > {
259264 let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
260265 let file_path = temp_dir. path ( ) . join ( "test.rs" ) ;
261-
266+
262267 let content = r#"// This is a regular file
263268fn main() {
264269 println!("No CODEOWNERS marker here");
@@ -276,7 +281,7 @@ fn main() {
276281 fn test_detect_inline_codeowners_no_owners ( ) -> Result < ( ) > {
277282 let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
278283 let file_path = temp_dir. path ( ) . join ( "test.rs" ) ;
279-
284+
280285 let content = r#"// !!!CODEOWNERS #just-tags
281286fn main() {
282287 println!("Only tags, no owners");
@@ -294,7 +299,7 @@ fn main() {
294299 fn test_detect_inline_codeowners_first_occurrence_only ( ) -> Result < ( ) > {
295300 let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
296301 let file_path = temp_dir. path ( ) . join ( "test.rs" ) ;
297-
302+
298303 let content = r#"// !!!CODEOWNERS @first-owner #first-tag
299304fn main() {
300305 // !!!CODEOWNERS @second-owner #second-tag
@@ -305,7 +310,7 @@ fn main() {
305310
306311 let result = detect_inline_codeowners ( & file_path) ?;
307312 assert ! ( result. is_some( ) ) ;
308-
313+
309314 let entry = result. unwrap ( ) ;
310315 assert_eq ! ( entry. line_number, 1 ) ;
311316 assert_eq ! ( entry. owners[ 0 ] . identifier, "@first-owner" ) ;
@@ -318,15 +323,15 @@ fn main() {
318323 fn test_detect_inline_codeowners_beyond_50_lines ( ) -> Result < ( ) > {
319324 let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
320325 let file_path = temp_dir. path ( ) . join ( "test.rs" ) ;
321-
326+
322327 let mut content = String :: new ( ) ;
323328 // Add 51 lines, with the marker on line 51
324329 for i in 1 ..=50 {
325330 content. push_str ( & format ! ( "// Line {}\n " , i) ) ;
326331 }
327332 content. push_str ( "// !!!CODEOWNERS @should-not-be-found #beyond-limit\n " ) ;
328333 content. push_str ( "fn main() {}\n " ) ;
329-
334+
330335 fs:: write ( & file_path, content) . unwrap ( ) ;
331336
332337 let result = detect_inline_codeowners ( & file_path) ?;
@@ -339,15 +344,15 @@ fn main() {
339344 fn test_detect_inline_codeowners_with_comment_after ( ) -> Result < ( ) > {
340345 let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
341346 let file_path = temp_dir. path ( ) . join ( "test.rs" ) ;
342-
347+
343348 let content = r#"// !!!CODEOWNERS @user1 #tag1 # this is a comment after
344349fn main() {}
345350"# ;
346351 fs:: write ( & file_path, content) . unwrap ( ) ;
347352
348353 let result = detect_inline_codeowners ( & file_path) ?;
349354 assert ! ( result. is_some( ) ) ;
350-
355+
351356 let entry = result. unwrap ( ) ;
352357 assert_eq ! ( entry. owners. len( ) , 1 ) ;
353358 assert_eq ! ( entry. owners[ 0 ] . identifier, "@user1" ) ;
@@ -367,5 +372,4 @@ fn main() {}
367372
368373 Ok ( ( ) )
369374 }
370-
371- }
375+ }
0 commit comments