pub struct File {
pub id: FileId,
pub name: Cow<'static, str>,
pub path: Option<PathBuf>,
pub file_type: FileType,
pub contents: Cow<'static, str>,
pub size: u32,
pub lines: Vec<u32>,
}Expand description
A file that’s either stored on the host system’s file system or in the vendored file system.
This struct encapsulates all the necessary information about a file, including its content, location, and metadata for change detection.
Fields§
§id: FileIdA stable, unique identifier for the file, generated from its logical name. This ID persists across application runs and content modifications.
name: Cow<'static, str>The logical name of the file, typically the path relative to the root of the project.
path: Option<PathBuf>The absolute path of the file on the host’s filesystem, if it exists there.
This will be None for vendored files that don’t have a physical counterpart.
file_type: FileTypeThe type of the file, indicating its origin.
contents: Cow<'static, str>The contents of the file, if available.
size: u32The size of the file’s contents in bytes.
lines: Vec<u32>A vector containing the starting byte offsets of each line in contents.
The first line always starts at offset 0. This is useful for quickly
navigating to a specific line number without scanning the whole file.
Implementations§
Source§impl File
impl File
Sourcepub fn new(
name: Cow<'static, str>,
file_type: FileType,
path: Option<PathBuf>,
contents: Cow<'static, str>,
) -> Self
pub fn new( name: Cow<'static, str>, file_type: FileType, path: Option<PathBuf>, contents: Cow<'static, str>, ) -> Self
Creates a new File instance from its name, type, path, and contents.
It automatically calculates the size, and line start offsets.
Sourcepub fn read(
workspace: &Path,
path: &Path,
file_type: FileType,
) -> Result<Self, DatabaseError>
pub fn read( workspace: &Path, path: &Path, file_type: FileType, ) -> Result<Self, DatabaseError>
Creates a new File instance by reading its contents from the filesystem.
This is the primary factory function for creating a File from a disk source.
It handles determining the file’s logical name relative to the workspace,
reading its contents, and robustly handling non-UTF-8 text via lossy conversion.
§Arguments
workspace: The root directory of the project, used to calculate the logical name.path: The absolute path to the file to read from disk.file_type: TheFileTypeto assign to the created file.
§Errors
Returns a DatabaseError::IOError if the file cannot be read from the disk.
Sourcepub fn ephemeral(name: Cow<'static, str>, contents: Cow<'static, str>) -> Self
pub fn ephemeral(name: Cow<'static, str>, contents: Cow<'static, str>) -> Self
Creates an ephemeral, in-memory File from a name and content.
This is a convenience method for situations like testing or formatting where
a full file context (e.g., a real path) is not required. It defaults to
FileType::Host and a path of None.
Sourcepub fn line_number(&self, offset: u32) -> u32
pub fn line_number(&self, offset: u32) -> u32
Sourcepub fn get_line_start_offset(&self, line: u32) -> Option<u32>
pub fn get_line_start_offset(&self, line: u32) -> Option<u32>
Sourcepub fn get_line_end_offset(&self, line: u32) -> Option<u32>
pub fn get_line_end_offset(&self, line: u32) -> Option<u32>
Trait Implementations§
Source§impl<'de> Deserialize<'de> for File
impl<'de> Deserialize<'de> for File
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for File
impl StructuralPartialEq for File
Auto Trait Implementations§
impl Freeze for File
impl RefUnwindSafe for File
impl Send for File
impl Sync for File
impl Unpin for File
impl UnwindSafe for File
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more