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
409
410
411
// Copyright Amazon.com, Inc. or its affiliates.

//! Types representing positions, spans, locations, etc of parsed `PartiQL` text.

use std::fmt;
use std::fmt::{Debug, Display, Formatter};
use std::num::NonZeroUsize;
use std::ops::{Add, Range, Sub};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

macro_rules! impl_pos {
    ($pos_type:ident, $primitive:ty) => {
        impl Add for $pos_type {
            type Output = Self;

            fn add(self, rhs: Self) -> Self::Output {
                Self(self.0 + rhs.0)
            }
        }
        impl Add<$primitive> for $pos_type {
            type Output = Self;

            fn add(self, rhs: $primitive) -> Self::Output {
                Self(self.0 + rhs)
            }
        }
        impl Sub for $pos_type {
            type Output = Self;

            fn sub(self, rhs: Self) -> Self::Output {
                Self(self.0 - rhs.0)
            }
        }
        impl Sub<$primitive> for $pos_type {
            type Output = Self;

            fn sub(self, rhs: $primitive) -> Self::Output {
                Self(self.0 - rhs)
            }
        }
        impl $pos_type {
            /// Constructs from a `usize`
            #[inline(always)]
            #[must_use]
            pub fn from_usize(n: usize) -> Self {
                Self(n as $primitive)
            }

            /// Converts to a `usize`
            #[inline(always)]
            #[must_use]
            pub fn to_usize(&self) -> usize {
                self.0 as usize
            }
        }
        impl From<usize> for $pos_type {
            fn from(n: usize) -> Self {
                Self::from_usize(n)
            }
        }
    };
}

/// A 0-indexed byte offset, relative to some other position.
///
/// This type is small (u32 currently) to allow it to be included in ASTs and other
/// data structures.
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ByteOffset(pub u32);
impl_pos!(ByteOffset, u32);

/// A 0-indexed line offset, relative to some other position.
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LineOffset(pub u32);
impl_pos!(LineOffset, u32);

/// A 0-indexed char offset, relative to some other position.
///
/// This value represents the number of unicode codepoints seen, so will differ
/// from [`ByteOffset`] for a given location in a &str if the string contains
/// non-ASCII unicode characters
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CharOffset(pub u32);
impl_pos!(CharOffset, u32);

/// A 0-indexed byte absolute position (i.e., relative to the start of a &str)
///
/// This type is small (u32 currently) to allow it to be included in ASTs and other
/// data structures.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BytePosition(pub ByteOffset);

impl From<ByteOffset> for BytePosition {
    fn from(offset: ByteOffset) -> Self {
        Self(offset)
    }
}

impl From<usize> for BytePosition {
    fn from(offset: usize) -> Self {
        Self(offset.into())
    }
}

impl fmt::Display for BytePosition {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let BytePosition(ByteOffset(n)) = self;
        write!(f, "b{n}")
    }
}

/// A 0-indexed line & char absolute position (i.e., relative to the start of a &str)
///
/// ## Example
/// ```
/// # use partiql_source_map::location::LineAndCharPosition;
/// assert_eq!("Beginning of &str: LineAndCharPosition { line: LineOffset(0), char: CharOffset(0) }",
///             format!("Beginning of &str: {:?}", LineAndCharPosition::new(0, 0)));
/// ```
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LineAndCharPosition {
    /// The 0-indexed line absolute position (i.e., relative to the start of a &str)
    pub line: LineOffset,
    /// The 0-indexed character absolute position (i.e., relative to the start of a &str)
    pub char: CharOffset,
}

impl LineAndCharPosition {
    /// Constructs at [`LineAndCharPosition`]
    #[inline]
    #[must_use]
    pub fn new(line: usize, char: usize) -> Self {
        Self {
            line: LineOffset::from_usize(line),
            char: CharOffset::from_usize(char),
        }
    }
}

