gitql_engine/
engine_output_into.rs1use std::fs::File;
2use std::io::Write;
3
4use gitql_ast::statement::IntoStatement;
5use gitql_core::object::GitQLObject;
6use gitql_core::values::Value;
7
8pub(crate) fn execute_into_statement(
9 statement: &IntoStatement,
10 gitql_object: &mut GitQLObject,
11) -> Result<(), String> {
12 let mut buffer = String::new();
13
14 let line_terminated_by = &statement.lines_terminated;
15 let field_terminated_by = &statement.fields_terminated;
16 let enclosing = &statement.enclosed;
17
18 let header = gitql_object.titles.join(field_terminated_by);
20 buffer.push_str(&header);
21 buffer.push_str(line_terminated_by);
22
23 if let Some(main_group) = gitql_object.groups.first() {
25 for row in &main_group.rows {
26 let row_values: Vec<String> = row
27 .values
28 .iter()
29 .map(|r| value_to_string_with_optional_enclosing(r, enclosing))
30 .collect();
31 buffer.push_str(&row_values.join(field_terminated_by));
32 buffer.push_str(line_terminated_by);
33 }
34 }
35
36 let file_result = File::create(statement.file_path.clone());
37 if let Err(error) = file_result {
38 return Err(error.to_string());
39 }
40
41 let mut file = file_result.ok().unwrap();
42 let write_result = file.write_all(buffer.as_bytes());
43 if let Err(error) = write_result {
44 return Err(error.to_string());
45 }
46
47 Ok(())
48}
49
50#[inline(always)]
51#[allow(clippy::borrowed_box)]
52fn value_to_string_with_optional_enclosing(value: &Box<dyn Value>, enclosed: &String) -> String {
53 if enclosed.is_empty() {
54 return value.literal();
55 }
56 format!("{}{}{}", enclosed, value.literal(), enclosed)
57}