nu-protocol 0.112.1

Nushell's internal protocols, including its abstract syntax tree
Documentation
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! [`Span`] to point to sections of source code and the [`Spanned`] wrapper type
use crate::shell_error::generic::GenericError;
use crate::{FromValue, IntoValue, ShellError, SpanId, Value, record};
use miette::SourceSpan;
use serde::{Deserialize, Serialize};
use std::{fmt, ops::Deref};

pub trait GetSpan {
    fn get_span(&self, span_id: SpanId) -> Span;
}

/// A spanned area of interest, generic over what kind of thing is of interest
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Spanned<T> {
    pub item: T,
    pub span: Span,
}

impl<T> Spanned<T> {
    /// Map to a spanned reference of the inner type, i.e. `Spanned<T> -> Spanned<&T>`.
    pub fn as_ref(&self) -> Spanned<&T> {
        Spanned {
            item: &self.item,
            span: self.span,
        }
    }

    /// Map to a mutable reference of the inner type, i.e. `Spanned<T> -> Spanned<&mut T>`.
    pub fn as_mut(&mut self) -> Spanned<&mut T> {
        Spanned {
            item: &mut self.item,
            span: self.span,
        }
    }

    /// Map to the result of [`.deref()`](std::ops::Deref::deref) on the inner type.
    ///
    /// This can be used for example to turn `Spanned<Vec<T>>` into `Spanned<&[T]>`.
    pub fn as_deref(&self) -> Spanned<&<T as Deref>::Target>
    where
        T: Deref,
    {
        Spanned {
            item: self.item.deref(),
            span: self.span,
        }
    }

    /// Map the spanned item with a function.
    pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Spanned<U> {
        Spanned {
            item: f(self.item),
            span: self.span,
        }
    }
}

impl<T> Spanned<&T>
where
    T: ToOwned + ?Sized,
{
    /// Map the spanned to hold an owned value.
    pub fn to_owned(&self) -> Spanned<T::Owned> {
        Spanned {
            item: self.item.to_owned(),
            span: self.span,
        }
    }
}

impl<T> Spanned<T>
where
    T: AsRef<str>,
{
    /// Span the value as a string slice.
    pub fn as_str(&self) -> Spanned<&str> {
        Spanned {
            item: self.item.as_ref(),
            span: self.span,
        }
    }
}

impl<T, E> Spanned<Result<T, E>> {
    /// Move the `Result` to the outside, resulting in a spanned `Ok` or unspanned `Err`.
    pub fn transpose(self) -> Result<Spanned<T>, E> {
        match self {
            Spanned {
                item: Ok(item),
                span,
            } => Ok(Spanned { item, span }),
            Spanned {
                item: Err(err),
                span: _,
            } => Err(err),
        }
    }
}

// With both Display and Into<SourceSpan> implemented on Spanned, we can use Spanned<String> in an
// error in one field instead of splitting it into two fields

impl<T: fmt::Display> fmt::Display for Spanned<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.item, f)
    }
}

impl<T> From<Spanned<T>> for SourceSpan {
    fn from(value: Spanned<T>) -> Self {
        value.span.into()
    }
}

/// Helper trait to create [`Spanned`] more ergonomically.
pub trait IntoSpanned: Sized {
    /// Wrap items together with a span into [`Spanned`].
    ///
    /// # Example
    ///
    /// ```
    /// # use nu_protocol::{Span, IntoSpanned};
    /// # let span = Span::test_data();
    /// let spanned = "Hello, world!".into_spanned(span);
    /// assert_eq!("Hello, world!", spanned.item);
    /// assert_eq!(span, spanned.span);
    /// ```
    fn into_spanned(self, span: Span) -> Spanned<Self>;
}

impl<T> IntoSpanned for T {
    fn into_spanned(self, span: Span) -> Spanned<Self> {
        Spanned { item: self, span }
    }
}

