Skip to content

Commit 3b5fc7b

Browse files
author
Grant Wuerker
committed
Common cleanup
1 parent 30b1534 commit 3b5fc7b

File tree

23 files changed

+145
-137
lines changed

23 files changed

+145
-137
lines changed

crates/common/src/config.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::{fmt::Display, str::FromStr};
1+
use std::fmt::Display;
22

33
use camino::Utf8PathBuf;
44
use smol_str::SmolStr;
55
use toml::Value;
66
use url::Url;
77

8-
use crate::{graph::EdgeWeight, ingot::Version, urlext::UrlExt};
8+
use crate::{dependencies::EdgeWeight, ingot::Version, urlext::UrlExt};
99

1010
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1111
pub struct Config {
@@ -15,12 +15,14 @@ pub struct Config {
1515
}
1616

1717
impl Config {
18-
pub fn parse(content: &str) -> Result<Self, <Value as FromStr>::Err> {
18+
pub fn parse(content: &str) -> Result<Self, String> {
1919
let mut diagnostics = Vec::new();
2020
let mut metadata = IngotMetadata::default();
2121
let mut dependencies = Vec::new();
2222

23-
let parsed: Value = content.parse()?;
23+
let parsed: Value = content
24+
.parse()
25+
.map_err(|e: toml::de::Error| e.to_string())?;
2426

2527
if let Some(table) = parsed.get("ingot").and_then(|value| value.as_table()) {
2628
if let Some(name) = table.get("name") {
@@ -208,7 +210,6 @@ pub enum ConfigDiagnostic {
208210
InvalidDependencyAlias(SmolStr),
209211
InvalidDependencyName(SmolStr),
210212
InvalidDependencyVersion(SmolStr),
211-
InvalidTomlSyntax(String),
212213
MissingDependencyPath {
213214
alias: SmolStr,
214215
description: String,
@@ -237,7 +238,6 @@ impl Display for ConfigDiagnostic {
237238
Self::InvalidDependencyVersion(version) => {
238239
write!(f, "Invalid dependency version \"{version}\"")
239240
}
240-
Self::InvalidTomlSyntax(err) => write!(f, "Invalid TOML syntax: {err}"),
241241
Self::MissingDependencyPath { alias, description } => write!(
242242
f,
243243
"The dependency \"{alias}\" is missing a path argument \"{description}\""

crates/common/src/tree.rs renamed to crates/common/src/dependencies/display_tree.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use petgraph::graph::{DiGraph, NodeIndex};
22
use std::collections::{HashMap, HashSet};
33
use url::Url;
44

5-
use crate::{config::Config, graph::EdgeWeight};
5+
use super::graph::Dependency;
6+
use crate::config::Config;
67

78
#[derive(Clone)]
89
enum TreePrefix {
@@ -29,8 +30,8 @@ impl TreePrefix {
2930
}
3031
}
3132

32-
pub fn display_dependency_tree(
33-
graph: &DiGraph<Url, EdgeWeight>,
33+
pub fn display_tree(
34+
graph: &DiGraph<Url, Dependency>,
3435
root_url: &Url,
3536
configs: &HashMap<Url, Config>,
3637
) -> String {
@@ -61,7 +62,7 @@ pub fn display_dependency_tree(
6162
}
6263

6364
struct TreeContext<'a> {
64-
graph: &'a DiGraph<Url, EdgeWeight>,
65+
graph: &'a DiGraph<Url, Dependency>,
6566
configs: &'a HashMap<Url, Config>,
6667
cycle_nodes: &'a HashSet<NodeIndex>,
6768
}

crates/common/src/graph.rs renamed to crates/common/src/dependencies/graph.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,28 @@ use crate::{InputDb, config::DependencyArguments};
99

1010
#[salsa::input]
1111
#[derive(Debug)]
12-
pub struct Graph {
13-
graph: DiGraph<Url, EdgeWeight>,
12+
pub struct DependencyGraph {
13+
graph: DiGraph<Url, Dependency>,
1414
node_map: HashMap<Url, NodeIndex>,
1515
}
1616

1717
#[derive(Debug, Clone)]
1818
pub struct JoinEdge {
1919
pub origin: Url,
2020
pub destination: Url,
21-
pub weight: EdgeWeight,
21+
pub weight: Dependency,
2222
}
2323

2424
#[derive(Clone, Debug)]
25-
pub struct EdgeWeight {
25+
pub struct Dependency {
2626
pub alias: SmolStr,
2727
pub arguments: DependencyArguments,
2828
}
2929

3030
#[salsa::tracked]
31-
impl Graph {
31+
impl DependencyGraph {
3232
pub fn default(db: &dyn InputDb) -> Self {
33-
Graph::new(db, DiGraph::new(), HashMap::new())
33+
DependencyGraph::new(db, DiGraph::new(), HashMap::new())
3434
}
3535

3636
pub fn contains_url(&self, db: &dyn InputDb, url: &Url) -> bool {
@@ -45,7 +45,7 @@ impl Graph {
4545
/// - All nodes that have paths leading to cyclic nodes
4646
///
4747
/// Returns an empty graph if no cycles are detected.
48-
pub fn cyclic_subgraph(&self, db: &dyn InputDb) -> DiGraph<Url, EdgeWeight> {
48+
pub fn cyclic_subgraph(&self, db: &dyn InputDb) -> DiGraph<Url, Dependency> {
4949
use petgraph::algo::tarjan_scc;
5050
use std::collections::VecDeque;
5151

@@ -119,7 +119,7 @@ impl Graph {
119119
pub fn join_graph(
120120
&self,
121121
db: &mut dyn InputDb,
122-
other: DiGraph<Url, EdgeWeight>,
122+
other: DiGraph<Url, Dependency>,
123123
join_edges: Vec<JoinEdge>,
124124
) {
125125
let current_graph = self.graph(db);
@@ -173,7 +173,7 @@ impl Graph {
173173
new_idx
174174
}
175175

176-
pub fn add_edge(&self, db: &mut dyn InputDb, from: &Url, to: &Url, weight: EdgeWeight) -> bool {
176+
pub fn add_edge(&self, db: &mut dyn InputDb, from: &Url, to: &Url, weight: Dependency) -> bool {
177177
let node_map = self.node_map(db);
178178

179179
if let (Some(&from_idx), Some(&to_idx)) = (node_map.get(from), node_map.get(to)) {
@@ -187,11 +187,11 @@ impl Graph {
187187
}
188188
}
189189

190-
fn join_graphs(
191-
graph1: &DiGraph<Url, EdgeWeight>,
192-
graph2: &DiGraph<Url, EdgeWeight>,
190+
pub fn join_graphs(
191+
graph1: &DiGraph<Url, Dependency>,
192+
graph2: &DiGraph<Url, Dependency>,
193193
join_edges: &[JoinEdge],
194-
) -> DiGraph<Url, EdgeWeight> {
194+
) -> DiGraph<Url, Dependency> {
195195
let mut combined = DiGraph::new();
196196
let mut node_map = HashMap::<Url, NodeIndex>::new();
197197

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pub mod display_tree;
2+
pub mod graph;
3+
4+
pub use display_tree::display_tree;
5+
pub use graph::{Dependency, DependencyGraph, JoinEdge, join_graphs};
6+
7+
// Re-export commonly used types for backward compatibility
8+
pub use display_tree::display_tree as display_dependency_tree;
9+
pub use graph::Dependency as EdgeWeight;
10+
pub use graph::DependencyGraph as Graph;

crates/common/src/ingot.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use smol_str::SmolStr;
66
use url::Url;
77

88
use crate::InputDb;
9-
use crate::config::{Config, ConfigDiagnostic};
9+
use crate::config::Config;
1010
use crate::core::BUILTIN_CORE_BASE_URL;
1111
use crate::file::{File, Workspace};
1212
use crate::urlext::UrlExt;
@@ -104,22 +104,25 @@ impl<'db> Ingot<'db> {
104104
}
105105
}
106106

107+
#[salsa::tracked]
108+
pub fn config_file(self, db: &'db dyn InputDb) -> Option<File> {
109+
db.workspace().containing_ingot_config(db, self.base(db))
110+
}
111+
112+
#[salsa::tracked]
113+
fn parse_config(self, db: &'db dyn InputDb) -> Option<Result<Config, String>> {
114+
self.config_file(db)
115+
.map(|config_file| Config::parse(config_file.text(db)))
116+
}
117+
107118
#[salsa::tracked]
108119
pub fn config(self, db: &'db dyn InputDb) -> Option<Config> {
109-
db.workspace()
110-
.containing_ingot_config(db, self.base(db))
111-
.map(|config_file| match Config::parse(config_file.text(db)) {
112-
Ok(config) => config,
113-
Err(err) => {
114-
// Create a Config with just the parse error as a diagnostic
115-
let diagnostics = vec![ConfigDiagnostic::InvalidTomlSyntax(err.to_string())];
116-
Config {
117-
metadata: Default::default(),
118-
diagnostics,
119-
dependencies: Vec::new(),
120-
}
121-
}
122-
})
120+
self.parse_config(db).and_then(|result| result.ok())
121+
}
122+
123+
#[salsa::tracked]
124+
pub fn config_parse_error(self, db: &'db dyn InputDb) -> Option<String> {
125+
self.parse_config(db).and_then(|result| result.err())
123126
}
124127

125128
#[salsa::tracked]

crates/common/src/lib.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
pub mod config;
22
pub mod core;
3+
pub mod dependencies;
34
pub mod diagnostics;
45
pub mod file;
5-
pub mod graph;
66
pub mod indexmap;
77
pub mod ingot;
8-
pub mod tree;
98
pub mod urlext;
109

10+
use dependencies::Graph;
1111
use file::Workspace;
12-
use graph::Graph;
1312

1413
#[salsa::db]
1514
// Each database must implement InputDb explicitly with its own storage mechanism
@@ -31,7 +30,7 @@ macro_rules! impl_input_db {
3130
fn workspace(&self) -> $crate::file::Workspace {
3231
self.index.clone().expect("Workspace not initialized")
3332
}
34-
fn graph(&self) -> $crate::graph::Graph {
33+
fn graph(&self) -> $crate::dependencies::Graph {
3534
self.graph.clone().expect("Graph not initialized")
3635
}
3736
}
@@ -57,7 +56,7 @@ macro_rules! impl_db_default {
5756
};
5857
let index = $crate::file::Workspace::default(&db);
5958
db.index = Some(index);
60-
let graph = $crate::graph::Graph::default(&db);
59+
let graph = $crate::dependencies::Graph::default(&db);
6160
db.graph = Some(graph);
6261
$crate::core::HasBuiltinCore::initialize_builtin_core(&mut db);
6362
db
@@ -75,7 +74,7 @@ macro_rules! define_input_db {
7574
pub struct $db_name {
7675
storage: salsa::Storage<Self>,
7776
index: Option<$crate::file::Workspace>,
78-
graph: Option<$crate::graph::Graph>,
77+
graph: Option<$crate::dependencies::Graph>,
7978
}
8079

8180
#[salsa::db]

0 commit comments

Comments
 (0)