11use std:: {
22 collections:: { HashMap , HashSet } ,
3- path:: PathBuf ,
3+ fs,
4+ path:: { Path , PathBuf } ,
45} ;
56
7+ use anyhow:: { Context , Result } ;
68use serde:: { Deserialize , Serialize } ;
79
810#[ derive( Serialize , Deserialize , Debug ) ]
@@ -15,6 +17,7 @@ pub struct BeefSpace {
1517 pub projects : HashMap < String , ProjectEntry > ,
1618 #[ serde( default ) ]
1719 pub workspace_folders : HashMap < String , HashSet < String > > ,
20+ pub workspace : Workspace ,
1821
1922 #[ serde( flatten) ]
2023 pub other : HashMap < String , toml:: Value > ,
@@ -27,12 +30,13 @@ impl Default for BeefSpace {
2730 locked : Default :: default ( ) ,
2831 projects : Default :: default ( ) ,
2932 workspace_folders : Default :: default ( ) ,
33+ workspace : Default :: default ( ) ,
3034 other : Default :: default ( ) ,
3135 }
3236 }
3337}
3438
35- #[ derive( Serialize , Deserialize , Debug ) ]
39+ #[ derive( Serialize , Deserialize , Debug , Default ) ]
3640#[ serde( rename_all = "PascalCase" ) ]
3741pub struct ProjectEntry {
3842 pub path : PathBuf ,
@@ -41,13 +45,10 @@ pub struct ProjectEntry {
4145 pub other : HashMap < String , toml:: Value > ,
4246}
4347
44- impl Default for ProjectEntry {
45- fn default ( ) -> Self {
46- Self {
47- path : Default :: default ( ) ,
48- other : Default :: default ( ) ,
49- }
50- }
48+ #[ derive( Serialize , Deserialize , Debug , Default ) ]
49+ #[ serde( rename_all = "PascalCase" ) ]
50+ pub struct Workspace {
51+ pub startup_project : String ,
5152}
5253
5354#[ derive( Serialize , Deserialize , Debug ) ]
@@ -59,13 +60,68 @@ pub struct BeefProj {
5960 pub project : Project ,
6061
6162 #[ serde( flatten) ]
62- pub other : HashMap < String , toml:: Value > ,
63+ other : HashMap < String , toml:: Value > ,
64+
65+ #[ serde( skip) ]
66+ path : PathBuf ,
6367}
6468
65- #[ derive( Serialize , Deserialize , Debug ) ]
69+ impl BeefProj {
70+ pub fn new < P > ( name : String , path : & P ) -> BeefProj
71+ where
72+ P : AsRef < Path > ,
73+ {
74+ let startup_object = format ! ( "{}.Program" , name) ;
75+
76+ Self {
77+ file_version : 1 ,
78+ dependencies : Default :: default ( ) ,
79+ project : Project {
80+ name,
81+ target_type : String :: from ( "BeefConsoleApplication" ) ,
82+ startup_object,
83+ ..Default :: default ( )
84+ } ,
85+ other : Default :: default ( ) ,
86+ path : path. as_ref ( ) . to_path_buf ( ) ,
87+ }
88+ }
89+
90+ pub fn from_file < P > ( path : & P ) -> Result < BeefProj >
91+ where
92+ P : AsRef < Path > ,
93+ {
94+ let mut proj: Self = toml:: from_str ( & fs:: read_to_string ( & path) ?) . with_context ( || {
95+ format ! ( "Failed to read project file '{}'" , path. as_ref( ) . display( ) )
96+ } ) ?;
97+ proj. path = path. as_ref ( ) . to_path_buf ( ) ;
98+ Ok ( proj)
99+ }
100+
101+ pub fn save ( & self ) -> Result < ( ) > {
102+ fs:: write ( & self . path , toml:: to_string ( & self ) ?)
103+ . with_context ( || format ! ( "Failed to write project file: '{}'" , self . path. display( ) ) )
104+ }
105+
106+ pub fn path < P > ( & mut self , path : & P ) -> & mut Self
107+ where
108+ P : AsRef < Path > ,
109+ {
110+ self . path = path. as_ref ( ) . to_path_buf ( ) ;
111+ self
112+ }
113+ }
114+
115+ #[ derive( Serialize , Deserialize , Debug , Default ) ]
66116#[ serde( rename_all = "PascalCase" ) ]
67117pub struct Project {
68118 pub name : String ,
119+ #[ serde( default ) ]
120+ pub target_type : String ,
121+ #[ serde( default ) ]
122+ pub startup_object : String ,
123+ #[ serde( default ) ]
124+ pub processor_macros : HashSet < String > ,
69125
70126 #[ serde( flatten) ]
71127 pub other : HashMap < String , toml:: Value > ,
0 commit comments