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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{ColumnPath, Primitive, Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::Tagged;
use num_bigint::{BigInt, BigUint, ToBigInt};
// TODO num_format::SystemLocale once platform-specific dependencies are stable (see Cargo.toml)
use nu_data::base::shape::InlineShape;
use num_format::Locale;
use num_traits::{Pow, Signed};
use std::iter;

pub struct SubCommand;

impl WholeStreamCommand for SubCommand {
    fn name(&self) -> &str {
        "into string"
    }

    fn signature(&self) -> Signature {
        Signature::build("into string")
            .rest(
                "rest",
                SyntaxShape::ColumnPath,
                "column paths to convert to string (for table input)",
            )
            .named(
                "decimals",
                SyntaxShape::Int,
                "decimal digits to which to round",
                Some('d'),
            )
    }

    fn usage(&self) -> &str {
        "Convert value to string"
    }

    fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
        into_string(args)
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                description: "convert decimal to string and round to nearest integer",
                example: "echo 1.7 | into string -d 0",
                result: Some(vec![UntaggedValue::string("2").into_untagged_value()]),
            },
            Example {
                description: "convert decimal to string",
                example: "echo 4.3 | into string",
                result: Some(vec![UntaggedValue::string("4.3").into_untagged_value()]),
            },
            Example {
                description: "convert string to string",
                example: "echo '1234' | into string",
                result: Some(vec![UntaggedValue::string("1234").into_untagged_value()]),
            },
            Example {
                description: "convert boolean to string",
                example: "echo $true | into string",
                result: Some(vec![UntaggedValue::string("true").into_untagged_value()]),
            },
            Example {
                description: "convert date to string",
                example: "date now | into string",
                result: None,
            },
            Example {
                description: "convert filepath to string",
                example: "ls Cargo.toml | get name | into string",
                result: None,
            },
            Example {
                description: "convert filesize to string",
                example: "ls Cargo.toml | get size | into string",
                result: None,
            },
        ]
    }
}

fn into_string(args: CommandArgs) -> Result<OutputStream, ShellError> {
    let decimals: Option<Tagged<u64>> = args.get_flag("decimals")?;
    let column_paths: Vec<ColumnPath> = args.rest(0)?;

    let digits = decimals.as_ref().map(|tagged| tagged.item);
    let group_digits = false;

    Ok(args
        .input
        .map(move |v| {
            if column_paths.is_empty() {
                action(&v, v.tag(), digits, group_digits)
            } else {
                let mut ret = v;
                for path in &column_paths {
                    ret = ret.swap_data_by_column_path(
                        path,
                        Box::new(move |old| action(old, old.tag(), digits, group_digits)),
                    )?;
                }

                Ok(ret)
            }
        })
        .into_input_stream())
}

pub fn action(
    input: &Value,
    tag: impl Into<Tag>,
    digits: Option<u64>,
    group_digits: bool,
) -> Result<Value, ShellError> {
    match &input.value {
        UntaggedValue::Primitive(prim) => Ok(UntaggedValue::string(match prim {
            Primitive::Int(int) => {
                if group_digits {
                    format_int(*int) // int.to_formatted_string(*locale)
                } else {
                    int.to_string()
                }
            }
            Primitive::BigInt(int) => {
                if group_digits {
                    format_bigint(int) // int.to_formatted_string(*locale)
                } else {
                    int.to_string()
                }
            }
            Primitive::Decimal(dec) => format_decimal(dec.clone(), digits, group_digits),
            Primitive::String(a_string) => a_string.to_string(),
            Primitive::Boolean(a_bool) => a_bool.to_string(),
            Primitive::Date(a_date) => a_date.format("%c").to_string(),
            Primitive::FilePath(a_filepath) => a_filepath.as_path().display().to_string(),
            Primitive::Filesize(a_filesize) => {
                let byte_string = InlineShape::format_bytes(*a_filesize, None);
                byte_string.1
            }
            Primitive::Nothing => "nothing".to_string(),
            _ => {
                return Err(ShellError::unimplemented(&format!(
                    "into string for primitive: {:?}",
                    prim
                )))
            }
        })
        .into_value(tag)),
        UntaggedValue::Row(_) => Err(ShellError::labeled_error(
            "specify column to use 'into string'",
            "found table",
            input.tag.clone(),
        )),
        UntaggedValue::Table(_) => Err(ShellError::unimplemented("into string for table")),
        _ => Err(ShellError::unimplemented("into string for non-primitive")),
    }
}

