nu_command/strings/
mod.rs

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