zrx_diagnostic/diagnostic/location/range.rs
1// Copyright (c) 2025-2026 Zensical and contributors
2
3// SPDX-License-Identifier: MIT
4// All contributions are certified under the DCO
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to
8// deal in the Software without restriction, including without limitation the
9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10// sell copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22// IN THE SOFTWARE.
23
24// ----------------------------------------------------------------------------
25
26//! Range.
27
28use std::fmt::{self, Display, Write};
29
30use super::position::Position;
31
32// ----------------------------------------------------------------------------
33// Structs
34// ----------------------------------------------------------------------------
35
36/// Range.
37///
38/// Ranges are used to represent start and end positions of text in a text file,
39/// which is particularly useful for error reporting and debugging purposes.
40#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
41pub struct Range {
42 /// Start position.
43 pub start: Position,
44 /// End position.
45 pub end: Position,
46}
47
48// ----------------------------------------------------------------------------
49// Implementations
50// ----------------------------------------------------------------------------
51
52impl Range {
53 /// Creates a range with the given start and end positions.
54 ///
55 /// # Examples
56 ///
57 /// ```
58 /// use zrx_diagnostic::location::{Position, Range};
59 ///
60 /// // Create range
61 /// let range = Range::new(
62 /// Position::new(0, 0),
63 /// Position::new(4, 0)
64 /// );
65 /// ```
66 #[inline]
67 #[must_use]
68 pub fn new<S, E>(start: S, end: E) -> Self
69 where
70 S: Into<Position>,
71 E: Into<Position>,
72 {
73 Self {
74 start: start.into(),
75 end: end.into(),
76 }
77 }
78}
79
80// ----------------------------------------------------------------------------
81// Trait implementations
82// ----------------------------------------------------------------------------
83
84impl<P> From<P> for Range
85where
86 P: Into<Position>,
87{
88 /// Creates a range at a single position.
89 ///
90 /// # Examples
91 ///
92 /// ```
93 /// use zrx_diagnostic::location::{Position, Range};
94 ///
95 /// // Create range from position
96 /// let range = Range::from(Position::new(0, 0));
97 /// ```
98 #[inline]
99 fn from(position: P) -> Self {
100 let start = position.into();
101 Self { start, end: start }
102 }
103}
104
105// ----------------------------------------------------------------------------
106
107impl Display for Range {
108 /// Formats the range for display.
109 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
110 Display::fmt(&self.start, f)?;
111 if self.start != self.end {
112 f.write_char('-')?;
113 Display::fmt(&self.end, f)?;
114 }
115
116 // No errors occurred
117 Ok(())
118 }
119}