File

Struct File 

Source
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: FileId

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

The type of the file, indicating its origin.

§contents: Cow<'static, str>

The contents of the file, if available.

§size: u32

The 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

Source

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.

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

Source

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

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: u32) -> Option<u32>

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: u32) -> Option<u32>

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: u32) -> u32

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 Debug for File

Source§

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

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

impl<'de> Deserialize<'de> for File

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. 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 Serialize for File

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,