1- use std:: ffi:: OsStr ;
1+ use std:: ffi:: { OsStr , OsString } ;
22use std:: path:: Path ;
33
44use crate :: file_reader:: FileReader ;
@@ -63,6 +63,35 @@ impl ApplicationContext {
6363 . show_alert ( )
6464 . unwrap ( ) ;
6565 }
66+
67+ /// Open the file dialog to select a file
68+ ///
69+ /// # Example
70+ ///
71+ /// ```rust
72+ /// let selected_file_result: Result<Option<String>, OsString> = open_file_dialog()
73+ /// ```
74+ ///
75+ /// # Returns
76+ ///
77+ /// The optional `String` that contains the path of the selected file or an `OsString` error
78+ fn open_file_dialog ( ) -> Result < Option < String > , OsString > {
79+ let path = FileDialog :: new ( )
80+ . add_filter ( "Text file" , & [ "txt" ] )
81+ . add_filter ( "All files" , & [ "*" ] )
82+ . show_open_single_file ( )
83+ . unwrap ( ) ;
84+
85+ let path = match path {
86+ Some ( path) => path,
87+ None => return Ok ( None ) ,
88+ } ;
89+
90+ match path. into_os_string ( ) . into_string ( ) {
91+ Ok ( d) => Ok ( Some ( d) ) ,
92+ Err ( e) => Err ( e) ,
93+ }
94+ }
6695}
6796
6897impl Sandbox for ApplicationContext {
@@ -81,32 +110,42 @@ impl Sandbox for ApplicationContext {
81110 Message :: FirstFileInputChanged ( d) => self . first_file = d,
82111 Message :: SecondFileInputChanged ( d) => self . second_file = d,
83112 Message :: SelectFirstFilePressed => {
84- let path = FileDialog :: new ( )
85- . add_filter ( "Text file" , & [ "txt" ] )
86- . add_filter ( "All files" , & [ "*" ] )
87- . show_open_single_file ( )
88- . unwrap ( ) ;
89-
90- let path = match path {
91- Some ( path) => path,
92- None => return ,
113+ let path = match ApplicationContext :: open_file_dialog ( ) {
114+ Ok ( res) => match res {
115+ Some ( d) => d,
116+ None => return ,
117+ } ,
118+ Err ( e) => {
119+ ApplicationContext :: display_alert (
120+ & self ,
121+ "text-diff" ,
122+ & format ! ( "Error while selecting file!\n {:?}" , e) ,
123+ MessageType :: Error ,
124+ ) ;
125+ return ;
126+ }
93127 } ;
94128
95- self . first_file = path. into_os_string ( ) . into_string ( ) . unwrap ( ) ;
129+ self . first_file = path;
96130 }
97131 Message :: SelectSecondFilePressed => {
98- let path = FileDialog :: new ( )
99- . add_filter ( "Text file" , & [ "txt" ] )
100- . add_filter ( "All files" , & [ "*" ] )
101- . show_open_single_file ( )
102- . unwrap ( ) ;
103-
104- let path = match path {
105- Some ( path) => path,
106- None => return ,
132+ let path = match ApplicationContext :: open_file_dialog ( ) {
133+ Ok ( res) => match res {
134+ Some ( d) => d,
135+ None => return ,
136+ } ,
137+ Err ( e) => {
138+ ApplicationContext :: display_alert (
139+ & self ,
140+ "text-diff" ,
141+ & format ! ( "Error while selecting file!\n {:?}" , e) ,
142+ MessageType :: Error ,
143+ ) ;
144+ return ;
145+ }
107146 } ;
108147
109- self . second_file = path. into_os_string ( ) . into_string ( ) . unwrap ( ) ;
148+ self . second_file = path;
110149 }
111150 Message :: ComparePressed => {
112151 if self . first_file . is_empty ( ) || self . second_file . is_empty ( ) {
0 commit comments