Skip to main content

WorkspaceFilePath

Struct WorkspaceFilePath 

Source
pub struct WorkspaceFilePath { /* private fields */ }
Expand description

Normalized relative path from workspace root (self-contained)

§Design

This type holds the relative path, workspace root, and crate name, making it completely self-contained. You can get the absolute path and derive symbol paths without any external context or provider.

  • relative: Path from workspace_root (used for Hash/Eq/Serialize)
  • workspace_root: Shared workspace root (Arc for lightweight sharing)
  • crate_name: The crate this file belongs to (required)

§Creation

Always create via WorkspacePathResolver. Direct construction with new_unchecked() is for internal/test use only.

let resolver = WorkspacePathResolver::new("/path/to/workspace".into());
let path = resolver.resolve("src/lib.rs")?;

Implementations§

Source§

impl WorkspaceFilePath

Source

pub fn as_relative(&self) -> &Path

Get the relative path

Source

pub fn workspace_root(&self) -> &Path

Get the workspace root

Source

pub fn crate_name(&self) -> &CrateName

Get the crate name

Source

pub fn to_absolute(&self) -> PathBuf

Get absolute path (no I/O, self-contained)

Source

pub fn canonicalize(&self) -> Result<PathBuf>

Get canonicalized absolute path (with I/O)

Source

pub fn file_name(&self) -> Option<&OsStr>

Get the file name

Source

pub fn extension(&self) -> Option<&OsStr>

Get the file extension

Source

pub fn parent(&self) -> Option<&Path>

Get the parent directory

Source

pub fn is_rust_file(&self) -> bool

Check if this is a Rust source file

Source

pub fn is_binary_entry(&self) -> bool

Check if this is a binary entry point (main.rs or src/bin/*.rs)

Binary entry points are handled separately from library code because:

  • main.rs and lib.rs both map to the crate root in module path terms
  • They represent different logical crates (binary vs library)
  • Storing them together causes data overwrite issues
§Returns

true if this file is:

  • src/main.rs or */src/main.rs
  • src/bin/*.rs or */src/bin/*.rs
Source

pub fn with_context( &self, workspace_root: Arc<Path>, crate_name: CrateName, ) -> Self

Create a new WorkspaceFilePath with different context

This is useful after deserialization when both workspace_root and crate_name need to be set or updated.

Source

pub fn with_relative(&self, relative: impl Into<PathBuf>) -> Self

Create a new WorkspaceFilePath with different relative path

This is useful for creating sibling files (e.g., creating src/storage.rs when you have src/lib.rs). The workspace_root and crate_name are inherited from the original path.

§Example
let lib_rs = resolver.resolve("src/lib.rs")?;
let storage_rs = lib_rs.with_relative("src/storage.rs");
Source

pub fn sibling(&self, file_name: &str) -> Self

Create a sibling file in the same directory

This is useful for creating module files (e.g., creating src/storage.rs when you have src/lib.rs).

§Example
let lib_rs = resolver.resolve("src/lib.rs")?;
let storage_rs = lib_rs.sibling("storage.rs");
assert_eq!(storage_rs.as_relative(), Path::new("src/storage.rs"));
Source

pub fn write(&self, content: impl AsRef<[u8]>) -> Result<()>

Write content to this file, creating parent directories if needed

This is the preferred way to write files in a workspace context. It automatically creates any missing parent directories before writing.

§Example
let path = resolver.resolve("src/new_module/lib.rs")?;
path.write("// New module\n")?;  // Creates src/new_module/ if needed
Source

pub fn read(&self) -> Result<String>

Read content from this file

Source

pub fn read_bytes(&self) -> Result<Vec<u8>>

Read content as bytes from this file

Source

pub fn exists(&self) -> bool

Check if the file exists

Trait Implementations§

Source§

impl AsRef<Path> for WorkspaceFilePath

Source§

fn as_ref(&self) -> &Path

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for WorkspaceFilePath

Source§

fn clone(&self) -> WorkspaceFilePath

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for WorkspaceFilePath

Source§

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

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

impl Display for WorkspaceFilePath

Source§

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

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

impl Hash for WorkspaceFilePath

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 WorkspaceFilePath

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 WorkspaceFilePath

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 WorkspaceFilePath

Auto Trait Implementations§

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, 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> ToCompactString for T
where T: Display,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.