66 lines
1.7 KiB
Rust
66 lines
1.7 KiB
Rust
use std::path::Path;
|
|
use super::Definition;
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
pub struct ModulePath {
|
|
components: Vec<String>,
|
|
}
|
|
|
|
impl std::fmt::Display for ModulePath {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
f.write_fmt(format_args!("{}", self.components.join("::")))
|
|
}
|
|
}
|
|
|
|
impl From<&Path> for ModulePath {
|
|
fn from(path: &Path) -> Self {
|
|
let meta = std::fs::metadata(path).unwrap();
|
|
ModulePath {
|
|
components: path
|
|
.components()
|
|
.map(|component| match component {
|
|
std::path::Component::Normal(n) => {
|
|
if meta.is_file() {
|
|
n.to_str().unwrap().split(".").nth(0).unwrap().to_string()
|
|
} else if meta.is_dir() {
|
|
n.to_str().unwrap().to_string()
|
|
} else {
|
|
// XXX: symlinks?
|
|
unreachable!()
|
|
}
|
|
}
|
|
_ => unreachable!(),
|
|
})
|
|
.collect(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<&str> for ModulePath {
|
|
fn from(string: &str) -> Self {
|
|
ModulePath {
|
|
components: string.split("::").map(|c| c.to_string()).collect(),
|
|
}
|
|
}
|
|
}
|
|
|
|
type ImportPath = ModulePath;
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
pub struct Module {
|
|
pub file: Option<std::path::PathBuf>,
|
|
pub path: ModulePath,
|
|
pub definitions: Vec<Definition>,
|
|
pub imports: Vec<ImportPath>,
|
|
}
|
|
|
|
impl Module {
|
|
pub fn new(path: ModulePath) -> Self {
|
|
Module {
|
|
path,
|
|
file: None,
|
|
definitions: vec![],
|
|
imports: vec![],
|
|
}
|
|
}
|
|
}
|