ligen_core/generator/context/
source_file.rs

1//! SourceFile definition module.
2
3use serde::{Deserialize, Serialize};
4use std::path::PathBuf;
5
6#[cfg(cargo_ligen)]
7use crate::proc_macro;
8
9#[derive(Debug, Default, Serialize, Deserialize)]
10/// SourceFile struct.
11pub struct SourceFile {
12    is_real: bool,
13    path: PathBuf,
14}
15
16impl SourceFile {
17    #[cfg(cargo_ligen)]
18    /// Gets the current source file where the proc-macro is running.
19    pub fn current() -> Self {
20        proc_macro::Span::call_site().source_file().into()
21    }
22
23    /// If it's a real file.
24    pub fn is_real(&self) -> bool { self.is_real }
25
26    /// The source file path.
27    pub fn path(&self) -> PathBuf { self.path.clone() }
28}
29
30#[cfg(cargo_ligen)]
31impl From<proc_macro::SourceFile> for SourceFile {
32    fn from(source_file: proc_macro::SourceFile) -> Self {
33        let path = source_file.path();
34        let is_real = source_file.is_real();
35        Self { path, is_real }
36    }
37}