sqlx_macros_core/
common.rs

1use proc_macro2::Span;
2use std::env;
3use std::path::{Path, PathBuf};
4
5pub(crate) fn resolve_path(path: impl AsRef<Path>, err_span: Span) -> syn::Result<PathBuf> {
6    let path = path.as_ref();
7
8    if path.is_absolute() {
9        return Err(syn::Error::new(
10            err_span,
11            "absolute paths will only work on the current machine",
12        ));
13    }
14
15    // requires `proc_macro::SourceFile::path()` to be stable
16    // https://github.com/rust-lang/rust/issues/54725
17    if path.is_relative()
18        && !path
19            .parent()
20            .map_or(false, |parent| !parent.as_os_str().is_empty())
21    {
22        return Err(syn::Error::new(
23            err_span,
24            "paths relative to the current file's directory are not currently supported",
25        ));
26    }
27
28    let base_dir = env::var("CARGO_MANIFEST_DIR").map_err(|_| {
29        syn::Error::new(
30            err_span,
31            "CARGO_MANIFEST_DIR is not set; please use Cargo to build",
32        )
33    })?;
34    let base_dir_path = Path::new(&base_dir);
35
36    Ok(base_dir_path.join(path))
37}