miden_debug_types/
selection.rs1#[cfg(feature = "serde")]
2use serde::{Deserialize, Serialize};
3
4use super::{ColumnIndex, LineIndex};
5
6#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
10#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
11pub struct Selection {
12 pub start: Position,
13 pub end: Position,
14}
15
16impl Selection {
17 #[inline]
18 pub fn new(start: Position, end: Position) -> Self {
19 let start = core::cmp::min(start, end);
20 let end = core::cmp::max(start, end);
21 Self { start, end }
22 }
23
24 pub fn canonicalize(&mut self) {
25 if self.end > self.start {
26 core::mem::swap(&mut self.start, &mut self.end);
27 }
28 }
29}
30
31impl From<core::ops::Range<Position>> for Selection {
32 #[inline]
33 fn from(value: core::ops::Range<Position>) -> Self {
34 Self::new(value.start, value.end)
35 }
36}
37
38impl From<core::ops::Range<LineIndex>> for Selection {
39 #[inline]
40 fn from(value: core::ops::Range<LineIndex>) -> Self {
41 Self::new(value.start.into(), value.end.into())
42 }
43}
44
45#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
49#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
50pub struct Position {
51 pub line: LineIndex,
52 pub character: ColumnIndex,
53}
54
55impl Position {
56 pub const fn new(line: u32, character: u32) -> Self {
57 Self {
58 line: LineIndex(line),
59 character: ColumnIndex(character),
60 }
61 }
62}
63
64impl From<LineIndex> for Position {
65 #[inline]
66 fn from(line: LineIndex) -> Self {
67 Self { line, character: ColumnIndex(0) }
68 }
69}