1+ use std:: fs:: { self , File } ;
2+ use std:: io:: Write ;
3+ use lazy_static:: lazy_static;
4+ use regex:: Regex ;
5+
6+ use crate :: http:: { Resp , Data , Ques } ;
7+
8+ lazy_static ! (
9+ static ref RE : Regex = Regex :: new( r"\|\s*([0-9]*)\s*\|\s*(\w*)\s*\|.*?bin/(.*?).rs.*?\|.*?\|" ) . unwrap( ) ;
10+ ) ;
11+
12+ /// 将结果写入README.md中
13+ pub fn write_readme ( r : & mut Vec < Resp > ) {
14+ // 先按id排序
15+ r. sort_by ( |x, y| {
16+ let x_id = x. data . question . question_id . parse :: < i32 > ( ) . unwrap ( ) ;
17+ let y_id = y. data . question . question_id . parse :: < i32 > ( ) . unwrap ( ) ;
18+ x_id. cmp ( & y_id)
19+ } ) ;
20+ let s = crate :: render:: render ( r) . unwrap ( ) ;
21+
22+ match std:: fs:: write ( "README.md" , s) {
23+ Ok ( _) => ( ) ,
24+ Err ( e) => println ! ( "写入 README.md 失败,err{}" , e. to_string( ) )
25+ }
26+ }
27+
28+ /// 获取 src/bin 目录下所有文件的名称
29+ pub fn get_all_bin_file ( ) -> Vec < String > {
30+ let dir = fs:: read_dir ( "src/bin/" ) . unwrap ( ) ;
31+ dir.
32+ into_iter ( ) .
33+ map ( |x| {
34+ x. unwrap ( ) . file_name ( ) . to_str ( ) . unwrap ( ) . trim_end_matches ( ".rs" ) . to_string ( )
35+ } ) .
36+ collect ( )
37+ }
38+
39+
40+ /// 创建 bin/{quest_name}.rs 文件
41+ pub fn write_question ( resp : Resp ) {
42+ let file = format ! ( "src/bin/{}.rs" , resp. data. question. title_slug) ;
43+ if std:: path:: Path :: new ( file. as_str ( ) ) . exists ( ) {
44+ println ! ( "{} exists" , file) ;
45+ return ;
46+ }
47+
48+ let mut f = File :: create ( file. as_str ( ) ) . unwrap ( ) ;
49+ let mut s = String :: new ( ) ;
50+ s. push_str ( "fn main() {}\n \n struct Solution;\n \n " ) ;
51+
52+ for i in resp. data . question . code_snippets {
53+ if i. lang == "Rust" {
54+ s. push_str ( i. code . replace ( "↵" , "\n " ) . as_str ( ) ) ;
55+ s. push_str ( "\n " ) ;
56+ break ;
57+ }
58+ }
59+
60+ f. write_all ( s. as_bytes ( ) ) . unwrap ( ) ;
61+ }
62+
63+ /// 解析README.md
64+ pub fn parse_readme ( ) -> Vec < Resp > {
65+ let contents = fs:: read_to_string ( "README.md" ) . unwrap ( ) ;
66+ parse ( & contents)
67+ }
68+
69+ fn parse ( contents : & str ) -> Vec < Resp > {
70+ let mut v = vec ! [ ] ;
71+ for content in contents. split ( '\n' ) {
72+ for i in RE . captures_iter ( content. trim ( ) ) {
73+ v. push ( Resp {
74+ data : Data {
75+ question : Ques {
76+ question_id : i. get ( 1 ) . unwrap ( ) . as_str ( ) . to_string ( ) ,
77+ translated_title : i. get ( 2 ) . unwrap ( ) . as_str ( ) . to_string ( ) ,
78+ title_slug : String :: new ( ) ,
79+ code_snippets : vec ! [ ] ,
80+ difficulty : String :: new ( )
81+ }
82+ } ,
83+ } )
84+ }
85+ }
86+
87+ v
88+ }
89+
90+ #[ cfg( test) ]
91+ mod tests {
92+ use crate :: file:: { parse, get_all_bin_file} ;
93+
94+ #[ test]
95+ fn test_parse_readme ( ) {
96+ let content = r"| 1111 | 两数之和| [src](https://github.com/rustors/leetcode/blob/main/src/bin/two_sum.rs) | [leetcode](https://leetcode-cn.com/problems/two_sum/) |
97+ | 1112 | 两数之和| [src](https://github.com/rustors/leetcode/blob/main/src/bin/two_sum.rs) | [leetcode](https://leetcode-cn.com/problems/two_sum/) |
98+ " ;
99+
100+ println ! ( "{:?}" , parse( content) ) ;
101+ }
102+
103+ #[ test]
104+ fn test_get_all_bin_file ( ) {
105+ println ! ( "{:?}" , get_all_bin_file( ) ) ;
106+ }
107+ }
0 commit comments