1#![allow(
2 clippy::empty_enums,
3 reason = "zerocopy derives generate uninhabited helper types"
4)]
5
6use alloc::{
7 boxed::Box,
8 string::{String, ToString},
9 sync::Arc,
10 vec::Vec,
11};
12use core::{fmt, num::NonZeroU32, ops::Range};
13
14#[cfg(feature = "arbitrary")]
15use proptest::prelude::*;
16#[cfg(feature = "serde")]
17use serde::{Deserialize, Serialize};
18
19use super::{
20 ByteReader, ByteWriter, Deserializable, DeserializationError, FileLineCol, Position, Selection,
21 Serializable, SourceId, SourceSpan, Uri,
22};
23
24#[derive(Debug, Copy, Clone, PartialEq, Eq)]
28pub enum SourceLanguage {
29 Masm,
30 Rust,
31 Other(&'static str),
32}
33
34#[cfg(feature = "arbitrary")]
35impl Arbitrary for SourceLanguage {
36 type Parameters = ();
37 type Strategy = BoxedStrategy<Self>;
38
39 fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
40 prop_oneof![
41 Just(Self::Masm),
42 Just(Self::Rust),
43 Just(Self::Other("other")),
44 Just(Self::Other("unknown")),
45 ]
46 .boxed()
47 }
48}
49
50impl AsRef<str> for SourceLanguage {
51 fn as_ref(&self) -> &str {
52 match self {
53 Self::Masm => "masm",
54 Self::Rust => "rust",
55 Self::Other(other) => other,
56 }
57 }
58}
59
60#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
65#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
66pub struct SourceFile {
67 id: SourceId,
69 #[cfg_attr(
71 feature = "serde",
72 serde(deserialize_with = "SourceContent::deserialize_and_recompute_line_starts")
73 )]
74 content: SourceContent,
75}
76
77impl miette::SourceCode for SourceFile {
78 fn read_span<'a>(
79 &'a self,
80 span: &miette::SourceSpan,
81 context_lines_before: usize,
82 context_lines_after: usize,
83 ) -> Result<Box<dyn miette::SpanContents<'a> + 'a>, miette::MietteError> {
84 let mut start =
85 u32::try_from(span.offset()).map_err(|_| miette::MietteError::OutOfBounds)?;
86 let len = u32::try_from(span.len()).map_err(|_| miette::MietteError::OutOfBounds)?;
87 let mut end = start.checked_add(len).ok_or(miette::MietteError::OutOfBounds)?;
88 if context_lines_before > 0 {
89 let line_index = self.content.line_index(start.into());
90 let start_line_index = line_index.saturating_sub(context_lines_before as u32);
91 start = self.content.line_start(start_line_index).map(ByteIndex::to_u32).unwrap_or(0);
92 }
93 if context_lines_after > 0 {
94 let line_index = self.content.line_index(end.into());
95 let end_line_index = line_index
96 .checked_add(context_lines_after as u32)
97 .ok_or(miette::MietteError::OutOfBounds)?;
98 end = self
99 .content
100 .line_range(end_line_index)
101 .map(|range| range.end.to_u32())
102 .unwrap_or_else(|| self.content.source_range().end.to_u32());
103 }
104 Ok(Box::new(ScopedSourceFileRef {
105 file: self,
106 span: miette::SourceSpan::new((start as usize).into(), end.abs_diff(start) as usize),
107 }))
108 }
109}
110
111impl SourceFile {
112 pub fn new(id: SourceId, lang: SourceLanguage, uri: Uri, content: impl Into<Box<str>>) -> Self {
114 let content = SourceContent::new(lang, uri, content.into());
115 Self { id, content }
116 }
117
118 pub fn from_raw_parts(id: SourceId, content: SourceContent) -> Self {
135 Self { id, content }
136 }
137
138 pub const fn id(&self) -> SourceId {
140 self.id
141 }
142
143 pub fn uri(&self) -> &Uri {
145 self.content.uri()
146 }
147
148 pub fn content(&self) -> &SourceContent {
150 &self.content
151 }
152
153 pub fn content_mut(&mut self) -> &mut SourceContent {
155 &mut self.content
156 }
157
158 pub fn line_count(&self) -> usize {
160 self.content.line_starts.len()
161 }
162
163 pub fn len(&self) -> usize {
165 self.content.len()
166 }
167
168 pub fn is_empty(&self) -> bool {
170 self.content.is_empty()
171 }
172
173 #[inline(always)]
175 pub fn as_str(&self) -> &str {
176 self.content.as_str()
177 }
178
179 #[inline(always)]
181 pub fn as_bytes(&self) -> &[u8] {
182 self.content.as_bytes()
183 }
184
185 #[inline]
187 pub fn source_span(&self) -> SourceSpan {
188 let range = self.content.source_range();
189 SourceSpan::new(self.id, range.start.0..range.end.0)
190 }
191
192 #[inline(always)]
199 pub fn source_slice(&self, span: impl Into<Range<usize>>) -> Option<&str> {
200 self.content.source_slice(span)
201 }
202
203 pub fn slice(self: &Arc<Self>, span: impl Into<Range<u32>>) -> SourceFileRef {
205 SourceFileRef::new(Arc::clone(self), span)
206 }
207
208 pub fn line_column_to_span(
212 &self,
213 line: LineNumber,
214 column: ColumnNumber,
215 ) -> Option<SourceSpan> {
216 let offset = self.content.line_column_to_offset(line.into(), column.into())?;
217 Some(SourceSpan::at(self.id, offset.0))
218 }
219
220 pub fn location(&self, span: SourceSpan) -> FileLineCol {
222 assert_eq!(span.source_id(), self.id, "mismatched source ids");
223
224 self.content
225 .location(ByteIndex(span.into_range().start))
226 .expect("invalid source span: starting byte is out of bounds")
227 }
228}
229
230impl AsRef<str> for SourceFile {
231 #[inline(always)]
232 fn as_ref(&self) -> &str {
233 self.as_str()
234 }
235}
236
237impl AsRef<[u8]> for SourceFile {
238 #[inline(always)]
239 fn as_ref(&self) -> &[u8] {
240 self.as_bytes()
241 }
242}
243
244#[derive(Debug, Clone)]
254pub struct SourceFileRef {
255 file: Arc<SourceFile>,
256 span: SourceSpan,
257}
258
259impl SourceFileRef {
260 pub fn new(file: Arc<SourceFile>, span: impl Into<Range<u32>>) -> Self {
265 let span = span.into();
266 let end = core::cmp::min(span.end, file.len() as u32);
267 let span = SourceSpan::new(file.id(), span.start..end);
268 Self { file, span }
269 }
270
271 pub fn source_file(&self) -> Arc<SourceFile> {
273 self.file.clone()
274 }
275
276 pub fn uri(&self) -> &Uri {
278 self.file.uri()
279 }
280
281 pub const fn span(&self) -> SourceSpan {
283 self.span
284 }
285
286 pub fn as_str(&self) -> &str {
288 self.file.source_slice(self.span).unwrap()
289 }
290
291 #[inline]
293 pub fn as_bytes(&self) -> &[u8] {
294 self.as_str().as_bytes()
295 }
296
297 pub fn len(&self) -> usize {
300 self.span.len()
301 }
302
303 pub fn is_empty(&self) -> bool {
305 self.len() == 0
306 }
307}
308
309impl Eq for SourceFileRef {}
310
311impl PartialEq for SourceFileRef {
312 fn eq(&self, other: &Self) -> bool {
313 self.as_str() == other.as_str()
314 }
315}
316
317impl Ord for SourceFileRef {
318 fn cmp(&self, other: &Self) -> core::cmp::Ordering {
319 self.as_str().cmp(other.as_str())
320 }
321}
322
323impl PartialOrd for SourceFileRef {
324 #[inline(always)]
325 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
326 Some(self.cmp(other))
327 }
328}
329
330impl core::hash::Hash for SourceFileRef {
331 fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
332 self.as_str().hash(state);
333 }
334}
335
336impl AsRef<str> for SourceFileRef {
337 #[inline(always)]
338 fn as_ref(&self) -> &str {
339 self.as_str()
340 }
341}
342
343impl AsRef<[u8]> for SourceFileRef {
344 #[inline(always)]
345 fn as_ref(&self) -> &[u8] {
346 self.as_bytes()
347 }
348}
349
350impl From<&SourceFileRef> for miette::SourceSpan {
351 fn from(source: &SourceFileRef) -> Self {
352 source.span.into()
353 }
354}
355
356struct ScopedSourceFileRef<'a> {
358 file: &'a SourceFile,
359 span: miette::SourceSpan,
360}
361
362impl<'a> miette::SpanContents<'a> for ScopedSourceFileRef<'a> {
363 #[inline]
364 fn data(&self) -> &'a [u8] {
365 let start = self.span.offset();
366 let end = start + self.span.len();
367 &self.file.as_bytes()[start..end]
368 }
369
370 #[inline]
371 fn span(&self) -> &miette::SourceSpan {
372 &self.span
373 }
374
375 fn line(&self) -> usize {
376 let offset = self.span.offset() as u32;
377 self.file.content.line_index(offset.into()).to_usize()
378 }
379
380 fn column(&self) -> usize {
381 let start = self.span.offset() as u32;
382 let end = start + self.span.len() as u32;
383 let span = SourceSpan::new(self.file.id(), start..end);
384 let loc = self.file.location(span);
385 loc.column.to_index().to_usize()
386 }
387
388 #[inline]
389 fn line_count(&self) -> usize {
390 self.file.line_count()
391 }
392
393 #[inline]
394 fn name(&self) -> Option<&str> {
395 Some(self.file.uri().as_ref())
396 }
397
398 #[inline]
399 fn language(&self) -> Option<&str> {
400 None
401 }
402}
403
404impl miette::SourceCode for SourceFileRef {
405 #[inline]
406 fn read_span<'a>(
407 &'a self,
408 span: &miette::SourceSpan,
409 context_lines_before: usize,
410 context_lines_after: usize,
411 ) -> Result<Box<dyn miette::SpanContents<'a> + 'a>, miette::MietteError> {
412 self.file.read_span(span, context_lines_before, context_lines_after)
413 }
414}
415
416#[derive(Clone)]
425#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
426pub struct SourceContent {
427 language: Box<str>,
429 uri: Uri,
431 content: String,
433 #[cfg_attr(feature = "serde", serde(default, skip))]
435 line_starts: Vec<ByteIndex>,
436 #[cfg_attr(feature = "serde", serde(default))]
438 version: i32,
439}
440
441impl fmt::Debug for SourceContent {
442 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
443 let Self {
444 language,
445 uri,
446 content,
447 line_starts,
448 version,
449 } = self;
450 f.debug_struct("SourceContent")
451 .field("version", version)
452 .field("language", language)
453 .field("uri", uri)
454 .field("size_in_bytes", &content.len())
455 .field("line_count", &line_starts.len())
456 .field("content", content)
457 .finish()
458 }
459}
460
461impl Eq for SourceContent {}
462
463impl PartialEq for SourceContent {
464 #[inline]
465 fn eq(&self, other: &Self) -> bool {
466 self.language == other.language && self.uri == other.uri && self.content == other.content
467 }
468}
469
470impl Ord for SourceContent {
471 #[inline]
472 fn cmp(&self, other: &Self) -> core::cmp::Ordering {
473 self.uri.cmp(&other.uri).then_with(|| self.content.cmp(&other.content))
474 }
475}
476
477impl PartialOrd for SourceContent {
478 #[inline]
479 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
480 Some(self.cmp(other))
481 }
482}
483
484impl core::hash::Hash for SourceContent {
485 fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
486 self.language.hash(state);
487 self.uri.hash(state);
488 self.content.hash(state);
489 }
490}
491
492#[derive(Debug, thiserror::Error)]
493pub enum SourceContentUpdateError {
494 #[error("invalid content selection: start position of {}:{} is out of bounds", .0.line, .0.character)]
495 InvalidSelectionStart(Position),
496 #[error("invalid content selection: end position of {}:{} is out of bounds", .0.line, .0.character)]
497 InvalidSelectionEnd(Position),
498}
499
500impl SourceContent {
501 pub fn new(language: impl AsRef<str>, uri: impl Into<Uri>, content: impl Into<String>) -> Self {
507 let language = language.as_ref().to_string().into_boxed_str();
508 let content: String = content.into();
509 let bytes = content.as_bytes();
510
511 assert!(
512 bytes.len() < u32::MAX as usize,
513 "unsupported source file: current maximum supported length in bytes is 2^32"
514 );
515
516 let line_starts = compute_line_starts(&content, None);
517
518 Self {
519 language,
520 uri: uri.into(),
521 content,
522 line_starts,
523 version: 0,
524 }
525 }
526
527 pub fn language(&self) -> &str {
529 &self.language
530 }
531
532 pub fn version(&self) -> i32 {
534 self.version
535 }
536
537 #[inline(always)]
539 pub fn set_version(&mut self, version: i32) {
540 self.version = version;
541 }
542
543 #[inline]
545 pub fn uri(&self) -> &Uri {
546 &self.uri
547 }
548
549 #[inline(always)]
551 pub fn as_str(&self) -> &str {
552 self.content.as_ref()
553 }
554
555 #[inline(always)]
557 pub fn as_bytes(&self) -> &[u8] {
558 self.content.as_bytes()
559 }
560
561 #[inline(always)]
563 pub fn len(&self) -> usize {
564 self.content.len()
565 }
566
567 #[inline(always)]
569 pub fn is_empty(&self) -> bool {
570 self.content.is_empty()
571 }
572
573 #[inline]
575 pub fn source_range(&self) -> Range<ByteIndex> {
576 ByteIndex(0)..ByteIndex(self.content.len() as u32)
577 }
578
579 #[inline(always)]
586 pub fn source_slice(&self, span: impl Into<Range<usize>>) -> Option<&str> {
587 self.as_str().get(span.into())
588 }
589
590 #[inline(always)]
594 pub fn byte_slice(&self, span: impl Into<Range<ByteIndex>>) -> Option<&[u8]> {
595 let Range { start, end } = span.into();
596 self.as_bytes().get(start.to_usize()..end.to_usize())
597 }
598
599 pub fn select(&self, mut range: Selection) -> Option<&str> {
604 range.canonicalize();
605
606 let start = self.line_column_to_offset(range.start.line, range.start.character)?;
607 let end = self.line_column_to_offset(range.end.line, range.end.character)?;
608
609 Some(&self.as_str()[start.to_usize()..end.to_usize()])
610 }
611
612 pub fn line_count(&self) -> usize {
614 self.line_starts.len()
615 }
616
617 pub fn line_start(&self, line_index: LineIndex) -> Option<ByteIndex> {
621 self.line_starts.get(line_index.to_usize()).copied()
622 }
623
624 pub fn last_line_index(&self) -> LineIndex {
626 LineIndex(self.line_count().saturating_sub(1).try_into().expect("too many lines in file"))
627 }
628
629 pub fn line_range(&self, line_index: LineIndex) -> Option<Range<ByteIndex>> {
631 let line_start = self.line_start(line_index)?;
632 match self.line_start(line_index + 1) {
633 Some(line_end) => Some(line_start..line_end),
634 None => Some(line_start..ByteIndex(self.content.len() as u32)),
635 }
636 }
637
638 pub fn line_index(&self, byte_index: ByteIndex) -> LineIndex {
640 match self.line_starts.binary_search(&byte_index) {
641 Ok(line) => LineIndex(line as u32),
642 Err(next_line) => LineIndex(next_line as u32 - 1),
643 }
644 }
645
646 pub fn line_column_to_offset(
653 &self,
654 line_index: LineIndex,
655 column_index: ColumnIndex,
656 ) -> Option<ByteIndex> {
657 let column_index = column_index.to_usize();
658 let line_span = self.line_range(line_index)?;
659 let line_src = self
660 .content
661 .get(line_span.start.to_usize()..line_span.end.to_usize())
662 .expect("invalid line boundaries: invalid utf-8");
663
664 let content = line_src
665 .strip_suffix("\r\n")
666 .or_else(|| line_src.strip_suffix('\n'))
667 .unwrap_or(line_src);
668
669 let byte_len = content
671 .char_indices()
672 .map(|(offset, _)| offset)
673 .chain(core::iter::once(content.len()))
674 .nth(column_index)?;
675
676 Some(line_span.start + ByteOffset(byte_len as i64))
677 }
678
679 pub fn location(&self, byte_index: ByteIndex) -> Option<FileLineCol> {
682 let line_index = self.line_index(byte_index);
683 let line_start_index = self.line_start(line_index)?;
684 let line_src = self.content.get(line_start_index.to_usize()..byte_index.to_usize())?;
685 let column_index = ColumnIndex::from(line_src.chars().count() as u32);
686 Some(FileLineCol {
687 uri: self.uri.clone(),
688 line: line_index.number(),
689 column: column_index.number(),
690 })
691 }
692
693 pub fn update(
700 &mut self,
701 text: String,
702 range: Option<Selection>,
703 version: i32,
704 ) -> Result<(), SourceContentUpdateError> {
705 match range {
706 Some(range) => {
707 let start = self
708 .line_column_to_offset(range.start.line, range.start.character)
709 .ok_or(SourceContentUpdateError::InvalidSelectionStart(range.start))?
710 .to_usize();
711 let end = self
712 .line_column_to_offset(range.end.line, range.end.character)
713 .ok_or(SourceContentUpdateError::InvalidSelectionEnd(range.end))?
714 .to_usize();
715 assert!(start <= end, "start of range must be less than end, got {start}..{end}",);
716 self.content.replace_range(start..end, &text);
717
718 let added_line_starts = compute_line_starts(&text, Some(start as u32));
719 let num_added = added_line_starts.len();
720 let splice_start = range.start.line.to_usize() + 1;
721 enum Deletion {
726 Empty,
727 Inclusive(usize), }
729 let deletion = if range.start.line == range.end.line {
730 Deletion::Empty
731 } else {
732 let mut end_line_for_splice = range.end.line.to_usize();
733 if !self.line_starts.is_empty() {
734 let max_idx = self.line_starts.len() - 1;
735 if end_line_for_splice > max_idx {
736 end_line_for_splice = max_idx;
737 }
738 }
739 if end_line_for_splice >= splice_start {
740 Deletion::Inclusive(end_line_for_splice)
741 } else {
742 Deletion::Empty
743 }
744 };
745
746 match deletion {
747 Deletion::Empty => {
748 self.line_starts.splice(splice_start..splice_start, added_line_starts);
749 },
750 Deletion::Inclusive(end_idx) => {
751 self.line_starts.splice(splice_start..=end_idx, added_line_starts);
752 },
753 }
754
755 let diff =
756 (text.len() as i32).saturating_sub_unsigned((end as u32) - (start as u32));
757 if diff != 0 {
758 for i in (splice_start + num_added)..self.line_starts.len() {
759 self.line_starts[i] =
760 ByteIndex(self.line_starts[i].to_u32().saturating_add_signed(diff));
761 }
762 }
763 },
764 None => {
765 self.line_starts = compute_line_starts(&text, None);
766 self.content = text;
767 },
768 }
769
770 self.version = version;
771
772 Ok(())
773 }
774}
775
776#[cfg(feature = "serde")]
777impl SourceContent {
778 fn deserialize_and_recompute_line_starts<'de, D>(deserializer: D) -> Result<Self, D::Error>
779 where
780 D: serde::Deserializer<'de>,
781 {
782 let mut content = SourceContent::deserialize(deserializer)?;
783 content.line_starts = compute_line_starts(&content.content, None);
784 Ok(content)
785 }
786}
787
788fn compute_line_starts(text: &str, text_offset: Option<u32>) -> Vec<ByteIndex> {
789 let bytes = text.as_bytes();
790 let initial_line_offset = match text_offset {
791 Some(_) => None,
792 None => Some(ByteIndex(0)),
793 };
794 let text_offset = text_offset.unwrap_or(0);
795 initial_line_offset
796 .into_iter()
797 .chain(
798 memchr::memchr_iter(b'\n', bytes)
799 .map(|offset| ByteIndex(text_offset + (offset + 1) as u32)),
800 )
801 .collect()
802}
803
804#[derive(
809 Default,
810 Debug,
811 Copy,
812 Clone,
813 PartialEq,
814 Eq,
815 PartialOrd,
816 Ord,
817 Hash,
818 zerocopy::FromBytes,
819 zerocopy::Immutable,
820 zerocopy::IntoBytes,
821 zerocopy::KnownLayout,
822)]
823#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
824#[cfg_attr(feature = "serde", serde(transparent))]
825#[cfg_attr(
826 all(feature = "arbitrary", test),
827 miden_test_serialization_macros::serialization_test
828)]
829pub struct ByteIndex(pub u32);
830
831impl Serializable for ByteIndex {
832 fn write_into<W: ByteWriter>(&self, target: &mut W) {
833 self.0.write_into(target);
834 }
835}
836
837impl Deserializable for ByteIndex {
838 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
839 u32::read_from(source).map(Self)
840 }
841}
842
843impl ByteIndex {
844 pub const fn new(index: u32) -> Self {
846 Self(index)
847 }
848
849 #[inline(always)]
851 pub const fn to_usize(self) -> usize {
852 self.0 as usize
853 }
854
855 #[inline(always)]
857 pub const fn to_u32(self) -> u32 {
858 self.0
859 }
860}
861
862impl core::ops::Add<ByteOffset> for ByteIndex {
863 type Output = ByteIndex;
864
865 fn add(self, rhs: ByteOffset) -> Self {
866 Self((self.0 as i64 + rhs.0) as u32)
867 }
868}
869
870impl core::ops::Add<u32> for ByteIndex {
871 type Output = ByteIndex;
872
873 fn add(self, rhs: u32) -> Self {
874 Self(self.0 + rhs)
875 }
876}
877
878impl core::ops::AddAssign<ByteOffset> for ByteIndex {
879 fn add_assign(&mut self, rhs: ByteOffset) {
880 *self = *self + rhs;
881 }
882}
883
884impl core::ops::AddAssign<u32> for ByteIndex {
885 fn add_assign(&mut self, rhs: u32) {
886 self.0 += rhs;
887 }
888}
889
890impl core::ops::Sub<ByteOffset> for ByteIndex {
891 type Output = ByteIndex;
892
893 fn sub(self, rhs: ByteOffset) -> Self {
894 Self((self.0 as i64 - rhs.0) as u32)
895 }
896}
897
898impl core::ops::Sub<u32> for ByteIndex {
899 type Output = ByteIndex;
900
901 fn sub(self, rhs: u32) -> Self {
902 Self(self.0 - rhs)
903 }
904}
905
906impl core::ops::SubAssign<ByteOffset> for ByteIndex {
907 fn sub_assign(&mut self, rhs: ByteOffset) {
908 *self = *self - rhs;
909 }
910}
911
912impl core::ops::SubAssign<u32> for ByteIndex {
913 fn sub_assign(&mut self, rhs: u32) {
914 self.0 -= rhs;
915 }
916}
917
918impl From<u32> for ByteIndex {
919 fn from(index: u32) -> Self {
920 Self(index)
921 }
922}
923
924impl From<ByteIndex> for u32 {
925 fn from(index: ByteIndex) -> Self {
926 index.0
927 }
928}
929
930impl fmt::Display for ByteIndex {
931 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
932 fmt::Display::fmt(&self.0, f)
933 }
934}
935
936#[cfg(feature = "arbitrary")]
937impl Arbitrary for ByteIndex {
938 type Parameters = ();
939 type Strategy = BoxedStrategy<Self>;
940
941 fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
942 any::<u32>().prop_map(Self).boxed()
943 }
944}
945
946#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
948pub struct ByteOffset(i64);
949
950impl ByteOffset {
951 pub fn from_char_len(c: char) -> ByteOffset {
953 Self(c.len_utf8() as i64)
954 }
955
956 pub fn from_str_len(s: &str) -> ByteOffset {
958 Self(s.len() as i64)
959 }
960}
961
962impl core::ops::Add for ByteOffset {
963 type Output = ByteOffset;
964
965 fn add(self, rhs: Self) -> Self {
966 Self(self.0 + rhs.0)
967 }
968}
969
970impl core::ops::AddAssign for ByteOffset {
971 fn add_assign(&mut self, rhs: Self) {
972 self.0 += rhs.0;
973 }
974}
975
976impl core::ops::Sub for ByteOffset {
977 type Output = ByteOffset;
978
979 fn sub(self, rhs: Self) -> Self {
980 Self(self.0 - rhs.0)
981 }
982}
983
984impl core::ops::SubAssign for ByteOffset {
985 fn sub_assign(&mut self, rhs: Self) {
986 self.0 -= rhs.0;
987 }
988}
989
990impl fmt::Display for ByteOffset {
991 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
992 fmt::Display::fmt(&self.0, f)
993 }
994}
995
996macro_rules! declare_dual_number_and_index_type {
997 ($name:ident, $description:literal) => {
998 paste::paste! {
999 declare_dual_number_and_index_type!([<$name Index>], [<$name Number>], $description);
1000 }
1001 };
1002
1003 ($index_name:ident, $number_name:ident, $description:literal) => {
1004 #[doc = concat!("A zero-indexed ", $description, " number")]
1005 #[derive(
1006 Default,
1007 Debug,
1008 Copy,
1009 Clone,
1010 PartialEq,
1011 Eq,
1012 PartialOrd,
1013 Ord,
1014 Hash,
1015 zerocopy::FromBytes,
1016 zerocopy::Immutable,
1017 zerocopy::IntoBytes,
1018 zerocopy::KnownLayout,
1019 )]
1020 #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
1021 #[cfg_attr(feature = "serde", serde(transparent))]
1022 #[cfg_attr(
1023 all(feature = "arbitrary", test),
1024 miden_test_serialization_macros::serialization_test
1025 )]
1026 pub struct $index_name(pub u32);
1027
1028 impl Serializable for $index_name {
1029 fn write_into<W: ByteWriter>(&self, target: &mut W) {
1030 self.0.write_into(target);
1031 }
1032 }
1033
1034 impl Deserializable for $index_name {
1035 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
1036 u32::read_from(source).map(Self)
1037 }
1038 }
1039
1040 impl $index_name {
1041 #[doc = concat!("Convert to a [", stringify!($number_name), "]")]
1042 pub const fn number(self) -> $number_name {
1043 $number_name(unsafe { NonZeroU32::new_unchecked(self.0 + 1) })
1044 }
1045
1046 #[inline(always)]
1048 pub const fn to_usize(self) -> usize {
1049 self.0 as usize
1050 }
1051
1052 #[inline(always)]
1054 pub const fn to_u32(self) -> u32 {
1055 self.0
1056 }
1057
1058 pub fn checked_add(self, offset: u32) -> Option<Self> {
1060 self.0.checked_add(offset).map(Self)
1061 }
1062
1063 pub fn checked_add_signed(self, offset: i32) -> Option<Self> {
1065 self.0.checked_add_signed(offset).map(Self)
1066 }
1067
1068 pub fn checked_sub(self, offset: u32) -> Option<Self> {
1070 self.0.checked_sub(offset).map(Self)
1071 }
1072
1073 pub const fn saturating_add(self, offset: u32) -> Self {
1075 Self(self.0.saturating_add(offset))
1076 }
1077
1078 pub const fn saturating_add_signed(self, offset: i32) -> Self {
1081 Self(self.0.saturating_add_signed(offset))
1082 }
1083
1084 pub const fn saturating_sub(self, offset: u32) -> Self {
1086 Self(self.0.saturating_sub(offset))
1087 }
1088 }
1089
1090 impl From<u32> for $index_name {
1091 #[inline]
1092 fn from(index: u32) -> Self {
1093 Self(index)
1094 }
1095 }
1096
1097 impl From<$number_name> for $index_name {
1098 #[inline]
1099 fn from(index: $number_name) -> Self {
1100 Self(index.to_u32() - 1)
1101 }
1102 }
1103
1104 impl core::ops::Add<u32> for $index_name {
1105 type Output = Self;
1106
1107 #[inline]
1108 fn add(self, rhs: u32) -> Self {
1109 Self(self.0 + rhs)
1110 }
1111 }
1112
1113 impl core::ops::AddAssign<u32> for $index_name {
1114 fn add_assign(&mut self, rhs: u32) {
1115 let result = *self + rhs;
1116 *self = result;
1117 }
1118 }
1119
1120 impl core::ops::Add<i32> for $index_name {
1121 type Output = Self;
1122
1123 fn add(self, rhs: i32) -> Self {
1124 self.checked_add_signed(rhs).expect("invalid offset: overflow occurred")
1125 }
1126 }
1127
1128 impl core::ops::AddAssign<i32> for $index_name {
1129 fn add_assign(&mut self, rhs: i32) {
1130 let result = *self + rhs;
1131 *self = result;
1132 }
1133 }
1134
1135 impl core::ops::Sub<u32> for $index_name {
1136 type Output = Self;
1137
1138 #[inline]
1139 fn sub(self, rhs: u32) -> Self {
1140 Self(self.0 - rhs)
1141 }
1142 }
1143
1144 impl core::ops::SubAssign<u32> for $index_name {
1145 fn sub_assign(&mut self, rhs: u32) {
1146 let result = *self - rhs;
1147 *self = result;
1148 }
1149 }
1150
1151 impl fmt::Display for $index_name {
1152 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1153 fmt::Display::fmt(&self.0, f)
1154 }
1155 }
1156
1157 #[cfg(feature = "arbitrary")]
1158 impl Arbitrary for $index_name {
1159 type Parameters = ();
1160 type Strategy = BoxedStrategy<Self>;
1161
1162 fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
1163 any::<u32>().prop_map(Self).boxed()
1164 }
1165 }
1166
1167 #[doc = concat!("A one-indexed ", $description, " number")]
1168 #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1169 #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
1170 #[cfg_attr(feature = "serde", serde(transparent))]
1171 #[cfg_attr(
1172 all(feature = "arbitrary", test),
1173 miden_test_serialization_macros::serialization_test
1174 )]
1175 pub struct $number_name(NonZeroU32);
1176
1177 impl Default for $number_name {
1178 fn default() -> Self {
1179 Self(unsafe { NonZeroU32::new_unchecked(1) })
1180 }
1181 }
1182
1183 impl $number_name {
1184 pub const fn new(number: u32) -> Option<Self> {
1185 match NonZeroU32::new(number) {
1186 Some(num) => Some(Self(num)),
1187 None => None,
1188 }
1189 }
1190
1191 #[doc = concat!("Convert to a [", stringify!($index_name), "]")]
1192 pub const fn to_index(self) -> $index_name {
1193 $index_name(self.to_u32().saturating_sub(1))
1194 }
1195
1196 #[inline(always)]
1198 pub const fn to_usize(self) -> usize {
1199 self.0.get() as usize
1200 }
1201
1202 #[inline(always)]
1204 pub const fn to_u32(self) -> u32 {
1205 self.0.get()
1206 }
1207
1208 pub fn checked_add(self, offset: u32) -> Option<Self> {
1210 self.0.checked_add(offset).map(Self)
1211 }
1212
1213 pub fn checked_add_signed(self, offset: i32) -> Option<Self> {
1215 self.0.get().checked_add_signed(offset).and_then(Self::new)
1216 }
1217
1218 pub fn checked_sub(self, offset: u32) -> Option<Self> {
1220 self.0.get().checked_sub(offset).and_then(Self::new)
1221 }
1222
1223 pub const fn saturating_add(self, offset: u32) -> Self {
1225 Self(unsafe { NonZeroU32::new_unchecked(self.0.get().saturating_add(offset)) })
1226 }
1227
1228 pub fn saturating_add_signed(self, offset: i32) -> Self {
1231 Self::new(self.to_u32().saturating_add_signed(offset)).unwrap_or_default()
1232 }
1233
1234 pub fn saturating_sub(self, offset: u32) -> Self {
1236 Self::new(self.to_u32().saturating_sub(offset)).unwrap_or_default()
1237 }
1238 }
1239
1240 impl From<NonZeroU32> for $number_name {
1241 #[inline]
1242 fn from(index: NonZeroU32) -> Self {
1243 Self(index)
1244 }
1245 }
1246
1247 impl From<$index_name> for $number_name {
1248 #[inline]
1249 fn from(index: $index_name) -> Self {
1250 Self(unsafe { NonZeroU32::new_unchecked(index.to_u32() + 1) })
1251 }
1252 }
1253
1254 impl core::ops::Add<u32> for $number_name {
1255 type Output = Self;
1256
1257 #[inline]
1258 fn add(self, rhs: u32) -> Self {
1259 Self(unsafe { NonZeroU32::new_unchecked(self.0.get() + rhs) })
1260 }
1261 }
1262
1263 impl core::ops::AddAssign<u32> for $number_name {
1264 fn add_assign(&mut self, rhs: u32) {
1265 let result = *self + rhs;
1266 *self = result;
1267 }
1268 }
1269
1270 impl core::ops::Add<i32> for $number_name {
1271 type Output = Self;
1272
1273 fn add(self, rhs: i32) -> Self {
1274 self.to_u32()
1275 .checked_add_signed(rhs)
1276 .and_then(Self::new)
1277 .expect("invalid offset: overflow occurred")
1278 }
1279 }
1280
1281 impl core::ops::AddAssign<i32> for $number_name {
1282 fn add_assign(&mut self, rhs: i32) {
1283 let result = *self + rhs;
1284 *self = result;
1285 }
1286 }
1287
1288 impl core::ops::Sub<u32> for $number_name {
1289 type Output = Self;
1290
1291 #[inline]
1292 fn sub(self, rhs: u32) -> Self {
1293 self.to_u32()
1294 .checked_sub(rhs)
1295 .and_then(Self::new)
1296 .expect("invalid offset: overflow occurred")
1297 }
1298 }
1299
1300 impl core::ops::SubAssign<u32> for $number_name {
1301 fn sub_assign(&mut self, rhs: u32) {
1302 let result = *self - rhs;
1303 *self = result;
1304 }
1305 }
1306
1307 impl fmt::Display for $number_name {
1308 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1309 fmt::Display::fmt(&self.0, f)
1310 }
1311 }
1312 };
1313}
1314
1315declare_dual_number_and_index_type!(Line, "line");
1316declare_dual_number_and_index_type!(Column, "column");
1317
1318impl Serializable for LineNumber {
1322 fn write_into<W: ByteWriter>(&self, target: &mut W) {
1323 target.write_u32(self.to_u32());
1324 }
1325}
1326
1327impl Deserializable for LineNumber {
1328 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
1329 let value = source.read_u32()?;
1330 Self::new(value)
1331 .ok_or_else(|| DeserializationError::InvalidValue("line number cannot be zero".into()))
1332 }
1333}
1334
1335impl Serializable for ColumnNumber {
1336 fn write_into<W: ByteWriter>(&self, target: &mut W) {
1337 target.write_u32(self.to_u32());
1338 }
1339}
1340
1341impl Deserializable for ColumnNumber {
1342 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
1343 let value = source.read_u32()?;
1344 Self::new(value).ok_or_else(|| {
1345 DeserializationError::InvalidValue("column number cannot be zero".into())
1346 })
1347 }
1348}
1349
1350#[cfg(feature = "arbitrary")]
1351impl Arbitrary for LineNumber {
1352 type Parameters = ();
1353 type Strategy = BoxedStrategy<Self>;
1354
1355 fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
1356 (1..=u32::MAX)
1357 .prop_map(|value| Self::new(value).expect("non-zero value"))
1358 .boxed()
1359 }
1360}
1361
1362#[cfg(feature = "arbitrary")]
1363impl Arbitrary for ColumnNumber {
1364 type Parameters = ();
1365 type Strategy = BoxedStrategy<Self>;
1366
1367 fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
1368 (1..=u32::MAX)
1369 .prop_map(|value| Self::new(value).expect("non-zero value"))
1370 .boxed()
1371 }
1372}
1373
1374#[cfg(test)]
1375mod tests {
1376 use super::*;
1377
1378 #[test]
1379 fn source_content_line_starts() {
1380 const CONTENT: &str = "\
1381begin
1382 push.1
1383 push.2
1384 add
1385end
1386";
1387 let content = SourceContent::new("masm", "foo.masm", CONTENT);
1388
1389 assert_eq!(content.line_count(), 6);
1390 assert_eq!(
1391 content
1392 .byte_slice(content.line_range(LineIndex(0)).expect("invalid line"))
1393 .expect("invalid byte range"),
1394 "begin\n".as_bytes()
1395 );
1396 assert_eq!(
1397 content
1398 .byte_slice(content.line_range(LineIndex(1)).expect("invalid line"))
1399 .expect("invalid byte range"),
1400 " push.1\n".as_bytes()
1401 );
1402 assert_eq!(
1403 content
1404 .byte_slice(content.line_range(content.last_line_index()).expect("invalid line"))
1405 .expect("invalid byte range"),
1406 "".as_bytes()
1407 );
1408 }
1409
1410 #[test]
1411 fn source_content_line_starts_after_update() {
1412 const CONTENT: &str = "\
1413begin
1414 push.1
1415 push.2
1416 add
1417end
1418";
1419 const FRAGMENT: &str = " push.2
1420 mul
1421end
1422";
1423 let mut content = SourceContent::new("masm", "foo.masm", CONTENT);
1424 content
1425 .update(FRAGMENT.to_string(), Some(Selection::from(LineIndex(4)..LineIndex(5))), 1)
1426 .expect("update failed");
1427
1428 assert_eq!(
1429 content.as_str(),
1430 "\
1431begin
1432 push.1
1433 push.2
1434 add
1435 push.2
1436 mul
1437end
1438"
1439 );
1440 assert_eq!(content.line_count(), 8);
1441 assert_eq!(
1442 content
1443 .byte_slice(content.line_range(LineIndex(0)).expect("invalid line"))
1444 .expect("invalid byte range"),
1445 "begin\n".as_bytes()
1446 );
1447 assert_eq!(
1448 content
1449 .byte_slice(content.line_range(LineIndex(3)).expect("invalid line"))
1450 .expect("invalid byte range"),
1451 " add\n".as_bytes()
1452 );
1453 assert_eq!(
1454 content
1455 .byte_slice(content.line_range(LineIndex(4)).expect("invalid line"))
1456 .expect("invalid byte range"),
1457 " push.2\n".as_bytes()
1458 );
1459 assert_eq!(
1460 content
1461 .byte_slice(content.line_range(content.last_line_index()).expect("invalid line"))
1462 .expect("invalid byte range"),
1463 "".as_bytes()
1464 );
1465 }
1466
1467 #[test]
1469 fn source_content_line_starts_with_trailing_backslash() {
1470 const CONTENT: &str =
1471 "//! Build with:\n//! cargo build \\\n//! --release\nfn main() {}\n";
1472
1473 let content = SourceContent::new("rust", "example.rs", CONTENT);
1474
1475 assert_eq!(content.line_count(), 5);
1482
1483 assert_eq!(
1485 content
1486 .byte_slice(content.line_range(LineIndex(0)).expect("invalid line"))
1487 .expect("invalid byte range"),
1488 "//! Build with:\n".as_bytes()
1489 );
1490 assert_eq!(
1491 content
1492 .byte_slice(content.line_range(LineIndex(1)).expect("invalid line"))
1493 .expect("invalid byte range"),
1494 "//! cargo build \\\n".as_bytes()
1495 );
1496 assert_eq!(
1497 content
1498 .byte_slice(content.line_range(LineIndex(2)).expect("invalid line"))
1499 .expect("invalid byte range"),
1500 "//! --release\n".as_bytes()
1501 );
1502 assert_eq!(
1503 content
1504 .byte_slice(content.line_range(LineIndex(3)).expect("invalid line"))
1505 .expect("invalid byte range"),
1506 "fn main() {}\n".as_bytes()
1507 );
1508
1509 let offset_line0 = content.line_column_to_offset(LineIndex(0), ColumnIndex(0));
1512 let offset_line1 = content.line_column_to_offset(LineIndex(1), ColumnIndex(0));
1513 let offset_line2 = content.line_column_to_offset(LineIndex(2), ColumnIndex(0));
1514 let offset_line3 = content.line_column_to_offset(LineIndex(3), ColumnIndex(0));
1515
1516 assert!(offset_line0.is_some(), "line 0 should be accessible");
1517 assert!(offset_line1.is_some(), "line 1 should be accessible");
1518 assert!(offset_line2.is_some(), "line 2 should be accessible");
1519 assert!(offset_line3.is_some(), "line 3 should be accessible");
1520
1521 assert_eq!(offset_line0.unwrap().to_u32(), 0);
1523 assert_eq!(offset_line1.unwrap().to_u32(), 16); assert_eq!(offset_line2.unwrap().to_u32(), 36); assert_eq!(offset_line3.unwrap().to_u32(), 54); }
1527
1528 #[test]
1530 fn source_content_line_starts_multiple_trailing_backslashes() {
1531 const CONTENT: &str = "line1 \\\nline2 \\\nline3 \\\nline4\n";
1533
1534 let content = SourceContent::new("text", "test.txt", CONTENT);
1535
1536 assert_eq!(content.line_count(), 5);
1538
1539 assert_eq!(
1541 content
1542 .byte_slice(content.line_range(LineIndex(0)).expect("invalid line"))
1543 .expect("invalid byte range"),
1544 "line1 \\\n".as_bytes()
1545 );
1546 assert_eq!(
1547 content
1548 .byte_slice(content.line_range(LineIndex(1)).expect("invalid line"))
1549 .expect("invalid byte range"),
1550 "line2 \\\n".as_bytes()
1551 );
1552 assert_eq!(
1553 content
1554 .byte_slice(content.line_range(LineIndex(2)).expect("invalid line"))
1555 .expect("invalid byte range"),
1556 "line3 \\\n".as_bytes()
1557 );
1558 assert_eq!(
1559 content
1560 .byte_slice(content.line_range(LineIndex(3)).expect("invalid line"))
1561 .expect("invalid byte range"),
1562 "line4\n".as_bytes()
1563 );
1564 }
1565
1566 #[test]
1567 fn source_content_line_column_to_offset_multibyte_utf8() {
1568 const CONTENT: &str = "héllo\n";
1570 let content = SourceContent::new("text", "test.txt", CONTENT);
1571
1572 let expected = [(0, 0u32), (1, 1), (2, 3), (3, 4), (4, 5), (5, 6)];
1573 for (column, expected_byte) in expected {
1574 let offset = content
1575 .line_column_to_offset(LineIndex(0), ColumnIndex(column))
1576 .unwrap_or_else(|| panic!("column {column} should be in bounds"));
1577 assert_eq!(offset.to_u32(), expected_byte, "wrong byte offset for column {column}");
1578 }
1579
1580 for (column, _) in expected {
1581 let offset = content.line_column_to_offset(LineIndex(0), ColumnIndex(column)).unwrap();
1582 let loc = content.location(offset).unwrap();
1583 assert_eq!(
1584 ColumnIndex::from(loc.column).to_u32(),
1585 column,
1586 "round-trip mismatch at column {column}"
1587 );
1588 }
1589 }
1590
1591 #[test]
1592 fn source_content_line_column_to_offset_rejects_line_terminator() {
1593 let lf = SourceContent::new("text", "test.txt", "ab\ncd");
1594 assert!(lf.line_column_to_offset(LineIndex(0), ColumnIndex(2)).is_some());
1595 assert!(lf.line_column_to_offset(LineIndex(0), ColumnIndex(3)).is_none());
1596
1597 let crlf = SourceContent::new("text", "test.txt", "ab\r\ncd");
1598 assert!(crlf.line_column_to_offset(LineIndex(0), ColumnIndex(2)).is_some());
1599 assert!(crlf.line_column_to_offset(LineIndex(0), ColumnIndex(3)).is_none());
1600 assert!(crlf.line_column_to_offset(LineIndex(0), ColumnIndex(4)).is_none());
1601 }
1602
1603 #[test]
1604 fn source_content_line_column_to_offset_astral_character() {
1605 const CONTENT: &str = "\u{1F600}x";
1607 let content = SourceContent::new("text", "test.txt", CONTENT);
1608
1609 assert_eq!(
1610 content.line_column_to_offset(LineIndex(0), ColumnIndex(0)).unwrap().to_u32(),
1611 0
1612 );
1613 assert_eq!(
1614 content.line_column_to_offset(LineIndex(0), ColumnIndex(1)).unwrap().to_u32(),
1615 4
1616 );
1617 assert_eq!(
1618 content.line_column_to_offset(LineIndex(0), ColumnIndex(2)).unwrap().to_u32(),
1619 5
1620 );
1621 }
1622
1623 #[test]
1624 fn source_content_update_rejects_same_line_selection_spanning_line_terminator() {
1625 let mut content = SourceContent::new("text", "test.txt", "a\nb");
1626 assert_eq!(content.line_count(), 2);
1627
1628 let selection = Selection::new(Position::new(0, 1), Position::new(0, 2));
1629 let result = content.update(String::new(), Some(selection), 1);
1630
1631 assert!(
1632 result.is_err(),
1633 "expected the same-line selection spanning the line terminator to be rejected, got: \
1634 {result:?}"
1635 );
1636 assert_eq!(content.line_count(), 2);
1637 }
1638}