nu_command/strings/
mod.rs

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