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
use nu_errors::ParseError;
use nu_protocol::hir::{Expression, SpannedExpression};
use nu_source::{Span, Spanned, SpannedItem};

use crate::lex::lexer::Token;

pub(crate) fn token_to_spanned_string(token: &Token) -> Spanned<String> {
    token.contents.to_string().spanned(token.span)
}

/// Easy shorthand function to create a garbage expression at the given span
pub fn garbage(span: Span) -> SpannedExpression {
    SpannedExpression::new(Expression::Garbage, span)
}

pub(crate) fn trim_quotes(input: &str) -> String {
    let mut chars = input.chars();

    match (chars.next(), chars.next_back()) {
        (Some('\''), Some('\'')) => chars.collect(),
        (Some('"'), Some('"')) => chars.collect(),
        (Some('`'), Some('`')) => chars.collect(),
        _ => input.to_string(),
    }
}

pub(crate) fn verify_and_strip(
    contents: &Spanned<String>,
    left: char,
    right: char,
) -> (String, Option<ParseError>) {
    let mut chars = contents.item.chars();

    match (chars.next(), chars.next_back()) {
        (Some(l), Some(r)) if l == left && r == right => {
            let output: String = chars.collect();
            (output, None)
        }
        _ => (
            String::new(),
            Some(ParseError::mismatch(
                format!("value in {} {}", left, right),
                contents.clone(),
            )),
        ),
    }
}