nu_command/strings/
mod.rs

1mod ansi;
2mod base;
3mod char_;
4mod detect_columns;
5mod encode_decode;
6mod format;
7mod guess_width;
8mod parse;
9mod split;
10mod str_;
11
12pub use ansi::{Ansi, AnsiLink, AnsiStrip};
13pub use base::{
14    DecodeBase32, DecodeBase32Hex, DecodeBase64, DecodeHex, EncodeBase32, EncodeBase32Hex,
15    EncodeBase64, EncodeHex,
16};
17pub use char_::Char;
18pub use detect_columns::*;
19pub use encode_decode::*;
20pub use format::*;
21pub use parse::*;
22pub use split::*;
23pub use str_::*;
24
25use nu_engine::CallExt;
26use nu_protocol::{
27    ShellError,
28    engine::{Call, EngineState, Stack, StateWorkingSet},
29};
30
31// For handling the grapheme_cluster related flags on some commands.
32// This ensures the error messages are consistent.
33pub fn grapheme_flags(
34    engine_state: &EngineState,
35    stack: &mut Stack,
36    call: &Call,
37) -> Result<bool, ShellError> {
38    let g_flag = call.has_flag(engine_state, stack, "grapheme-clusters")?;
39    // Check for the other flags and produce errors if they exist.
40    // Note that Nushell already prevents nonexistent flags from being used with commands,
41    // so this function can be reused for both the --utf-8-bytes commands and the --code-points commands.
42    if g_flag && call.has_flag(engine_state, stack, "utf-8-bytes")? {
43        Err(ShellError::IncompatibleParametersSingle {
44            msg: "Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
45            span: call.head,
46        })?
47    }
48    if g_flag && call.has_flag(engine_state, stack, "code-points")? {
49        Err(ShellError::IncompatibleParametersSingle {
50            msg: "Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
51            span: call.head,
52        })?
53    }
54    // Grapheme cluster usage is decided by the non-default -g flag
55    Ok(g_flag)
56}
57
58// Const version of grapheme_flags
59pub fn grapheme_flags_const(
60    working_set: &StateWorkingSet,
61    call: &Call,
62) -> Result<bool, ShellError> {
63    let g_flag = call.has_flag_const(working_set, "grapheme-clusters")?;
64    if g_flag && call.has_flag_const(working_set, "utf-8-bytes")? {
65        Err(ShellError::IncompatibleParametersSingle {
66            msg: "Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
67            span: call.head,
68        })?
69    }
70    if g_flag && call.has_flag_const(working_set, "code-points")? {
71        Err(ShellError::IncompatibleParametersSingle {
72            msg: "Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
73            span: call.head,
74        })?
75    }
76    Ok(g_flag)
77}