1
1
use crate :: build:: packages;
2
2
use crate :: helpers:: deserialize:: * ;
3
- use anyhow:: Result ;
3
+ use anyhow:: { Result , bail } ;
4
4
use convert_case:: { Case , Casing } ;
5
5
use serde:: Deserialize ;
6
6
use std:: fs;
@@ -180,32 +180,10 @@ pub struct JsxSpecs {
180
180
/// We do not care about the internal structure because the gentype config is loaded by bsc.
181
181
pub type GenTypeConfig = serde_json:: Value ;
182
182
183
- /// Wrapper around dependencies to emit a warning when the bs- prefix is used
184
- #[ derive( Deserialize , Debug , Clone ) ]
185
- pub struct Dependencies {
186
- pub deprecation_warning : bool ,
187
- pub value : Option < Vec < String > > ,
188
- }
189
-
190
- impl Default for Dependencies {
191
- fn default ( ) -> Self {
192
- Self {
193
- deprecation_warning : false ,
194
- value : None ,
195
- }
196
- }
197
- }
198
-
199
- impl From < Dependencies > for Option < Vec < String > > {
200
- fn from ( dependencies : Dependencies ) -> Self {
201
- dependencies. value
202
- }
203
- }
204
-
205
- #[ derive( Deserialize , Debug , Clone ) ]
206
- pub struct DependencyInfo {
207
- pub dependencies : Dependencies ,
208
- pub dev_dependencies : Dependencies ,
183
+ #[ derive( Debug , Clone , Copy , PartialEq , PartialOrd ) ]
184
+ pub enum DeprecationWarning {
185
+ BsDependencies ,
186
+ BsDevDependencies ,
209
187
}
210
188
211
189
/// # bsconfig.json representation
@@ -222,9 +200,18 @@ pub struct Config {
222
200
pub suffix : Option < String > ,
223
201
#[ serde( rename = "pinned-dependencies" ) ]
224
202
pub pinned_dependencies : Option < Vec < String > > ,
225
-
226
- #[ serde( flatten, deserialize_with = "deserialize_dependencies" ) ]
227
- pub dependency_info : DependencyInfo ,
203
+ pub dependencies : Option < Vec < String > > ,
204
+ #[ serde( rename = "dev-dependencies" ) ]
205
+ pub dev_dependencies : Option < Vec < String > > ,
206
+ // Deprecated field: overwrites dependencies
207
+ #[ serde( rename = "bs-dependencies" ) ]
208
+ bs_dependencies : Option < Vec < String > > ,
209
+ // Deprecated field: overwrites dev_dependencies
210
+ #[ serde( rename = "bs-dev-dependencies" ) ]
211
+ bs_dev_dependencies : Option < Vec < String > > ,
212
+ // Holds all deprecation warnings for the config struct
213
+ #[ serde( skip) ]
214
+ deprecation_warnings : Vec < DeprecationWarning > ,
228
215
229
216
#[ serde( rename = "ppx-flags" ) ]
230
217
pub ppx_flags : Option < Vec < OneOrMore < String > > > ,
@@ -349,9 +336,16 @@ impl Config {
349
336
/// Try to convert a bsconfig from a certain path to a bsconfig struct
350
337
pub fn new ( path : & Path ) -> Result < Self > {
351
338
let read = fs:: read_to_string ( path) ?;
352
- let parse = serde_json:: from_str :: < Config > ( & read) ?;
339
+ Config :: new_from_json_string ( & read)
340
+ }
341
+
342
+ /// Try to convert a bsconfig from a string to a bsconfig struct
343
+ pub fn new_from_json_string ( config_str : & str ) -> Result < Self > {
344
+ let mut config = serde_json:: from_str :: < Config > ( config_str) ?;
353
345
354
- Ok ( parse)
346
+ config. handle_deprecations ( ) ?;
347
+
348
+ Ok ( config)
355
349
}
356
350
357
351
pub fn get_namespace ( & self ) -> packages:: Namespace {
@@ -389,6 +383,7 @@ impl Config {
389
383
} ,
390
384
}
391
385
}
386
+
392
387
pub fn get_jsx_args ( & self ) -> Vec < String > {
393
388
match self . jsx . to_owned ( ) {
394
389
Some ( jsx) => match jsx. version {
@@ -498,12 +493,70 @@ impl Config {
498
493
. or ( self . suffix . clone ( ) )
499
494
. unwrap_or ( ".js" . to_string ( ) )
500
495
}
496
+
497
+ pub fn get_deprecations ( & self ) -> & [ DeprecationWarning ] {
498
+ & self . deprecation_warnings
499
+ }
500
+
501
+ fn handle_deprecations ( & mut self ) -> Result < ( ) > {
502
+ if self . dependencies . is_some ( ) && self . bs_dependencies . is_some ( ) {
503
+ bail ! ( "dependencies and bs-dependencies are mutually exclusive. Please use 'dependencies'." ) ;
504
+ }
505
+ if self . dev_dependencies . is_some ( ) && self . bs_dev_dependencies . is_some ( ) {
506
+ bail ! (
507
+ "dev-dependencies and bs-dev-dependencies are mutually exclusive. Please use 'dev-dependencies'"
508
+ ) ;
509
+ }
510
+
511
+ if self . bs_dependencies . is_some ( ) {
512
+ self . dependencies = self . bs_dependencies . take ( ) ;
513
+ self . deprecation_warnings . push ( DeprecationWarning :: BsDependencies ) ;
514
+ }
515
+ if self . bs_dev_dependencies . is_some ( ) {
516
+ self . dev_dependencies = self . bs_dev_dependencies . take ( ) ;
517
+ self . deprecation_warnings
518
+ . push ( DeprecationWarning :: BsDevDependencies ) ;
519
+ }
520
+
521
+ Ok ( ( ) )
522
+ }
501
523
}
502
524
503
525
#[ cfg( test) ]
504
- mod tests {
526
+ pub mod tests {
505
527
use super :: * ;
506
528
529
+ pub fn create_config (
530
+ name : String ,
531
+ bs_deps : Vec < String > ,
532
+ pinned_deps : Vec < String > ,
533
+ build_dev_deps : Vec < String > ,
534
+ allowed_dependents : Option < Vec < String > > ,
535
+ ) -> Config {
536
+ Config {
537
+ name : name. clone ( ) ,
538
+ sources : Some ( crate :: config:: OneOrMore :: Single ( Source :: Shorthand ( String :: from (
539
+ "Source" ,
540
+ ) ) ) ) ,
541
+ package_specs : None ,
542
+ warnings : None ,
543
+ suffix : None ,
544
+ pinned_dependencies : Some ( pinned_deps) ,
545
+ dependencies : Some ( bs_deps) ,
546
+ bs_dependencies : None ,
547
+ dev_dependencies : Some ( build_dev_deps) ,
548
+ bs_dev_dependencies : None ,
549
+ ppx_flags : None ,
550
+ bsc_flags : None ,
551
+ namespace : None ,
552
+ jsx : None ,
553
+ gentype_config : None ,
554
+ namespace_entry : None ,
555
+ deprecation_warnings : vec ! [ ] ,
556
+ allowed_dependents,
557
+ }
558
+ }
559
+
507
560
#[ test]
508
561
fn test_getters ( ) {
509
562
let json = r#"
@@ -718,12 +771,9 @@ mod tests {
718
771
}
719
772
"# ;
720
773
721
- let config = serde_json:: from_str :: < Config > ( json) . unwrap ( ) ;
722
- assert_eq ! (
723
- config. dependency_info. dependencies. value,
724
- Some ( vec![ "@testrepo/main" . to_string( ) ] )
725
- ) ;
726
- assert_eq ! ( config. dependency_info. dependencies. deprecation_warning, true )
774
+ let config = Config :: new_from_json_string ( json) . expect ( "a valid json string" ) ;
775
+ assert_eq ! ( config. dependencies, Some ( vec![ "@testrepo/main" . to_string( ) ] ) ) ;
776
+ assert_eq ! ( config. get_deprecations( ) , [ DeprecationWarning :: BsDependencies ] ) ;
727
777
}
728
778
729
779
#[ test]
@@ -746,12 +796,9 @@ mod tests {
746
796
}
747
797
"# ;
748
798
749
- let config = serde_json:: from_str :: < Config > ( json) . unwrap ( ) ;
750
- assert_eq ! (
751
- config. dependency_info. dependencies. value,
752
- Some ( vec![ "@testrepo/main" . to_string( ) ] )
753
- ) ;
754
- assert_eq ! ( config. dependency_info. dependencies. deprecation_warning, false ) ;
799
+ let config = Config :: new_from_json_string ( json) . expect ( "a valid json string" ) ;
800
+ assert_eq ! ( config. dependencies, Some ( vec![ "@testrepo/main" . to_string( ) ] ) ) ;
801
+ assert_eq ! ( config. get_deprecations( ) . is_empty( ) , true ) ;
755
802
}
756
803
757
804
#[ test]
@@ -774,12 +821,9 @@ mod tests {
774
821
}
775
822
"# ;
776
823
777
- let config = serde_json:: from_str :: < Config > ( json) . unwrap ( ) ;
778
- assert_eq ! (
779
- config. dependency_info. dev_dependencies. value,
780
- Some ( vec![ "@testrepo/main" . to_string( ) ] )
781
- ) ;
782
- assert_eq ! ( config. dependency_info. dev_dependencies. deprecation_warning, true ) ;
824
+ let config = Config :: new_from_json_string ( json) . expect ( "a valid json string" ) ;
825
+ assert_eq ! ( config. dev_dependencies, Some ( vec![ "@testrepo/main" . to_string( ) ] ) ) ;
826
+ assert_eq ! ( config. get_deprecations( ) , [ DeprecationWarning :: BsDevDependencies ] ) ;
783
827
}
784
828
785
829
#[ test]
@@ -802,11 +846,8 @@ mod tests {
802
846
}
803
847
"# ;
804
848
805
- let config = serde_json:: from_str :: < Config > ( json) . unwrap ( ) ;
806
- assert_eq ! (
807
- config. dependency_info. dev_dependencies. value,
808
- Some ( vec![ "@testrepo/main" . to_string( ) ] )
809
- ) ;
810
- assert_eq ! ( config. dependency_info. dev_dependencies. deprecation_warning, false ) ;
849
+ let config = Config :: new_from_json_string ( json) . expect ( "a valid json string" ) ;
850
+ assert_eq ! ( config. dev_dependencies, Some ( vec![ "@testrepo/main" . to_string( ) ] ) ) ;
851
+ assert_eq ! ( config. get_deprecations( ) . is_empty( ) , true ) ;
811
852
}
812
853
}
0 commit comments