/// A 1-indexed line and column location intended for usage in errors/warnings/lints/etc.
///
/// Both line and column are 1-indexed, as that is how most people think of lines and columns.
///
/// ## Example
/// ```
/// # use partiql_source_map::location::LineAndColumn;
/// assert_eq!("Beginning of &str: 1:1",format!("Beginning of &str: {}", LineAndColumn::new(1, 1).unwrap()));
/// ```
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LineAndColumn {
    /// The 1-indexed line absolute position (i.e., relative to the start of a &str)
    pub line: NonZeroUsize,
    /// The 1-indexed character absolute position (i.e., relative to the start of a &str)
    pub column: NonZeroUsize,
}

impl LineAndColumn {
    /// Constructs at [`LineAndColumn`] if non-zero-index invariants, else [`None`]
    #[inline]
    #[must_use]
    pub fn new(line: usize, column: usize) -> Option<Self> {
        Some(Self {
            line: NonZeroUsize::new(line)?,
            column: NonZeroUsize::new(column)?,
        })
    }

    /// Constructs at [`LineAndColumn`] without verifying 1-indexed invariant (i.e. nonzero).
    /// This results in undefined behaviour if either `line` or `column` is zero.
    ///
    /// # Safety
    ///
    /// Both `line` and `column` values must not be zero.
    #[inline]
    #[must_use]
    pub const unsafe fn new_unchecked(line: usize, column: usize) -> Self {
        Self {
            line: NonZeroUsize::new_unchecked(line),
            column: NonZeroUsize::new_unchecked(column),
        }
    }
}

impl From<LineAndCharPosition> for LineAndColumn {
    fn from(LineAndCharPosition { line, char }: LineAndCharPosition) -> Self {
        let line = line.to_usize() + 1;
        let column = char.to_usize() + 1;
        // SAFETY: +1 is added to each of line and char after upcasting from a smaller integer
        unsafe { LineAndColumn::new_unchecked(line, column) }
    }
}

impl fmt::Display for LineAndColumn {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}", self.line, self.column)
    }
}
/// A range with an inclusive start and exclusive end.
///
/// Basically, a [`Range`].
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Location<Loc: Display> {
    /// The start the range (inclusive).
    pub start: Loc,
    /// The end of the range (exclusive).
    pub end: Loc,
}

impl<Loc> fmt::Display for Location<Loc>
where
    Loc: Display,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "(")?;
        self.start.fmt(f)?;
        write!(f, "..")?;
        self.end.fmt(f)?;
        write!(f, ")")?;
        Ok(())
    }
}

impl<Loc> From<Range<Loc>> for Location<Loc>
where
    Loc: Display,
{
    fn from(Range { start, end }: Range<Loc>) -> Self {
        Location { start, end }
    }
}

/// A wrapper type that holds an `inner` value and a `location` for it
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Located<T, Loc: Display> {
    /// The item that has a location attached
    pub inner: T,
    /// The location of the error
    pub location: Location<Loc>,
}

/// Trait adding a `to_located` method to ease construction of [`Located`] from its inner value.
///
/// ## Example
///
/// ```rust
/// # use partiql_source_map::location::{ByteOffset, BytePosition, Located, ToLocated};
/// assert_eq!("blah".to_string().to_located(BytePosition::from(5)..BytePosition::from(10)),
///             Located{
///                 inner: "blah".to_string(),
///                 location:  (BytePosition(ByteOffset(5))..BytePosition(ByteOffset(10))).into()
///             });
/// ```
pub trait ToLocated<Loc: Display>: Sized {
    /// Create a [`Located`] from its inner value.
    fn to_located<IntoLoc>(self, location: IntoLoc) -> Located<Self, Loc>
    where
        IntoLoc: Into<Location<Loc>>,
    {
        Located {
            inner: self,
            location: location.into(),
        }
    }
}

// "Blanket" impl of `ToLocated` for all `T`
// See https://doc.rust-lang.org/book/ch10-02-traits.html#using-trait-bounds-to-conditionally-implement-methods
impl<T, Loc: Display> ToLocated<Loc> for T {}

