11//! Checks that all Flunt files have messages in alphabetical order
22
33use crate :: walk:: { filter_dirs, walk} ;
4+ use std:: collections:: HashMap ;
45use std:: { fs:: OpenOptions , io:: Write , path:: Path } ;
56
67use regex:: Regex ;
78
89lazy_static:: lazy_static! {
9- static ref MESSAGE : Regex = Regex :: new( r#"(?m)^([a-zA-Z0-9_]+)\s*=\s*"# ) . unwrap( ) ;
10+ pub static ref MESSAGE : Regex = Regex :: new( r#"(?m)^([a-zA-Z0-9_]+)\s*=\s*"# ) . unwrap( ) ;
1011}
1112
1213fn filter_fluent ( path : & Path ) -> bool {
1314 if let Some ( ext) = path. extension ( ) { ext. to_str ( ) != Some ( "ftl" ) } else { true }
1415}
1516
16- fn check_alphabetic ( filename : & str , fluent : & str , bad : & mut bool ) {
17+ fn check_alphabetic (
18+ filename : & str ,
19+ fluent : & str ,
20+ bad : & mut bool ,
21+ msgs : & mut HashMap < String , String > ,
22+ ) {
1723 let mut matches = MESSAGE . captures_iter ( fluent) . peekable ( ) ;
1824 while let Some ( m) = matches. next ( ) {
25+ let name = m. get ( 1 ) . unwrap ( ) ;
26+ msgs. insert ( name. as_str ( ) . to_owned ( ) , filename. to_owned ( ) ) ;
27+
1928 if let Some ( next) = matches. peek ( ) {
20- let name = m. get ( 1 ) . unwrap ( ) ;
2129 let next = next. get ( 1 ) . unwrap ( ) ;
2230 if name. as_str ( ) > next. as_str ( ) {
2331 tidy_error ! (
@@ -34,13 +42,15 @@ run `./x.py test tidy --bless` to sort the file correctly",
3442 }
3543}
3644
37- fn sort_messages ( fluent : & str ) -> String {
45+ fn sort_messages ( filename : & str , fluent : & str , msgs : & mut HashMap < String , String > ) -> String {
3846 let mut chunks = vec ! [ ] ;
3947 let mut cur = String :: new ( ) ;
4048 for line in fluent. lines ( ) {
41- if MESSAGE . is_match ( line) {
49+ if let Some ( name) = MESSAGE . find ( line) {
50+ msgs. insert ( name. as_str ( ) . to_owned ( ) , filename. to_owned ( ) ) ;
4251 chunks. push ( std:: mem:: take ( & mut cur) ) ;
4352 }
53+
4454 cur += line;
4555 cur. push ( '\n' ) ;
4656 }
@@ -53,20 +63,23 @@ fn sort_messages(fluent: &str) -> String {
5363}
5464
5565pub fn check ( path : & Path , bless : bool , bad : & mut bool ) {
66+ let mut msgs = HashMap :: new ( ) ;
5667 walk (
5768 path,
5869 |path, is_dir| filter_dirs ( path) || ( !is_dir && filter_fluent ( path) ) ,
5970 & mut |ent, contents| {
6071 if bless {
61- let sorted = sort_messages ( contents) ;
72+ let sorted = sort_messages ( ent . path ( ) . to_str ( ) . unwrap ( ) , contents, & mut msgs ) ;
6273 if sorted != contents {
6374 let mut f =
6475 OpenOptions :: new ( ) . write ( true ) . truncate ( true ) . open ( ent. path ( ) ) . unwrap ( ) ;
6576 f. write ( sorted. as_bytes ( ) ) . unwrap ( ) ;
6677 }
6778 } else {
68- check_alphabetic ( ent. path ( ) . to_str ( ) . unwrap ( ) , contents, bad) ;
79+ check_alphabetic ( ent. path ( ) . to_str ( ) . unwrap ( ) , contents, bad, & mut msgs ) ;
6980 }
7081 } ,
7182 ) ;
83+
84+ crate :: fluent_used:: check ( path, & mut msgs, bad) ;
7285}
0 commit comments