#[cfg(test)]
mod test;
use std::path::{Path, PathBuf};
use cairo_lang_filesystem::db::Edition;
use cairo_lang_filesystem::ids::Directory;
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use serde::{Deserialize, Serialize};
use smol_str::SmolStr;
#[derive(thiserror::Error, Debug)]
pub enum DeserializationError {
#[error(transparent)]
TomlError(#[from] toml::de::Error),
#[error(transparent)]
IoError(#[from] std::io::Error),
#[error("PathError")]
PathError,
}
const PROJECT_FILE_NAME: &str = "cairo_project.toml";
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProjectConfig {
pub base_path: PathBuf,
pub corelib: Option<Directory>,
pub content: ProjectConfigContent,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProjectConfigContent {
pub crate_roots: OrderedHashMap<SmolStr, PathBuf>,
#[serde(default)]
#[serde(rename = "config")]
pub crates_config: AllCratesConfig,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct AllCratesConfig {
#[serde(default)]
pub global: SingleCrateConfig,
#[serde(default)]
#[serde(rename = "override")]
pub override_map: OrderedHashMap<SmolStr, SingleCrateConfig>,
}
impl AllCratesConfig {
pub fn get(&self, crate_name: &str) -> &SingleCrateConfig {
self.override_map.get(crate_name).unwrap_or(&self.global)
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct SingleCrateConfig {
pub edition: Edition,
}
impl ProjectConfig {
pub fn from_directory(directory: &Path) -> Result<Self, DeserializationError> {
Self::from_file(&directory.join(PROJECT_FILE_NAME))
}
pub fn from_file(filename: &Path) -> Result<Self, DeserializationError> {
let base_path = filename
.parent()
.and_then(|p| p.to_str())
.ok_or(DeserializationError::PathError)?
.into();
let content = toml::from_str(&std::fs::read_to_string(filename)?)?;
Ok(ProjectConfig { base_path, content, corelib: None })
}
}