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
mod char_;
mod detect_columns;
mod encode_decode;
mod format;
mod parse;
mod split;
mod str_;

pub use char_::Char;
pub use detect_columns::*;
pub use encode_decode::*;
pub use format::*;
use nu_engine::CallExt;
pub use parse::*;
pub use split::*;
pub use str_::*;

use nu_protocol::{
    ast::Call,
    engine::{EngineState, Stack, StateWorkingSet},
    ShellError,
};

// For handling the grapheme_cluster related flags on some commands.
// This ensures the error messages are consistent.
pub fn grapheme_flags(
    engine_state: &EngineState,
    stack: &mut Stack,
    call: &Call,
) -> Result<bool, ShellError> {
    let g_flag = call.has_flag(engine_state, stack, "grapheme-clusters")?;
    // Check for the other flags and produce errors if they exist.
    // Note that Nushell already prevents nonexistent flags from being used with commands,
    // so this function can be reused for both the --utf-8-bytes commands and the --code-points commands.
    if g_flag && call.has_flag(engine_state, stack, "utf-8-bytes")? {
        Err(ShellError::IncompatibleParametersSingle {
            msg: "Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
            span: call.head,
        })?
    }
    if g_flag && call.has_flag(engine_state, stack, "code-points")? {
        Err(ShellError::IncompatibleParametersSingle {
            msg: "Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
            span: call.head,
        })?
    }
    // Grapheme cluster usage is decided by the non-default -g flag
    Ok(g_flag)
}

// Const version of grapheme_flags
pub fn grapheme_flags_const(
    working_set: &StateWorkingSet,
    call: &Call,
) -> Result<bool, ShellError> {
    let g_flag = call.has_flag_const(working_set, "grapheme-clusters")?;
    if g_flag && call.has_flag_const(working_set, "utf-8-bytes")? {
        Err(ShellError::IncompatibleParametersSingle {
            msg: "Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
            span: call.head,
        })?
    }
    if g_flag && call.has_flag_const(working_set, "code-points")? {
        Err(ShellError::IncompatibleParametersSingle {
            msg: "Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
            span: call.head,
        })?
    }
    Ok(g_flag)
}