@@ -9,20 +9,37 @@ import spark/lex
9
9
import spark/module . { type Module }
10
10
import spark/parse
11
11
import spark/util
12
+ import tom
12
13
13
14
// ---- TYPES ------------------------------------------------------------------
14
15
15
16
pub type Project {
16
- Project ( name : String , modules : List ( Module ) , extra_files : List ( String ) , dir : String )
17
+ Project (
18
+ config : Config ,
19
+ modules : List ( Module ) ,
20
+ extra_files : List ( String ) ,
21
+ dir : String ,
22
+ )
23
+ }
24
+
25
+ pub type Config {
26
+ Config ( name : String )
17
27
}
18
28
19
29
// ---- CONSTRUCTOR ------------------------------------------------------------
20
30
21
- pub fn from ( name : String , dir : String ) -> Result ( Project , String ) {
31
+ pub fn from ( dir : String ) -> Result ( Project , String ) {
32
+ use config_file <- try (
33
+ file . read ( filepath . join ( dir , "spark.toml" ) )
34
+ |> result . replace_error ( error . simple_error (
35
+ "`spark.toml` config file is missing" ,
36
+ ) ) ,
37
+ )
38
+ use config <- try ( parse_config ( config_file ) )
22
39
use # ( modules , extra_files ) <- try (
23
40
scan_project_files ( filepath . join ( dir , "src" ) ) ,
24
41
)
25
- Ok ( Project ( name , modules , extra_files , dir ) )
42
+ Ok ( Project ( config , modules , extra_files , dir ) )
26
43
}
27
44
28
45
fn scan_project_files (
@@ -40,6 +57,22 @@ fn scan_project_files(
40
57
|> Ok
41
58
}
42
59
60
+ fn parse_config ( config_file : String ) -> Result ( Config , String ) {
61
+ use parsed <- try (
62
+ tom . parse ( config_file )
63
+ |> result . replace_error ( error . simple_error (
64
+ "I was unable to parse the config file as valid TOML" ,
65
+ ) ) ,
66
+ )
67
+ use name <- try (
68
+ tom . get_string ( parsed , [ "name" ] )
69
+ |> result . replace_error ( error . simple_error (
70
+ "Config is missing the `name` field or it is not a string" ,
71
+ ) ) ,
72
+ )
73
+ Ok ( Config ( name ) )
74
+ }
75
+
43
76
// ---- FUNCTIONS --------------------------------------------------------------
44
77
45
78
/// Compile a project to the given build directory.
0 commit comments