/// Spans are a global offset across all seen files, which are cached in the engine's state. The start and
/// end offset together make the inclusive start/exclusive end pair for where to underline to highlight
/// a given point of interest.
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Span {
    pub start: usize,
    pub end: usize,
}

impl fmt::Debug for Span {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        const TEST_DATA: Span = Span::test_data();
        const UNKNOWN: Span = Span::unknown();

        match *self {
            TEST_DATA => write!(f, "Span(TEST)"),
            UNKNOWN => write!(f, "Span(UNKNOWN)"),
            Span { start, end } => write!(f, "Span[{start}..{end}]"),
        }
    }
}

impl Span {
    pub fn new(start: usize, end: usize) -> Self {
        debug_assert!(
            end >= start,
            "Can't create a Span whose end < start, start={start}, end={end}"
        );

        Self { start, end }
    }

    pub const fn unknown() -> Self {
        Self { start: 0, end: 0 }
    }

    /// Span for testing purposes.
    ///
    /// The provided span does not point into any known source but is unequal to [`Span::unknown()`].
    ///
    /// Note: Only use this for test data, *not* live data, as it will point into unknown source
    /// when used in errors
    pub const fn test_data() -> Self {
        Self {
            start: usize::MAX / 2,
            end: usize::MAX / 2,
        }
    }

    pub fn offset(&self, offset: usize) -> Self {
        Self::new(self.start - offset, self.end - offset)
    }

    /// Return length of the slice.
    pub fn len(&self) -> usize {
        self.end - self.start
    }

    /// Indicate if slice has length 0.
    pub fn is_empty(&self) -> bool {
        self.start == self.end
    }

    /// Return another span fully inside the [`Span`].
    ///
    /// `start` and `end` are relative to `self.start`, and must lie within the `Span`.
    /// In other words, both `start` and `end` must be `<= self.len()`.
    pub fn subspan(&self, offset_start: usize, offset_end: usize) -> Option<Self> {
        let len = self.len();

        if offset_start > len || offset_end > len || offset_start > offset_end {
            None
        } else {
            Some(Self::new(
                self.start + offset_start,
                self.start + offset_end,
            ))
        }
    }

    /// Return two spans that split the ['Span'] at the given position.
    pub fn split_at(&self, offset: usize) -> Option<(Self, Self)> {
        if offset < self.len() {
            Some((
                Self::new(self.start, self.start + offset),
                Self::new(self.start + offset, self.end),
            ))
        } else {
            None
        }
    }

    pub fn contains(&self, pos: usize) -> bool {
        self.start <= pos && pos < self.end
    }

    pub fn contains_span(&self, span: Self) -> bool {
        self.start <= span.start && span.end <= self.end && span.end != 0
    }

    /// Point to the space just past this span, useful for missing values
    pub fn past(&self) -> Self {
        Self {
            start: self.end,
            end: self.end,
        }
    }

    /// Converts row and column in a String to a Span, assuming bytes (1-based rows)
    pub fn from_row_column(row: usize, col: usize, contents: &str) -> Span {
        let mut cur_row = 1;
        let mut cur_col = 1;

        for (offset, curr_byte) in contents.bytes().enumerate() {
            if curr_byte == b'\n' {
                cur_row += 1;
                cur_col = 1;
            } else if cur_row >= row && cur_col >= col {
                return Span::new(offset, offset);
            } else {
                cur_col += 1;
            }
        }

        Self {
            start: contents.len(),
            end: contents.len(),
        }
    }

    /// Returns the minimal [`Span`] that encompasses both of the given spans.
    ///
    /// The two `Spans` can overlap in the middle,
    /// but must otherwise be in order by satisfying:
    /// - `self.start <= after.start`
    /// - `self.end <= after.end`
    ///
    /// If this is not guaranteed to be the case, use [`Span::merge`] instead.
    pub fn append(self, after: Self) -> Self {
        debug_assert!(
            self.start <= after.start && self.end <= after.end,
            "Can't merge two Spans that are not in order"
        );
        Self {
            start: self.start,
            end: after.end,
        }
    }

