1
+ use once_cell:: sync:: Lazy ;
2
+ use std:: collections:: HashMap ;
3
+
1
4
/// Extract all HTTPS URLs from a string
2
5
pub fn extract_https_urls ( text : & str ) -> Vec < String > {
3
6
let mut urls = Vec :: new ( ) ;
4
7
let mut start_idx = 0 ;
5
-
8
+
6
9
while let Some ( https_idx) = text[ start_idx..] . find ( "https://" ) {
7
10
let abs_start = start_idx + https_idx;
8
11
let url_text = & text[ abs_start..] ;
9
-
12
+
10
13
// Find the end of the URL (first whitespace or common URL-ending chars)
11
- let mut end_idx = url_text. find ( |c : char | {
12
- c. is_whitespace ( ) || c == '"' || c == '<' || c == '>' || c == ')'
13
- || c == ']' || c == '}' || c == '|'
14
- } ) . unwrap_or ( url_text. len ( ) ) ;
15
-
14
+ let mut end_idx = url_text
15
+ . find ( |c : char | {
16
+ c. is_whitespace ( )
17
+ || c == '"'
18
+ || c == '<'
19
+ || c == '>'
20
+ || c == ')'
21
+ || c == ']'
22
+ || c == '}'
23
+ || c == '|'
24
+ } )
25
+ . unwrap_or ( url_text. len ( ) ) ;
26
+
16
27
// Trim trailing punctuation
17
28
while end_idx > 0 {
18
29
let last_char = url_text[ ..end_idx] . chars ( ) . last ( ) . unwrap ( ) ;
@@ -22,13 +33,51 @@ pub fn extract_https_urls(text: &str) -> Vec<String> {
22
33
break ;
23
34
}
24
35
}
25
-
36
+
26
37
if end_idx > "https://" . len ( ) {
27
38
urls. push ( url_text[ ..end_idx] . to_string ( ) ) ;
28
39
}
29
-
40
+
30
41
start_idx = abs_start + 1 ;
31
42
}
32
-
43
+
33
44
urls
34
- }
45
+ }
46
+
47
+ /// Creates a description of a file type based on its extension.
48
+ pub fn get_file_type_description ( extension : & str ) -> String {
49
+ // Define file types with descriptions
50
+ static FILE_TYPES : Lazy < HashMap < & ' static str , & ' static str > > = Lazy :: new ( || {
51
+ let mut map = HashMap :: new ( ) ;
52
+
53
+ // Images
54
+ map. insert ( "png" , "Picture" ) ;
55
+ map. insert ( "jpg" , "Picture" ) ;
56
+ map. insert ( "jpeg" , "Picture" ) ;
57
+ map. insert ( "gif" , "GIF Animation" ) ;
58
+ map. insert ( "webp" , "Picture" ) ;
59
+
60
+ // Audio
61
+ map. insert ( "wav" , "Voice Message" ) ;
62
+ map. insert ( "mp3" , "Audio Clip" ) ;
63
+
64
+ // Videos
65
+ map. insert ( "mp4" , "Video" ) ;
66
+ map. insert ( "webm" , "Video" ) ;
67
+ map. insert ( "mov" , "Video" ) ;
68
+ map. insert ( "avi" , "Video" ) ;
69
+ map. insert ( "mkv" , "Video" ) ;
70
+
71
+ map
72
+ } ) ;
73
+
74
+ // Normalize the extension to lowercase
75
+ let normalized_ext = extension. to_lowercase ( ) ;
76
+
77
+ // Return the file type description if found, otherwise return default value
78
+ FILE_TYPES
79
+ . get ( normalized_ext. as_str ( ) )
80
+ . copied ( )
81
+ . unwrap_or ( "File" )
82
+ . to_string ( )
83
+ }
0 commit comments