@@ -2,7 +2,7 @@ extern crate quick_xml;
2
2
extern crate yaml_rust;
3
3
4
4
use std:: io:: { Read , Write , stdout} ;
5
- use std:: fs:: File ;
5
+ use std:: fs:: { File , OpenOptions } ;
6
6
use std:: path:: Path ;
7
7
use std:: env;
8
8
use std:: cell:: RefCell ;
@@ -18,12 +18,18 @@ struct Table<'a> {
18
18
columns : Vec < Column < ' a > >
19
19
}
20
20
impl < ' a > Table < ' a > {
21
- fn new ( path : & str , file : Option < & str > ) -> Table < ' a > {
21
+ fn new ( path : & str , file : Option < & str > , filemode : & str ) -> Table < ' a > {
22
22
Table {
23
23
path : String :: from ( path) ,
24
24
file : match file {
25
25
None => RefCell :: new ( Box :: new ( stdout ( ) ) ) ,
26
- Some ( ref file) => RefCell :: new ( Box :: new ( File :: create ( & Path :: new ( file) ) . unwrap ( ) ) )
26
+ Some ( ref file) => RefCell :: new ( Box :: new (
27
+ match filemode {
28
+ "truncate" => File :: create ( & Path :: new ( file) ) . unwrap ( ) ,
29
+ "append" => OpenOptions :: new ( ) . append ( true ) . create ( true ) . open ( & Path :: new ( file) ) . unwrap ( ) ,
30
+ mode => panic ! ( "Invalid 'mode' setting in configuration file: {}" , mode)
31
+ }
32
+ ) )
27
33
} ,
28
34
columns : Vec :: new ( )
29
35
}
@@ -97,8 +103,8 @@ fn gml_to_ewkb(cell: &RefCell<String>, geom: &Geometry) {
97
103
}
98
104
}
99
105
100
- fn add_table < ' a > ( rowpath : & str , outfile : Option < & str > , colspec : & ' a [ Yaml ] ) -> Table < ' a > {
101
- let mut table = Table :: new ( rowpath, outfile) ;
106
+ fn add_table < ' a > ( rowpath : & str , outfile : Option < & str > , filemode : & str , colspec : & ' a [ Yaml ] ) -> Table < ' a > {
107
+ let mut table = Table :: new ( rowpath, outfile, filemode ) ;
102
108
for col in colspec {
103
109
let name = col[ "name" ] . as_str ( ) . expect ( "Column has no 'name' entry in configuration file" ) ;
104
110
let colpath = col[ "path" ] . as_str ( ) . expect ( "Column has no 'path' entry in configuration file" ) ;
@@ -108,7 +114,7 @@ fn add_table<'a>(rowpath: &str, outfile: Option<&str>, colspec: &'a [Yaml]) -> T
108
114
true => None ,
109
115
false => {
110
116
let file = col[ "file" ] . as_str ( ) . expect ( "Subtable has no 'file' entry" ) ;
111
- Some ( add_table ( & path, Some ( & file) , col[ "cols" ] . as_vec ( ) . expect ( "Subtable 'cols' entry is not an array" ) ) )
117
+ Some ( add_table ( & path, Some ( & file) , filemode , col[ "cols" ] . as_vec ( ) . expect ( "Subtable 'cols' entry is not an array" ) ) )
112
118
}
113
119
} ;
114
120
let attr = col[ "attr" ] . as_str ( ) ;
@@ -147,7 +153,11 @@ fn main() -> std::io::Result<()> {
147
153
let rowpath = config[ "path" ] . as_str ( ) . expect ( "No valid 'path' entry in configuration file" ) ;
148
154
let colspec = config[ "cols" ] . as_vec ( ) . expect ( "No valid 'cols' array in configuration file" ) ;
149
155
let outfile = config[ "file" ] . as_str ( ) ;
150
- let maintable = add_table ( rowpath, outfile, colspec) ;
156
+ let filemode = match config[ "mode" ] . is_badvalue ( ) {
157
+ true => "truncate" ,
158
+ false => config[ "mode" ] . as_str ( ) . expect ( "Invalid 'mode' entry in configuration file" )
159
+ } ;
160
+ let maintable = add_table ( rowpath, outfile, filemode, colspec) ;
151
161
let mut tables: Vec < & Table > = Vec :: new ( ) ;
152
162
let mut table = & maintable;
153
163
0 commit comments