    /// Returns the minimal [`Span`] that encompasses both of the given spans.
    ///
    /// The spans need not be in order or have any relationship.
    ///
    /// [`Span::append`] is slightly more efficient if the spans are known to be in order.
    pub fn merge(self, other: Self) -> Self {
        Self {
            start: usize::min(self.start, other.start),
            end: usize::max(self.end, other.end),
        }
    }

    /// Returns the minimal [`Span`] that encompasses all of the spans in the given slice.
    ///
    /// The spans are assumed to be in order, that is, all consecutive spans must satisfy:
    /// - `spans[i].start <= spans[i + 1].start`
    /// - `spans[i].end <= spans[i + 1].end`
    ///
    /// (Two consecutive spans can overlap as long as the above is true.)
    ///
    /// Use [`Span::merge_many`] if the spans are not known to be in order.
    pub fn concat(spans: &[Self]) -> Self {
        // TODO: enable assert below
        // debug_assert!(!spans.is_empty());
        debug_assert!(spans.windows(2).all(|spans| {
            let &[a, b] = spans else {
                return false;
            };
            a.start <= b.start && a.end <= b.end
        }));
        Self {
            start: spans.first().map(|s| s.start).unwrap_or(0),
            end: spans.last().map(|s| s.end).unwrap_or(0),
        }
    }

    /// Returns the minimal [`Span`] that encompasses all of the spans in the given iterator.
    ///
    /// The spans need not be in order or have any relationship.
    ///
    /// [`Span::concat`] is more efficient if the spans are known to be in order.
    pub fn merge_many(spans: impl IntoIterator<Item = Self>) -> Self {
        spans
            .into_iter()
            .reduce(Self::merge)
            .unwrap_or(Self::unknown())
    }
}

impl IntoValue for Span {
    fn into_value(self, span: Span) -> Value {
        let record = record! {
            "start" => Value::int(self.start as i64, self),
            "end" => Value::int(self.end as i64, self),
        };
        record.into_value(span)
    }
}

impl FromValue for Span {
    fn from_value(value: Value) -> Result<Self, ShellError> {
        let rec = value.as_record();
        match rec {
            Ok(val) => {
                let Some(pre_start) = val.get("start") else {
                    return Err(ShellError::Generic(GenericError::new(
                        "Unable to parse Span.",
                        "`start` must be an `int`",
                        value.span(),
                    )));
                };
                let Some(pre_end) = val.get("end") else {
                    return Err(ShellError::Generic(GenericError::new(
                        "Unable to parse Span.",
                        "`end` must be an `int`",
                        value.span(),
                    )));
                };
                let start = pre_start.as_int()? as usize;
                let end = pre_end.as_int()? as usize;
                if start <= end {
                    Ok(Self::new(start, end))
                } else {
                    Err(ShellError::Generic(GenericError::new(
                        "Unable to parse Span.",
                        "`end` must not be less than `start`",
                        value.span(),
                    )))
                }
            }
            _ => Err(ShellError::TypeMismatch {
                err_message: "Must be a record".into(),
                span: value.span(),
            }),
        }
    }
}

impl From<Span> for SourceSpan {
    fn from(s: Span) -> Self {
        Self::new(s.start.into(), s.end - s.start)
    }
}

/// An extension trait for [`Result`], which adds a span to the error type.
///
/// This trait might be removed later, since the old [`Spanned<std::io::Error>`] to
/// [`ShellError`] conversion was replaced by
/// [`IoError`](crate::shell_error::io::IoError).
pub trait ErrSpan {
    type Result;

    /// Adds the given span to the error type, turning it into a [`Spanned<E>`].
    fn err_span(self, span: Span) -> Self::Result;
}

impl<T, E> ErrSpan for Result<T, E> {
    type Result = Result<T, Spanned<E>>;

    fn err_span(self, span: Span) -> Self::Result {
        self.map_err(|err| err.into_spanned(span))
    }
}