nu_command/strings/
mod.rs

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