continuity_text/
position.rs1use ropey::Rope;
8use serde::{Deserialize, Serialize};
9
10use crate::Error;
11
12#[derive(
17 Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
18)]
19pub struct Position {
20 pub line: u32,
22 pub byte_in_line: u32,
24}
25
26impl Position {
27 pub const ZERO: Self = Self {
29 line: 0,
30 byte_in_line: 0,
31 };
32
33 #[must_use]
35 pub const fn new(line: u32, byte_in_line: u32) -> Self {
36 Self { line, byte_in_line }
37 }
38
39 pub fn to_byte_offset(self, rope: &Rope) -> Result<usize, Error> {
46 let line = self.line as usize;
47 if line > rope.len_lines() {
48 return Err(Error::OutOfBounds(line));
49 }
50 let line_start = rope
51 .try_line_to_byte(line)
52 .map_err(|_| Error::OutOfBounds(line))?;
53 let byte = line_start + self.byte_in_line as usize;
54 if byte > rope.len_bytes() {
55 return Err(Error::OutOfBounds(byte));
56 }
57 if !is_rope_char_boundary(rope, byte) {
58 return Err(Error::OutOfBounds(byte));
59 }
60 Ok(byte)
61 }
62
63 pub fn from_byte_offset(rope: &Rope, byte: usize) -> Result<Self, Error> {
70 if byte > rope.len_bytes() {
71 return Err(Error::OutOfBounds(byte));
72 }
73 if !is_rope_char_boundary(rope, byte) {
74 return Err(Error::OutOfBounds(byte));
75 }
76 let line = rope.byte_to_line(byte);
77 let line_start = rope.line_to_byte(line);
78 Ok(Self {
79 line: u32::try_from(line).map_err(|_| Error::OutOfBounds(line))?,
80 byte_in_line: u32::try_from(byte - line_start)
81 .map_err(|_| Error::OutOfBounds(byte - line_start))?,
82 })
83 }
84}
85
86fn is_rope_char_boundary(rope: &Rope, byte: usize) -> bool {
87 if byte > rope.len_bytes() {
88 return false;
89 }
90 if byte == rope.len_bytes() {
91 return true;
92 }
93 let Some((chunk, chunk_byte_start, _, _)) = rope.get_chunk_at_byte(byte) else {
94 return false;
95 };
96 chunk.is_char_boundary(byte.saturating_sub(chunk_byte_start))
97}
98
99#[cfg(test)]
100mod tests {
101 use ropey::Rope;
102
103 use super::*;
104
105 #[test]
106 fn zero_translates_to_byte_zero() {
107 let rope = Rope::from_str("hello\nworld");
108 assert_eq!(Position::ZERO.to_byte_offset(&rope).unwrap(), 0);
109 }
110
111 #[test]
112 fn line_two_resolves_correctly() {
113 let rope = Rope::from_str("hello\nworld");
114 assert_eq!(Position::new(1, 0).to_byte_offset(&rope).unwrap(), 6);
115 assert_eq!(Position::new(1, 5).to_byte_offset(&rope).unwrap(), 11);
116 }
117
118 #[test]
119 fn round_trip_through_byte_offset() {
120 let rope = Rope::from_str("a\nbb\nccc\ndddd");
121 for byte in 0..=rope.len_bytes() {
122 let p = Position::from_byte_offset(&rope, byte).unwrap();
123 assert_eq!(p.to_byte_offset(&rope).unwrap(), byte, "byte {byte}");
124 }
125 }
126
127 #[test]
128 fn out_of_bounds_byte_fails() {
129 let rope = Rope::from_str("hi");
130 assert!(Position::from_byte_offset(&rope, 999).is_err());
131 }
132
133 #[test]
134 fn out_of_bounds_position_fails() {
135 let rope = Rope::from_str("hi");
136 assert!(Position::new(99, 0).to_byte_offset(&rope).is_err());
137 }
138
139 #[test]
140 fn interior_utf8_byte_position_fails() {
141 let rope = Rope::from_str("é");
142 assert!(Position::new(0, 1).to_byte_offset(&rope).is_err());
143 }
144
145 #[test]
146 fn interior_utf8_byte_offset_fails() {
147 let rope = Rope::from_str("é");
148 assert!(Position::from_byte_offset(&rope, 1).is_err());
149 }
150}