gitql_engine/
engine_output_into.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::fs::File;
use std::io::Write;

use gitql_ast::statement::IntoStatement;
use gitql_core::object::GitQLObject;
use gitql_core::values::base::Value;

pub(crate) fn execute_into_statement(
    statement: &IntoStatement,
    gitql_object: &mut GitQLObject,
) -> Result<(), String> {
    let mut buffer = String::new();

    let line_terminated_by = &statement.lines_terminated;
    let field_terminated_by = &statement.fields_terminated;
    let enclosing = &statement.enclosed;

    // Headers
    let header = gitql_object.titles.join(field_terminated_by);
    buffer.push_str(&header);
    buffer.push_str(line_terminated_by);

    // Rows of the main group
    if let Some(main_group) = gitql_object.groups.first() {
        for row in &main_group.rows {
            let row_values: Vec<String> = row
                .values
                .iter()
                .map(|r| value_to_string_with_optional_enclosing(r, enclosing))
                .collect();
            buffer.push_str(&row_values.join(field_terminated_by));
            buffer.push_str(line_terminated_by);
        }
    }

    let file_result = File::create(statement.file_path.clone());
    if let Err(error) = file_result {
        return Err(error.to_string());
    }

    let mut file = file_result.ok().unwrap();
    let write_result = file.write_all(buffer.as_bytes());
    if let Err(error) = write_result {
        return Err(error.to_string());
    }

    Ok(())
}

#[inline(always)]
#[allow(clippy::borrowed_box)]
fn value_to_string_with_optional_enclosing(value: &Box<dyn Value>, enclosed: &String) -> String {
    if enclosed.is_empty() {
        return value.literal();
    }
    format!("{}{}{}", enclosed, value.literal(), enclosed)
}