1
- use std:: ops:: Range ;
1
+ use std:: { borrow :: Cow , collections :: HashMap , ops:: Range } ;
2
2
3
3
use codespan_reporting:: {
4
4
diagnostic:: { Diagnostic , Label } ,
@@ -8,12 +8,19 @@ use codespan_reporting::{
8
8
use thiserror:: Error ;
9
9
use tracing:: trace;
10
10
11
- use super :: Composer ;
11
+ use super :: {
12
+ preprocess:: { PreprocessOutput , PreprocessorMetaData } ,
13
+ Composer , ShaderDefValue ,
14
+ } ;
12
15
use crate :: { compose:: SPAN_SHIFT , redirect:: RedirectError } ;
13
16
14
17
#[ derive( Debug ) ]
15
18
pub enum ErrSource {
16
- Module ( String , usize ) ,
19
+ Module {
20
+ name : String ,
21
+ offset : usize ,
22
+ defs : HashMap < String , ShaderDefValue > ,
23
+ } ,
17
24
Constructing {
18
25
path : String ,
19
26
source : String ,
@@ -24,21 +31,38 @@ pub enum ErrSource {
24
31
impl ErrSource {
25
32
pub fn path < ' a > ( & ' a self , composer : & ' a Composer ) -> & ' a String {
26
33
match self {
27
- ErrSource :: Module ( c , _ ) => & composer. module_sets . get ( c ) . unwrap ( ) . file_path ,
34
+ ErrSource :: Module { name , .. } => & composer. module_sets . get ( name ) . unwrap ( ) . file_path ,
28
35
ErrSource :: Constructing { path, .. } => path,
29
36
}
30
37
}
31
38
32
- pub fn source < ' a > ( & ' a self , composer : & ' a Composer ) -> & ' a String {
39
+ pub fn source < ' a > ( & ' a self , composer : & ' a Composer ) -> Cow < ' a , String > {
33
40
match self {
34
- ErrSource :: Module ( c, _) => & composer. module_sets . get ( c) . unwrap ( ) . substituted_source ,
35
- ErrSource :: Constructing { source, .. } => source,
41
+ ErrSource :: Module { name, defs, .. } => {
42
+ let raw_source = & composer. module_sets . get ( name) . unwrap ( ) . sanitized_source ;
43
+ let Ok ( PreprocessOutput {
44
+ preprocessed_source : source,
45
+ meta : PreprocessorMetaData { imports, .. } ,
46
+ } ) = composer
47
+ . preprocessor
48
+ . preprocess ( raw_source, defs, composer. validate )
49
+ else {
50
+ return Default :: default ( )
51
+ } ;
52
+
53
+ let Ok ( source) = composer
54
+ . substitute_shader_string ( & source, & imports)
55
+ else { return Default :: default ( ) } ;
56
+
57
+ Cow :: Owned ( source)
58
+ }
59
+ ErrSource :: Constructing { source, .. } => Cow :: Borrowed ( source) ,
36
60
}
37
61
}
38
62
39
63
pub fn offset ( & self ) -> usize {
40
64
match self {
41
- ErrSource :: Module ( _ , offset) | ErrSource :: Constructing { offset, .. } => * offset,
65
+ ErrSource :: Module { offset, .. } | ErrSource :: Constructing { offset, .. } => * offset,
42
66
}
43
67
}
44
68
}
@@ -159,7 +183,7 @@ impl ComposerError {
159
183
..( ( rng. end & ( ( 1 << SPAN_SHIFT ) - 1 ) ) . saturating_sub ( source_offset) )
160
184
} ;
161
185
162
- let files = SimpleFile :: new ( path, source) ;
186
+ let files = SimpleFile :: new ( path, source. as_str ( ) ) ;
163
187
let config = term:: Config :: default ( ) ;
164
188
#[ cfg( test) ]
165
189
let mut writer = term:: termcolor:: NoColor :: new ( Vec :: new ( ) ) ;
0 commit comments