@@ -48,7 +48,6 @@ use jwt::SignWithKey;
4848use oauth2:: {
4949 basic:: BasicTokenType , AccessToken , EmptyExtraTokenFields , Scope , StandardTokenResponse ,
5050} ;
51- use serde_json:: Value as JsonValue ;
5251use std:: {
5352 collections:: BTreeMap ,
5453 convert:: TryFrom ,
@@ -60,7 +59,7 @@ use std::{collections::HashMap, ffi::OsStr};
6059use tmc_langs_framework:: { TmcError , TmcProjectYml } ;
6160use tmc_langs_plugins:: { get_language_plugin, tmc_zip, AntPlugin , PluginType } ;
6261use tmc_langs_util:: progress_reporter;
63- use toml:: { map :: Map as TomlMap , Value as TomlValue } ;
62+ use toml:: Value as TomlValue ;
6463use url:: Url ;
6564use walkdir:: WalkDir ;
6665
@@ -675,19 +674,12 @@ pub fn get_settings(client_name: &str) -> Result<TmcConfig, LangsError> {
675674}
676675
677676/// Saves a setting in the config.
678- pub fn set_setting ( client_name : & str , key : & str , value : & str ) -> Result < ( ) , LangsError > {
679- log:: debug!( "setting {}={} in {}" , key, value , client_name) ;
677+ pub fn set_setting < T : Serialize > ( client_name : & str , key : & str , value : T ) -> Result < ( ) , LangsError > {
678+ log:: debug!( "setting {} in {}" , key, client_name) ;
680679
681680 let config_path = TmcConfig :: get_location ( client_name) ?;
682681 let mut tmc_config = TmcConfig :: load ( client_name, & config_path) ?;
683- let value = match serde_json:: from_str ( value) {
684- Ok ( json) => json,
685- Err ( _) => {
686- // interpret as string
687- JsonValue :: String ( value. to_string ( ) )
688- }
689- } ;
690- let value = setting_json_to_toml ( value) ?;
682+ let value = TomlValue :: try_from ( value) ?;
691683
692684 tmc_config. insert ( key. to_string ( ) , value) ?;
693685 tmc_config. save ( & config_path) ?;
@@ -714,38 +706,6 @@ pub fn unset_setting(client_name: &str, key: &str) -> Result<(), LangsError> {
714706 Ok ( ( ) )
715707}
716708
717- fn setting_json_to_toml ( json : JsonValue ) -> Result < TomlValue , LangsError > {
718- match json {
719- JsonValue :: Array ( arr) => {
720- let mut v = vec ! [ ] ;
721- for value in arr {
722- v. push ( setting_json_to_toml ( value) ?) ;
723- }
724- Ok ( TomlValue :: Array ( v) )
725- }
726- JsonValue :: Bool ( b) => Ok ( TomlValue :: Boolean ( b) ) ,
727- JsonValue :: Null => Err ( LangsError :: SettingsCannotContainNull ) ,
728- JsonValue :: Number ( num) => {
729- if let Some ( int) = num. as_i64 ( ) {
730- Ok ( TomlValue :: Integer ( int) )
731- } else if let Some ( float) = num. as_f64 ( ) {
732- Ok ( TomlValue :: Float ( float) )
733- } else {
734- // this error can occur because serde_json supports u64 ints but toml doesn't
735- Err ( LangsError :: SettingNumberTooHigh ( num) )
736- }
737- }
738- JsonValue :: Object ( obj) => {
739- let mut map = TomlMap :: new ( ) ;
740- for ( key, value) in obj {
741- map. insert ( key, setting_json_to_toml ( value) ?) ;
742- }
743- Ok ( TomlValue :: Table ( map) )
744- }
745- JsonValue :: String ( s) => Ok ( TomlValue :: String ( s) ) ,
746- }
747- }
748-
749709/// Checks the exercise's code quality.
750710pub fn checkstyle (
751711 exercise_path : & Path ,
0 commit comments