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: FileIdA stable, unique identifier for the file, generated from its logical name. This ID persists across application runs and content modifications.
name: StringThe 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: StringThe contents of the file, if available.
size: usizeThe 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
impl File
Sourcepub fn new(
name: String,
file_type: FileType,
path: Option<PathBuf>,
contents: String,
) -> Self
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.
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: String, contents: String) -> Self
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.