11use crate :: build:: packages;
22use crate :: helpers:: deserialize:: * ;
3- use anyhow:: Result ;
3+ use anyhow:: { Result , bail } ;
44use convert_case:: { Case , Casing } ;
55use serde:: Deserialize ;
66use std:: fs;
@@ -180,32 +180,10 @@ pub struct JsxSpecs {
180180/// We do not care about the internal structure because the gentype config is loaded by bsc.
181181pub type GenTypeConfig = serde_json:: Value ;
182182
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 ,
209187}
210188
211189/// # bsconfig.json representation
@@ -222,9 +200,18 @@ pub struct Config {
222200 pub suffix : Option < String > ,
223201 #[ serde( rename = "pinned-dependencies" ) ]
224202 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 > ,
228215
229216 #[ serde( rename = "ppx-flags" ) ]
230217 pub ppx_flags : Option < Vec < OneOrMore < String > > > ,
@@ -349,9 +336,16 @@ impl Config {
349336 /// Try to convert a bsconfig from a certain path to a bsconfig struct
350337 pub fn new ( path : & Path ) -> Result < Self > {
351338 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) ?;
353345
354- Ok ( parse)
346+ config. handle_deprecations ( ) ?;
347+
348+ Ok ( config)
355349 }
356350
357351 pub fn get_namespace ( & self ) -> packages:: Namespace {
@@ -389,6 +383,7 @@ impl Config {
389383 } ,
390384 }
391385 }
386+
392387 pub fn get_jsx_args ( & self ) -> Vec < String > {
393388 match self . jsx . to_owned ( ) {
394389 Some ( jsx) => match jsx. version {
@@ -498,12 +493,70 @@ impl Config {
498493 . or ( self . suffix . clone ( ) )
499494 . unwrap_or ( ".js" . to_string ( ) )
500495 }
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+ }
501523}
502524
503525#[ cfg( test) ]
504- mod tests {
526+ pub mod tests {
505527 use super :: * ;
506528
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+
507560 #[ test]
508561 fn test_getters ( ) {
509562 let json = r#"
@@ -718,12 +771,9 @@ mod tests {
718771 }
719772 "# ;
720773
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 ] ) ;
727777 }
728778
729779 #[ test]
@@ -746,12 +796,9 @@ mod tests {
746796 }
747797 "# ;
748798
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 ) ;
755802 }
756803
757804 #[ test]
@@ -774,12 +821,9 @@ mod tests {
774821 }
775822 "# ;
776823
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 ] ) ;
783827 }
784828
785829 #[ test]
@@ -802,11 +846,8 @@ mod tests {
802846 }
803847 "# ;
804848
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 ) ;
811852 }
812853}
0 commit comments