nu_command/env/config/
config_flatten.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use nu_engine::command_prelude::*;
use nu_utils::JsonFlattener; // Ensure this import is present // Ensure this import is present

#[derive(Clone)]
pub struct ConfigFlatten;

impl Command for ConfigFlatten {
    fn name(&self) -> &str {
        "config flatten"
    }

    fn signature(&self) -> Signature {
        Signature::build(self.name())
            .category(Category::Debug)
            .input_output_types(vec![(Type::Nothing, Type::record())])
    }

    fn description(&self) -> &str {
        "Show the current configuration in a flattened form."
    }

    fn examples(&self) -> Vec<Example> {
        vec![Example {
            description: "Show the current configuration in a flattened form",
            example: "config flatten",
            result: None,
        }]
    }

    fn run(
        &self,
        engine_state: &EngineState,
        _stack: &mut Stack,
        call: &Call,
        _input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        // Get the Config instance from the EngineState
        let config = engine_state.get_config();
        // Serialize the Config instance to JSON
        let serialized_config =
            serde_json::to_value(&**config).map_err(|err| ShellError::GenericError {
                error: format!("Failed to serialize config to json: {err}"),
                msg: "".into(),
                span: Some(call.head),
                help: None,
                inner: vec![],
            })?;
        // Create a JsonFlattener instance with appropriate arguments
        let flattener = JsonFlattener {
            separator: ".",
            alt_array_flattening: false,
            preserve_arrays: true,
        };
        // Flatten the JSON value
        let flattened_config_str = flattener.flatten(&serialized_config).to_string();
        let flattened_values =
            convert_string_to_value(&flattened_config_str, engine_state, call.head)?;

        Ok(flattened_values.into_pipeline_data())
    }
}

// From here below is taken from `from json`. Would be nice to have a nu-utils-value crate that could be shared
fn convert_string_to_value(
    string_input: &str,
    engine_state: &EngineState,
    span: Span,
) -> Result<Value, ShellError> {
    match nu_json::from_str(string_input) {
        Ok(value) => Ok(convert_nujson_to_value(None, value, engine_state, span)),

        Err(x) => match x {
            nu_json::Error::Syntax(_, row, col) => {
                let label = x.to_string();
                let label_span = convert_row_column_to_span(row, col, string_input);
                Err(ShellError::GenericError {
                    error: "Error while parsing JSON text".into(),
                    msg: "error parsing JSON text".into(),
                    span: Some(span),
                    help: None,
                    inner: vec![ShellError::OutsideSpannedLabeledError {
                        src: string_input.into(),
                        error: "Error while parsing JSON text".into(),
                        msg: label,
                        span: label_span,
                    }],
                })
            }
            x => Err(ShellError::CantConvert {
                to_type: format!("structured json data ({x})"),
                from_type: "string".into(),
                span,
                help: None,
            }),
        },
    }
}

fn convert_nujson_to_value(
    key: Option<String>,
    value: nu_json::Value,
    engine_state: &EngineState,
    span: Span,
) -> Value {
    match value {
        nu_json::Value::Array(array) => Value::list(
            array
                .into_iter()
                .map(|x| convert_nujson_to_value(key.clone(), x, engine_state, span))
                .collect(),
            span,
        ),
        nu_json::Value::Bool(b) => Value::bool(b, span),
        nu_json::Value::F64(f) => Value::float(f, span),
        nu_json::Value::I64(i) => {
            if let Some(closure_str) = expand_closure(key.clone(), i, engine_state) {
                Value::string(closure_str, span)
            } else {
                Value::int(i, span)
            }
        }
        nu_json::Value::Null => Value::nothing(span),
        nu_json::Value::Object(k) => Value::record(
            k.into_iter()
                .map(|(k, v)| {
                    let mut key = k.clone();
                    // Keep .Closure.val and .block_id as part of the key during conversion to value
                    let value = convert_nujson_to_value(Some(key.clone()), v, engine_state, span);
                    // Replace .Closure.val and .block_id from the key after the conversion
                    if key.contains(".Closure.val") || key.contains(".block_id") {
                        key = key.replace(".Closure.val", "").replace(".block_id", "");
                    }
                    (key, value)
                })
                .collect(),
            span,
        ),
        nu_json::Value::U64(u) => {
            if u > i64::MAX as u64 {
                Value::error(
                    ShellError::CantConvert {
                        to_type: "i64 sized integer".into(),
                        from_type: "value larger than i64".into(),
                        span,
                        help: None,
                    },
                    span,
                )
            } else if let Some(closure_str) = expand_closure(key.clone(), u as i64, engine_state) {
                Value::string(closure_str, span)
            } else {
                Value::int(u as i64, span)
            }
        }
        nu_json::Value::String(s) => Value::string(s, span),
    }
}

// If the block_id is a real block id, then it should expand into the closure contents, otherwise return None
fn expand_closure(
    key: Option<String>,
    block_id: i64,
    engine_state: &EngineState,
) -> Option<String> {
    match key {
        Some(key) if key.contains(".Closure.val") || key.contains(".block_id") => engine_state
            .try_get_block(nu_protocol::BlockId::new(block_id as usize))
            .and_then(|block| block.span)
            .map(|span| {
                let contents = engine_state.get_span_contents(span);
                String::from_utf8_lossy(contents).to_string()
            }),
        _ => None,
    }
}

// Converts row+column to a Span, assuming bytes (1-based rows)
fn convert_row_column_to_span(row: usize, col: usize, contents: &str) -> Span {
    let mut cur_row = 1;
    let mut cur_col = 1;

    for (offset, curr_byte) in contents.bytes().enumerate() {
        if curr_byte == b'\n' {
            cur_row += 1;
            cur_col = 1;
        }
        if cur_row >= row && cur_col >= col {
            return Span::new(offset, offset);
        } else {
            cur_col += 1;
        }
    }

    Span::new(contents.len(), contents.len())
}