fn format_int(int: i64) -> String {
    int.to_string()

    // TODO once platform-specific dependencies are stable (see Cargo.toml)
    // #[cfg(windows)]
    // {
    //     int.to_formatted_string(&Locale::en)
    // }
    // #[cfg(not(windows))]
    // {
    //     match SystemLocale::default() {
    //         Ok(locale) => int.to_formatted_string(&locale),
    //         Err(_) => int.to_formatted_string(&Locale::en),
    //     }
    // }
}

fn format_bigint(int: &BigInt) -> String {
    int.to_string()

    // TODO once platform-specific dependencies are stable (see Cargo.toml)
    // #[cfg(windows)]
    // {
    //     int.to_formatted_string(&Locale::en)
    // }
    // #[cfg(not(windows))]
    // {
    //     match SystemLocale::default() {
    //         Ok(locale) => int.to_formatted_string(&locale),
    //         Err(_) => int.to_formatted_string(&Locale::en),
    //     }
    // }
}

fn format_decimal(mut decimal: BigDecimal, digits: Option<u64>, group_digits: bool) -> String {
    if let Some(n) = digits {
        decimal = round_decimal(&decimal, n)
    }

    if decimal.is_integer() && (digits.is_none() || digits == Some(0)) {
        let int = decimal
            .to_bigint()
            .expect("integer BigDecimal should convert to BigInt");
        return if group_digits {
            int.to_string()
        } else {
            format_bigint(&int)
        };
    }

    let (int, exp) = decimal.as_bigint_and_exponent();
    let factor = BigInt::from(10).pow(BigUint::from(exp as u64)); // exp > 0 for non-int decimal
    let int_part = &int / &factor;
    let dec_part = (&int % &factor)
        .abs()
        .to_biguint()
        .expect("BigInt::abs should always produce positive signed BigInt and thus BigUInt")
        .to_str_radix(10);

    let dec_str = if let Some(n) = digits {
        dec_part
            .chars()
            .chain(iter::repeat('0'))
            .take(n as usize)
            .collect()
    } else {
        String::from(dec_part.trim_end_matches('0'))
    };

    let format_default_loc = |int_part: BigInt| {
        let loc = Locale::en;
        //TODO: when num_format is available for recent bigint, replace this with the locale-based format
        let (int_str, sep) = (int_part.to_string(), String::from(loc.decimal()));

        format!("{}{}{}", int_str, sep, dec_str)
    };

    format_default_loc(int_part)

    // TODO once platform-specific dependencies are stable (see Cargo.toml)
    // #[cfg(windows)]
    // {
    //     format_default_loc(int_part)
    // }
    // #[cfg(not(windows))]
    // {
    //     match SystemLocale::default() {
    //         Ok(sys_loc) => {
    //             let int_str = int_part.to_formatted_string(&sys_loc);
    //             let sep = String::from(sys_loc.decimal());
    //             format!("{}{}{}", int_str, sep, dec_str)
    //         }
    //         Err(_) => format_default_loc(int_part),
    //     }
    // }
}

fn round_decimal(decimal: &BigDecimal, mut digits: u64) -> BigDecimal {
    let mut mag = decimal.clone();
    while mag >= BigDecimal::from(1) {
        mag = mag / 10;
        digits += 1;
    }

    decimal.with_prec(digits)
}

#[cfg(test)]
mod tests {
    use super::ShellError;
    use super::SubCommand;

    #[test]
    fn examples_work_as_expected() -> Result<(), ShellError> {
        use crate::examples::test as test_examples;

        test_examples(SubCommand {})
    }
}