|
| 1 | +//! A reimplementation of `codespan-reporting::files::SimpleFiles` that uses |
| 2 | +//! `FileId` as the file id, instead of `usize`. |
| 3 | +
|
| 4 | +use codespan_reporting::files::{Error, SimpleFile}; |
| 5 | +use std::{num::NonZeroU32, ops::Range}; |
| 6 | + |
| 7 | +/// File id. |
| 8 | +// 4 billion files should be enough for anyone, and `u16` doesn't save any size in `ByteRange` or `Span`. |
| 9 | +// `NonZeroU32` saves 4 bytes on the size of `Span` compared to `u32`. |
| 10 | +#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 11 | +pub struct FileId(NonZeroU32); |
| 12 | + |
| 13 | +impl TryFrom<u32> for FileId { |
| 14 | + type Error = <NonZeroU32 as TryFrom<u32>>::Error; |
| 15 | + |
| 16 | + fn try_from(value: u32) -> Result<Self, Self::Error> { |
| 17 | + let id = NonZeroU32::try_from(value)?; |
| 18 | + Ok(Self(id)) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl From<FileId> for NonZeroU32 { |
| 23 | + fn from(value: FileId) -> Self { |
| 24 | + value.0 |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +impl From<FileId> for u32 { |
| 29 | + fn from(value: FileId) -> Self { |
| 30 | + value.0.get() |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl From<FileId> for usize { |
| 35 | + fn from(value: FileId) -> Self { |
| 36 | + value.0.get() as usize |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +pub struct Files<Name, Source> { |
| 41 | + files: Vec<SimpleFile<Name, Source>>, |
| 42 | +} |
| 43 | + |
| 44 | +impl<Name, Source> Files<Name, Source> |
| 45 | +where |
| 46 | + Name: std::fmt::Display, |
| 47 | + Source: AsRef<str>, |
| 48 | +{ |
| 49 | + /// Create a new files database. |
| 50 | + pub fn new() -> Files<Name, Source> { |
| 51 | + Files { files: Vec::new() } |
| 52 | + } |
| 53 | + |
| 54 | + /// Add a file to the database, returning the handle that can be used to |
| 55 | + /// refer to it again. |
| 56 | + pub fn add(&mut self, name: Name, source: Source) -> FileId { |
| 57 | + self.files.push(SimpleFile::new(name, source)); |
| 58 | + let len = u32::try_from(self.files.len()) |
| 59 | + .expect("Too many files (maximum amount of files is `u32::MAX`)"); |
| 60 | + FileId::try_from(len).unwrap() |
| 61 | + } |
| 62 | + |
| 63 | + /// Get the file corresponding to the given id. |
| 64 | + pub fn get(&self, file_id: FileId) -> Result<&SimpleFile<Name, Source>, Error> { |
| 65 | + let index = usize::from(file_id) - 1; |
| 66 | + self.files.get(index).ok_or(Error::FileMissing) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl<'a, Name, Source> codespan_reporting::files::Files<'a> for Files<Name, Source> |
| 71 | +where |
| 72 | + Name: 'a + std::fmt::Display + Clone, |
| 73 | + Source: 'a + AsRef<str>, |
| 74 | +{ |
| 75 | + type FileId = FileId; |
| 76 | + type Name = Name; |
| 77 | + type Source = &'a str; |
| 78 | + |
| 79 | + fn name(&self, file_id: FileId) -> Result<Name, Error> { |
| 80 | + Ok(self.get(file_id)?.name().clone()) |
| 81 | + } |
| 82 | + |
| 83 | + fn source(&self, file_id: FileId) -> Result<&str, Error> { |
| 84 | + Ok(self.get(file_id)?.source().as_ref()) |
| 85 | + } |
| 86 | + |
| 87 | + fn line_index(&self, file_id: FileId, byte_index: usize) -> Result<usize, Error> { |
| 88 | + self.get(file_id)?.line_index((), byte_index) |
| 89 | + } |
| 90 | + |
| 91 | + fn line_range(&self, file_id: FileId, line_index: usize) -> Result<Range<usize>, Error> { |
| 92 | + self.get(file_id)?.line_range((), line_index) |
| 93 | + } |
| 94 | +} |
0 commit comments