lila/src/ast/module.rs
Romain Paquet 99434748fa restructure parsing and typing modules
* parsing backend submodules
* move typing to its own module
2023-07-05 16:15:05 +02:00

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![],
}
}
}