zrx_diagnostic/diagnostic/location/position.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//! Position.
27
28use std::fmt::{self, Display, Write};
29
30// ----------------------------------------------------------------------------
31// Structs
32// ----------------------------------------------------------------------------
33
34/// Position.
35///
36/// Positions are used to represent a specific point in a text file, typically
37/// for error reporting or debugging purposes. They are defined by a line and
38/// column number, both of which use 0-based indexing, which is what the
39/// [Language Server Protocol][LSP] and [Source Maps] require.
40///
41/// However, when printing via [`fmt::Display`], the line and column numbers
42/// are formatted as 1-based indices, which is what most editors use.
43///
44/// [LSP]: https://microsoft.github.io/language-server-protocol/
45/// [Source Maps]: https://developer.mozilla.org/en-US/docs/Glossary/Source_map
46#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
47pub struct Position {
48 /// Line number, 0-based.
49 pub line: u32,
50 /// Column number, 0-based.
51 pub column: u32,
52}
53
54// ----------------------------------------------------------------------------
55// Implementations
56// ----------------------------------------------------------------------------
57
58impl Position {
59 /// Creates a position with the given line and column numbers.
60 ///
61 /// # Examples
62 ///
63 /// ```
64 /// use zrx_diagnostic::location::Position;
65 ///
66 /// // Create position
67 /// let position = Position::new(0, 0);
68 /// ```
69 #[must_use]
70 pub fn new(line: u32, column: u32) -> Self {
71 Self { line, column }
72 }
73}
74
75// ----------------------------------------------------------------------------
76// Trait implementations
77// ----------------------------------------------------------------------------
78
79impl From<(u32, u32)> for Position {
80 /// Creates a position from a tuple.
81 ///
82 /// # Examples
83 ///
84 /// ```
85 /// use zrx_diagnostic::location::Position;
86 ///
87 /// // Create position from tuple
88 /// let position: Position = (0, 0).into();
89 /// ```
90 #[inline]
91 fn from((line, column): (u32, u32)) -> Self {
92 Self { line, column }
93 }
94}
95
96// ----------------------------------------------------------------------------
97
98impl Display for Position {
99 /// Formats the position for display.
100 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
101 Display::fmt(&self.line.saturating_add(1), f)?;
102 f.write_char(':')?;
103 Display::fmt(&self.column.saturating_add(1), f)
104 }
105}