@@ -7,29 +7,29 @@ use datafusion::prelude::{
7
7
AvroReadOptions , CsvReadOptions , DataFrame , NdJsonReadOptions , ParquetReadOptions ,
8
8
SessionContext ,
9
9
} ;
10
+ use file_format:: FileFormat as DetectFileFormat ;
10
11
use std:: path:: Path ;
11
12
12
13
pub fn file_format ( filename : & str ) -> Result < FileFormat , Error > {
13
- match file_ending ( filename) ?. as_str ( ) {
14
- "avro" => Ok ( FileFormat :: Avro ) ,
15
- "csv" => Ok ( FileFormat :: Csv ) ,
16
- "json" => Ok ( FileFormat :: Json ) ,
17
- "parquet" | "parq" => Ok ( FileFormat :: Parquet ) ,
18
- other => Err ( Error :: General ( format ! (
19
- "unsupported file extension '{}'" ,
20
- other
21
- ) ) ) ,
14
+ match DetectFileFormat :: from_file ( filename) ? {
15
+ DetectFileFormat :: ApacheAvro => Ok ( FileFormat :: Avro ) ,
16
+ DetectFileFormat :: ApacheParquet => Ok ( FileFormat :: Parquet ) ,
17
+ DetectFileFormat :: PlainText => match file_ending ( filename) ?. as_str ( ) {
18
+ "json" => Ok ( FileFormat :: Avro ) ,
19
+ "csv" => Ok ( FileFormat :: Csv ) ,
20
+ other => Err ( Error :: General ( format ! (
21
+ "unsupported file extension '{}'" ,
22
+ other
23
+ ) ) ) ,
24
+ } ,
25
+ other => Err ( Error :: General ( format ! ( "unsupported file type '{}'" , other) ) ) ,
22
26
}
23
27
}
24
28
25
29
pub fn file_ending ( filename : & str ) -> Result < String , Error > {
26
- if let Some ( ending) = std:: path:: Path :: new ( filename) . extension ( ) {
27
- Ok ( ending. to_string_lossy ( ) . to_string ( ) )
28
- } else {
29
- Err ( Error :: General (
30
- "Could not determine file extension" . to_string ( ) ,
31
- ) )
32
- }
30
+ Ok ( std:: path:: Path :: new ( filename)
31
+ . extension ( )
32
+ . map_or_else ( || "" . to_owned ( ) , |e| e. to_string_lossy ( ) . to_string ( ) ) )
33
33
}
34
34
35
35
pub fn parse_filename ( filename : & Path ) -> Result < & str , Error > {
0 commit comments