Struct File

Source
pub struct File {
    pub id: FileId,
    pub name: String,
    pub path: Option<PathBuf>,
    pub file_type: FileType,
    pub contents: String,
    pub size: usize,
    pub lines: Vec<usize>,
}
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: FileId

A stable, unique identifier for the file, generated from its logical name. This ID persists across application runs and content modifications.

§name: String

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: FileType

The type of the file, indicating its origin.

§contents: String

The contents of the file, if available.

§size: usize

The size of the file’s contents in bytes.

§lines: Vec<usize>

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

Source

pub fn new( name: String, file_type: FileType, path: Option<PathBuf>, contents: String, ) -> Self

Creates a new File instance from its name, type, path, and contents.

It automatically calculates the size, and line start offsets.

Source

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: The FileType to assign to the created file.
§Errors

Returns a DatabaseError::IOError if the file cannot be read from the disk.

Source

pub fn ephemeral(name: String, contents: String) -> 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.

Source

pub fn line_number(&self, offset: usize) -> usize

Retrieve the line number for the given byte offset.

§Parameters
  • offset: The byte offset to retrieve the line number for.
§Returns

The line number for the given byte offset (0-based index).

Source

pub fn get_line_start_offset(&self, line: usize) -> Option<usize>

Retrieve the byte offset for the start of the given line.

§Parameters
  • line: The line number to retrieve the start offset for.
§Returns

The byte offset for the start of the given line (0-based index).

Source

pub fn get_line_end_offset(&self, line: usize) -> Option<usize>

Retrieve the byte offset for the end of the given line.

§Parameters
  • line: The line number to retrieve the end offset for.
§Returns

The byte offset for the end of the given line (0-based index).

Source

pub fn column_number(&self, offset: usize) -> usize

Retrieve the column number for the given byte offset.

§Parameters
  • offset: The byte offset to retrieve the column number for.
§Returns

The column number for the given byte offset (0-based index).

Trait Implementations§

Source§

impl Clone for File

Source§

fn clone(&self) -> File

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for File

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl HasFileId for File

Source§

fn file_id(&self) -> FileId

Returns the unique identifier of the file.
Source§

impl Hash for File

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for File

Source§

fn eq(&self, other: &File) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for File

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more