impl<T, Loc: Display> Located<T, Loc> {
    /// Maps an `Located<T, Loc>` to `Located<T, Loc2>` by applying a function to the contained
    /// location and moving the contained `inner`
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use partiql_source_map::location::{ByteOffset, BytePosition, Located, ToLocated};
    /// assert_eq!("blah".to_string()
    ///                 .to_located(BytePosition::from(5)..BytePosition::from(10))
    ///                 .map_loc(|BytePosition(o)| BytePosition(o+5)),
    ///             Located{
    ///                 inner: "blah".to_string(),
    ///                 location: (BytePosition(ByteOffset(10))..BytePosition(ByteOffset(15))).into()
    ///             });
    /// ```
    pub fn map_loc<F, Loc2>(self, mut tx: F) -> Located<T, Loc2>
    where
        Loc2: Display,
        F: FnMut(Loc) -> Loc2,
    {
        let Located { inner, location } = self;
        let location = Range {
            start: tx(location.start),
            end: tx(location.end),
        };
        inner.to_located(location)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::num::NonZeroUsize;

    use crate::location::{ByteOffset, BytePosition, Located, Location};

    #[test]
    fn located() {
        let l1: Located<String, BytePosition> = "test"
            .to_string()
            .to_located(ByteOffset(0).into()..ByteOffset(42).into());

        assert_eq!(l1.inner, "test");
        assert_eq!(l1.location.start.0 .0, 0);
        assert_eq!(l1.location.end.0 .0, 42);
        assert_eq!(l1.location.to_string(), "(b0..b42)");

        let l1c = l1.clone();
        assert!(matches!(
            l1c,
            Located {
                inner: s,
                location: Location {
                    start:BytePosition(ByteOffset(0)),
                    end: BytePosition(ByteOffset(42))
                }
            } if s == "test"
        ));

        let l2 = l1.map_loc(|x| x);

        assert!(matches!(
            l2.location,
            Location {
                start: BytePosition(ByteOffset(0)),
                end: BytePosition(ByteOffset(42))
            }
        ));
    }

    #[test]
    fn byteoff() {
        let offset1 = ByteOffset(5);
        let offset2 = ByteOffset::from_usize(15);

        assert_eq!(20, (offset1 + offset2).to_usize());
        assert_eq!(10, (offset2 - offset1).to_usize());
        assert_eq!(ByteOffset(10), offset2 - 5);
        assert_eq!(ByteOffset(20), offset2 + 5);
    }

    #[test]
    fn lineoff() {
        let offset1 = LineOffset(5);
        let offset2 = LineOffset::from_usize(15);

        assert_eq!(20, (offset1 + offset2).to_usize());
        assert_eq!(10, (offset2 - offset1).to_usize());
        assert_eq!(LineOffset(10), offset2 - 5);
        assert_eq!(LineOffset(20), offset2 + 5);
    }

    #[test]
    fn charoff() {
        let offset1 = CharOffset(5);
        let offset2 = CharOffset::from_usize(15);

        assert_eq!(20, (offset1 + offset2).to_usize());
        assert_eq!(10, (offset2 - offset1).to_usize());
        assert_eq!(CharOffset(10), offset2 - 5);
        assert_eq!(CharOffset(20), offset2 + 5);
    }

    #[test]
    fn positions() {
        assert_eq!(BytePosition(ByteOffset(15)), BytePosition(15.into()));
        assert_eq!(BytePosition(ByteOffset(5)), ByteOffset(5).into());
        assert_eq!(BytePosition(ByteOffset(25)), 25.into());
        assert_eq!("b25", format!("{}", BytePosition(ByteOffset(25))));
        assert_eq!("b25", BytePosition(ByteOffset(25)).to_string());

        let loc = LineAndCharPosition::new(13, 42);
        assert_eq!(
            LineAndCharPosition {
                line: LineOffset(13),
                char: CharOffset(42)
            },
            loc
        );
        let display = LineAndColumn {
            line: unsafe { NonZeroUsize::new_unchecked(14) },
            column: unsafe { NonZeroUsize::new_unchecked(43) },
        };

        assert_eq!(display, loc.into());
        assert_eq!(display, unsafe { LineAndColumn::new_unchecked(14, 43) });
        assert_eq!(display, LineAndColumn::new(14, 43).unwrap());
        assert_eq!("14:43", format!("{display}"));
        assert_eq!("14:43", display.to_string());
    }
}