1#![warn(missing_docs)]
2
3use nargo_types::Result;
4use serde::{Deserialize, Serialize};
5use std::path::PathBuf;
6
7pub const DEFAULT_CONFIG_FILE: &str = "Nargo.toml";
9
10pub const CONFIG_EXTENSIONS: &[&str] = &[".toml"];
12
13#[derive(Debug, Clone, Serialize, Deserialize, Default)]
17pub struct NargoConfig {
18 pub name: Option<String>,
20
21 pub version: Option<String>,
23
24 pub build: Option<BuildConfig>,
26
27 pub dev: Option<DevConfig>,
29
30 pub plugins: Option<Vec<PluginConfig>>,
32
33 pub aliases: Option<std::collections::HashMap<String, String>>,
35
36 pub formatter: Option<serde_json::Value>,
38
39 pub lint: Option<serde_json::Value>,
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize, Default)]
45pub struct BuildConfig {
46 pub out_dir: Option<PathBuf>,
48
49 pub prod: Option<bool>,
51
52 pub sourcemap: Option<bool>,
54
55 pub browserslist: Option<Vec<String>>,
57
58 pub targets: Option<Vec<String>>,
60}
61
62#[derive(Debug, Clone, Serialize, Deserialize, Default)]
64pub struct DevConfig {
65 pub port: Option<u16>,
67
68 pub host: Option<String>,
70
71 pub hot: Option<bool>,
73
74 pub proxy: Option<std::collections::HashMap<String, String>>,
76}
77
78#[derive(Debug, Clone, Serialize, Deserialize, Default)]
80pub struct PluginConfig {
81 pub name: String,
83
84 pub options: Option<serde_json::Value>,
86}
87
88pub struct ConfigLoader {
92 pub config_path: PathBuf,
94}
95
96impl Default for ConfigLoader {
97 fn default() -> Self {
98 Self { config_path: PathBuf::from(DEFAULT_CONFIG_FILE) }
99 }
100}
101
102impl ConfigLoader {
103 pub fn new(config_path: PathBuf) -> Self {
113 Self { config_path }
114 }
115
116 pub fn load(&self) -> Result<NargoConfig> {
122 use std::fs;
123
124 let extension = self.config_path.extension().unwrap_or_default().to_string_lossy().to_lowercase();
125 match extension.as_str() {
126 "toml" => {
127 let content = fs::read_to_string(&self.config_path).map_err(nargo_types::Error::io_error)?;
128 let config: NargoConfig = toml::from_str(&content).map_err(|e| nargo_types::Error::external_error("config".to_string(), format!("Invalid TOML config: {}", e), nargo_types::Span::default()))?;
129 Ok(config)
130 }
131 "json" => {
132 let content = fs::read_to_string(&self.config_path).map_err(nargo_types::Error::io_error)?;
133 let config: NargoConfig = serde_json::from_str(&content).map_err(|e| nargo_types::Error::external_error("config".to_string(), format!("Invalid JSON config: {}", e), nargo_types::Span::default()))?;
134 Ok(config)
135 }
136 _ => Err(nargo_types::Error::external_error("config".to_string(), format!("Unsupported config file extension: {}", extension), nargo_types::Span::default())),
137 }
138 }
139
140 pub fn save(&self, config: &NargoConfig) -> Result<()> {
150 Ok(())
152 }
153
154 pub fn find_config_file(start_dir: &PathBuf) -> Option<PathBuf> {
166 let mut current_dir = start_dir.clone();
167
168 loop {
169 let config_path = current_dir.join(DEFAULT_CONFIG_FILE);
170 if config_path.exists() {
171 return Some(config_path);
172 }
173
174 if !current_dir.pop() {
175 break;
176 }
177 }
178
179 None
180 }
181}
182
183pub mod types;
184pub use types::Dependency;
185
186#[derive(Debug, Clone, Serialize, Deserialize, Default)]
190pub struct NargoToml {
191 pub package: crate::types::PackageConfig,
193 pub dependencies: std::collections::HashMap<String, crate::types::Dependency>,
195 pub dev_dependencies: std::collections::HashMap<String, crate::types::Dependency>,
197 pub build_dependencies: std::collections::HashMap<String, crate::types::Dependency>,
199 pub workspace: Option<crate::types::WorkspaceConfig>,
201 pub features: std::collections::HashMap<String, Vec<String>>,
203 pub profile: crate::types::ProfileConfig,
205 pub target: std::collections::HashMap<String, crate::types::TargetConfig>,
207 pub scripts: std::collections::HashMap<String, crate::types::ScriptConfig>,
209 pub registries: std::collections::HashMap<String, crate::types::RegistryEntry>,
211 pub security: crate::types::SecurityConfig,
213 pub lint: crate::types::LintConfig,
215 pub format: crate::types::FormatConfig,
217}
218
219impl NargoToml {
220 pub fn validate(&self) -> nargo_types::Result<()> {
222 Ok(())
224 }
225
226 pub fn get_dependency(&self, name: &str) -> Option<&crate::types::Dependency> {
228 self.dependencies.get(name)
229 }
230}
231
232pub mod prelude {
234 pub use crate::{types::*, BuildConfig, ConfigLoader, DevConfig, NargoConfig, NargoToml, PluginConfig, CONFIG_EXTENSIONS, DEFAULT_CONFIG_FILE};
235}