11use std:: collections:: HashMap ;
22use std:: env;
3- use std:: error:: Error ;
43use std:: fmt:: { self , Display } ;
54use std:: fs:: { self , File } ;
65use std:: io:: { self , Read , Write } ;
@@ -48,10 +47,31 @@ pub struct InitArgs {
4847 workspace_folder : Option < PathBuf > ,
4948}
5049
50+ #[ allow( clippy:: enum_variant_names) ]
51+ #[ derive( thiserror:: Error , Debug ) ]
52+ pub enum InitError {
53+ #[ error( transparent) ]
54+ ParseBoolError ( #[ from] std:: str:: ParseBoolError ) ,
55+ #[ error( transparent) ]
56+ IOError ( #[ from] std:: io:: Error ) ,
57+ #[ error( transparent) ]
58+ SystemTimeError ( #[ from] std:: time:: SystemTimeError ) ,
59+ #[ error( transparent) ]
60+ RegexBuildError ( #[ from] regex:: Error ) ,
61+ #[ error( transparent) ]
62+ JsonError ( #[ from] serde_json:: error:: Error ) ,
63+ #[ error( transparent) ]
64+ JsonWithCommentsError ( #[ from] serde_jsonc:: error:: Error ) ,
65+ #[ error( transparent) ]
66+ PromptError ( #[ from] inquire:: error:: InquireError ) ,
67+ #[ error( transparent) ]
68+ OciPkgError ( #[ from] ocipkg:: error:: Error ) ,
69+ }
70+
5171fn get_feature (
5272 index : & registry:: DevcontainerIndex ,
5373 feature_ref : & OciReference ,
54- ) -> Result < registry:: Feature , Box < dyn Error > > {
74+ ) -> ocipkg :: error :: Result < registry:: Feature > {
5575 log:: debug!( "get_feature" ) ;
5676
5777 match index. get_feature ( & feature_ref. id ( ) ) {
@@ -60,7 +80,7 @@ fn get_feature(
6080 }
6181}
6282
63- fn pull_feature_configuration ( feature_ref : & OciReference ) -> Result < registry:: Feature , Box < dyn Error > > {
83+ fn pull_feature_configuration ( feature_ref : & OciReference ) -> ocipkg :: error :: Result < registry:: Feature > {
6484 log:: debug!( "pull_feature_configuration" ) ;
6585 let bytes = registry:: pull_archive_bytes ( feature_ref) ?;
6686 let mut archive = Archive :: new ( bytes. as_slice ( ) ) ;
@@ -166,7 +186,7 @@ impl<'t> DevOptionPrompt<'t> {
166186 }
167187 }
168188
169- fn display_prompt ( & self ) -> Result < DevOptionPromptValue , Box < dyn Error > > {
189+ fn display_prompt ( & self ) -> Result < DevOptionPromptValue , InitError > {
170190 let dev_option = self . inner ;
171191 let default = dev_option. configured_default ( ) ;
172192
@@ -276,7 +296,7 @@ impl FeatureEntryBuilder {
276296 }
277297 }
278298
279- fn use_prompt_values ( & mut self , feature : & registry:: Feature ) -> Result < ( ) , Box < dyn Error > > {
299+ fn use_prompt_values ( & mut self , feature : & registry:: Feature ) -> Result < ( ) , InitError > {
280300 log:: debug!( "FeatureEntryBuilder::use_prompt_values" ) ;
281301 let key = format ! ( "{}:{}" , feature. id, feature. major_version) ;
282302 let value = {
@@ -335,7 +355,7 @@ struct TemplateBuilder {
335355}
336356
337357impl TemplateBuilder {
338- fn new ( template_ref : & OciReference , config : Option < registry:: Template > ) -> Result < Self , Box < dyn Error > > {
358+ fn new ( template_ref : & OciReference , config : Option < registry:: Template > ) -> ocipkg :: error :: Result < Self > {
339359 log:: debug!( "TemplateBuilder::new" ) ;
340360 let archive_bytes = registry:: pull_archive_bytes ( template_ref) ?;
341361 let template_archive = TemplateBuilder {
@@ -352,7 +372,7 @@ impl TemplateBuilder {
352372 Archive :: new ( self . archive_bytes . as_slice ( ) )
353373 }
354374
355- fn replace_config ( & mut self ) -> Result < ( ) , Box < dyn Error > > {
375+ fn replace_config ( & mut self ) -> std :: io :: Result < ( ) > {
356376 log:: debug!( "TemplateBuilder::replace_config" ) ;
357377 let mut tar = self . as_archive ( ) ;
358378 let entries = tar. entries ( ) ?;
@@ -380,7 +400,7 @@ impl TemplateBuilder {
380400 ) ) ?
381401 }
382402
383- fn use_prompt_values ( & mut self ) -> Result < ( ) , Box < dyn Error > > {
403+ fn use_prompt_values ( & mut self ) -> Result < ( ) , InitError > {
384404 log:: debug!( "TemplateBuilder::use_prompt_values" ) ;
385405 let config = self
386406 . config
@@ -400,7 +420,7 @@ impl TemplateBuilder {
400420 Ok ( ( ) )
401421 }
402422
403- fn use_default_values ( & mut self ) -> Result < ( ) , Box < dyn Error > > {
423+ fn use_default_values ( & mut self ) -> std :: io :: Result < ( ) > {
404424 log:: debug!( "TemplateBuilder::use_default_values" ) ;
405425 let config = self
406426 . config
@@ -447,11 +467,7 @@ impl TemplateBuilder {
447467 false
448468 }
449469
450- fn apply_context_and_features (
451- & mut self ,
452- attempt_single_file : bool ,
453- workspace : & Path ,
454- ) -> Result < ( ) , Box < dyn Error > > {
470+ fn apply_context_and_features ( & mut self , attempt_single_file : bool , workspace : & Path ) -> Result < ( ) , InitError > {
455471 log:: debug!( "TemplateBuilder::apply_context_and_features" ) ;
456472 let template_option_re = Regex :: new ( r"\$\{templateOption:\s*(?<name>\w+)\s*\}" ) ?;
457473 let apply_context = |captures : & Captures | -> & [ u8 ] {
@@ -548,7 +564,7 @@ impl TemplateBuilder {
548564 Ok ( ( ) )
549565 }
550566
551- fn create_empty_start_point ( ) -> Result < Self , Box < dyn Error > > {
567+ fn create_empty_start_point ( ) -> Result < Self , InitError > {
552568 let template_value = serde_json:: json!( {
553569 "id" : "tyedev-base-template" ,
554570 "version" : "1.0.0" ,
@@ -695,7 +711,7 @@ pub fn init(
695711 include_deprecated,
696712 workspace_folder,
697713 } : InitArgs ,
698- ) -> Result < ( ) , Box < dyn Error > > {
714+ ) -> Result < ( ) , InitError > {
699715 log:: debug!( "init" ) ;
700716 // Do this evaluation of the `env` first so that it can error early.
701717 let workspace = workspace_folder. map_or_else ( env:: current_dir, Ok ) ?;
@@ -818,12 +834,11 @@ pub fn init(
818834// TODO these are more *proof of concept* than actual tests...
819835#[ cfg( test) ]
820836mod tests {
821- use super :: { FeatureEntryBuilder , TemplateBuilder } ;
837+ use super :: { FeatureEntryBuilder , InitError , TemplateBuilder } ;
822838 use serde_json:: { self , Map , Value } ;
823- use std:: error:: Error ;
824839
825840 #[ test]
826- fn test_feature_entry_builder_as_value ( ) -> Result < ( ) , Box < dyn Error > > {
841+ fn test_feature_entry_builder_as_value ( ) -> serde_json :: error :: Result < ( ) > {
827842 let mut feature_entry_builder = FeatureEntryBuilder :: default ( ) ;
828843
829844 let git_id = "ghcr.io/devcontainers/git:1" ;
@@ -843,7 +858,7 @@ mod tests {
843858 }
844859
845860 #[ test]
846- fn test_create_empty_start_point ( ) -> Result < ( ) , Box < dyn Error > > {
861+ fn test_create_empty_start_point ( ) -> Result < ( ) , InitError > {
847862 let _template_builder = TemplateBuilder :: create_empty_start_point ( ) ?;
848863 Ok ( ( ) )
849864 }
0 commit comments