1- #![ feature( let_chains) ]
21use anyhow:: Result ;
32use clap:: { Parser , Subcommand } ;
43use ontoenv:: api:: { OntoEnv , ResolveTarget } ;
@@ -181,17 +180,16 @@ fn main() -> Result<()> {
181180 // create the env object to use in the subcommand.
182181 // - if temporary is true, create a new env object each time
183182 // - if temporary is false, load the env from the .ontoenv directory if it exists
184- let mut env: Option < OntoEnv > = if cmd. temporary {
183+ let mut env: Option < OntoEnv > = None ;
184+
185+ if cmd. temporary {
185186 // Create a new OntoEnv object in temporary mode
186187 let mut e = OntoEnv :: init ( config. clone ( ) , false ) ?;
187188 e. update ( ) ?;
188- Some ( e)
189+ env = Some ( e) ;
189190 } else if cmd. command . to_string ( ) != "Init" {
190- Some ( OntoEnv :: load_from_directory ( current_dir ( ) ?) ?)
191- } else {
192- // Create a new OntoEnv object in non-temporary mode
193- None
194- } ;
191+ env = Some ( OntoEnv :: load_from_directory ( current_dir ( ) ?) ?) ;
192+ }
195193
196194 match cmd. command {
197195 Commands :: Init {
@@ -227,11 +225,12 @@ fn main() -> Result<()> {
227225 ) ;
228226 }
229227 Commands :: Status => {
230- let env = env. as_ref ( ) . unwrap ( ) ;
231- // load env from .ontoenv/ontoenv.json
232- let status = env. status ( ) ?;
233- // pretty print the status
234- println ! ( "{}" , status) ;
228+ if let Some ( ref env) = env {
229+ // load env from .ontoenv/ontoenv.json
230+ let status = env. status ( ) ?;
231+ // pretty print the status
232+ println ! ( "{}" , status) ;
233+ }
235234 }
236235 Commands :: Refresh => {
237236 // if temporary, raise an error
@@ -240,9 +239,10 @@ fn main() -> Result<()> {
240239 "Cannot refresh in temporary mode. Run `ontoenv init` to create a new OntoEnv."
241240 ) ) ;
242241 }
243- let env = env. as_mut ( ) . unwrap ( ) ;
244- env. update ( ) ?;
245- env. save_to_directory ( ) ?;
242+ if let Some ( ref mut env) = env {
243+ env. update ( ) ?;
244+ env. save_to_directory ( ) ?;
245+ }
246246 }
247247 Commands :: GetClosure {
248248 ontology,
@@ -252,24 +252,21 @@ fn main() -> Result<()> {
252252 } => {
253253 // make ontology an IRI
254254 let iri = NamedNode :: new ( ontology) . map_err ( |e| anyhow:: anyhow!( e. to_string( ) ) ) ?;
255- let env = env. as_mut ( ) . unwrap ( ) ;
256-
257- let graphid = env
258- . resolve ( ResolveTarget :: Graph ( iri . clone ( ) ) )
259- . ok_or ( anyhow :: anyhow! ( format! ( "Ontology {} not found" , iri ) ) ) ?;
260- let closure = env . get_dependency_closure ( & graphid ) ? ;
261- let ( graph , _successful , failed_imports ) =
262- env . get_union_graph ( & closure , rewrite_sh_prefixes , remove_owl_imports ) ? ;
263- if let Some ( failed_imports ) = failed_imports {
264- for imp in failed_imports {
265- eprintln ! ( "{}" , imp ) ;
255+ if let Some ( ref mut env) = env {
256+ let graphid = env
257+ . resolve ( ResolveTarget :: Graph ( iri . clone ( ) ) )
258+ . ok_or ( anyhow :: anyhow! ( format! ( "Ontology {} not found" , iri ) ) ) ? ;
259+ let closure = env . get_dependency_closure ( & graphid ) ?;
260+ let ( graph , _successful , failed_imports ) =
261+ env . get_union_graph ( & closure , rewrite_sh_prefixes , remove_owl_imports ) ? ;
262+ if let Some ( failed_imports ) = failed_imports {
263+ for imp in failed_imports {
264+ eprintln ! ( "{}" , imp ) ;
265+ }
266266 }
267- }
268- // write the graph to a file
269- if let Some ( destination) = destination {
267+ // write the graph to a file
268+ let destination = destination. unwrap_or_else ( || "output.ttl" . to_string ( ) ) ;
270269 write_dataset_to_file ( & graph, & destination) ?;
271- } else {
272- write_dataset_to_file ( & graph, "output.ttl" ) ?;
273270 }
274271 }
275272 Commands :: Add { url, file } => {
@@ -278,76 +275,82 @@ fn main() -> Result<()> {
278275 ( None , Some ( file) ) => OntologyLocation :: File ( PathBuf :: from ( file) ) ,
279276 _ => return Err ( anyhow:: anyhow!( "Must specify either --url or --file" ) ) ,
280277 } ;
281- let env = env. as_mut ( ) . unwrap ( ) ;
282-
283- env. add ( location , true ) ?;
284- env . save_to_directory ( ) ? ;
278+ if let Some ( ref mut env) = env {
279+ env . add ( location , true ) ? ;
280+ env. save_to_directory ( ) ?;
281+ }
285282 }
286283 Commands :: ListOntologies => {
287- // print list of ontology URLs from env.onologies.values() sorted alphabetically
288- let env = env. as_ref ( ) . unwrap ( ) ;
289- let mut ontologies: Vec < & GraphIdentifier > = env. ontologies ( ) . keys ( ) . collect ( ) ;
290- ontologies. sort_by ( |a, b| a. name ( ) . cmp ( & b. name ( ) ) ) ;
291- ontologies. dedup_by ( |a, b| a. name ( ) == b. name ( ) ) ;
292- for ont in ontologies {
293- println ! ( "{}" , ont. name( ) . as_str( ) ) ;
284+ if let Some ( ref env) = env {
285+ // print list of ontology URLs from env.ontologies.values() sorted alphabetically
286+ let mut ontologies: Vec < & GraphIdentifier > = env. ontologies ( ) . keys ( ) . collect ( ) ;
287+ ontologies. sort_by ( |a, b| a. name ( ) . cmp ( & b. name ( ) ) ) ;
288+ ontologies. dedup_by ( |a, b| a. name ( ) == b. name ( ) ) ;
289+ for ont in ontologies {
290+ println ! ( "{}" , ont. name( ) . as_str( ) ) ;
291+ }
294292 }
295293 }
296294 Commands :: ListLocations => {
297- let env = env. as_ref ( ) . unwrap ( ) ;
298- let mut ontologies: Vec < & GraphIdentifier > = env. ontologies ( ) . keys ( ) . collect ( ) ;
299- ontologies. sort_by ( |a, b| a. location ( ) . as_str ( ) . cmp ( b. location ( ) . as_str ( ) ) ) ;
300- for ont in ontologies {
301- println ! ( "{}" , ont. location( ) . as_str( ) ) ;
295+ if let Some ( ref env) = env {
296+ let mut ontologies: Vec < & GraphIdentifier > = env. ontologies ( ) . keys ( ) . collect ( ) ;
297+ ontologies. sort_by ( |a, b| a. location ( ) . as_str ( ) . cmp ( b. location ( ) . as_str ( ) ) ) ;
298+ for ont in ontologies {
299+ println ! ( "{}" , ont. location( ) . as_str( ) ) ;
300+ }
302301 }
303302 }
304303 Commands :: Dump { contains } => {
305- let env = env. as_ref ( ) . unwrap ( ) ;
306- env. dump ( contains. as_deref ( ) ) ;
304+ if let Some ( ref env) = env {
305+ env. dump ( contains. as_deref ( ) ) ;
306+ }
307307 }
308308 Commands :: DepGraph { roots, output } => {
309- let env = env. as_mut ( ) . unwrap ( ) ;
310- let dot = if let Some ( roots) = roots {
311- let roots: Vec < GraphIdentifier > = roots
312- . iter ( )
313- . map ( |iri| {
314- env. resolve ( ResolveTarget :: Graph ( NamedNode :: new ( iri) . unwrap ( ) ) )
315- . unwrap ( )
316- . clone ( )
317- } )
318- . collect ( ) ;
319- env. rooted_dep_graph_to_dot ( roots) ?
320- } else {
321- env. dep_graph_to_dot ( ) ?
322- } ;
323- // call graphviz to generate PDF
324- let dot_path = current_dir ( ) ?. join ( "dep_graph.dot" ) ;
325- std:: fs:: write ( & dot_path, dot) ?;
326- let output_path = output. unwrap_or_else ( || "dep_graph.pdf" . to_string ( ) ) ;
327- let output = std:: process:: Command :: new ( "dot" )
328- . args ( [ "-Tpdf" , dot_path. to_str ( ) . unwrap ( ) , "-o" , & output_path] )
329- . output ( ) ?;
330- if !output. status . success ( ) {
331- return Err ( anyhow:: anyhow!(
332- "Failed to generate PDF: {}" ,
333- String :: from_utf8_lossy( & output. stderr)
334- ) ) ;
309+ if let Some ( ref mut env) = env {
310+ let dot = if let Some ( roots) = roots {
311+ let roots: Vec < GraphIdentifier > = roots
312+ . iter ( )
313+ . map ( |iri| {
314+ env. resolve ( ResolveTarget :: Graph ( NamedNode :: new ( iri) . unwrap ( ) ) )
315+ . unwrap ( )
316+ . clone ( )
317+ } )
318+ . collect ( ) ;
319+ env. rooted_dep_graph_to_dot ( roots) ?
320+ } else {
321+ env. dep_graph_to_dot ( ) ?
322+ } ;
323+ // call graphviz to generate PDF
324+ let dot_path = current_dir ( ) ?. join ( "dep_graph.dot" ) ;
325+ std:: fs:: write ( & dot_path, dot) ?;
326+ let output_path = output. unwrap_or_else ( || "dep_graph.pdf" . to_string ( ) ) ;
327+ let output = std:: process:: Command :: new ( "dot" )
328+ . args ( [ "-Tpdf" , dot_path. to_str ( ) . unwrap ( ) , "-o" , & output_path] )
329+ . output ( ) ?;
330+ if !output. status . success ( ) {
331+ return Err ( anyhow:: anyhow!(
332+ "Failed to generate PDF: {}" ,
333+ String :: from_utf8_lossy( & output. stderr)
334+ ) ) ;
335+ }
335336 }
336337 }
337338 Commands :: Dependents { ontologies } => {
338- let env = env. as_mut ( ) . unwrap ( ) ;
339- for ont in ontologies {
340- let iri = NamedNode :: new ( ont) . map_err ( |e| anyhow:: anyhow!( e. to_string( ) ) ) ?;
341- let dependents = env. get_dependents ( & iri) ?;
342- println ! ( "Dependents of {}: " , iri) ;
343- for dep in dependents {
344- println ! ( "{}" , dep) ;
339+ if let Some ( ref mut env) = env {
340+ for ont in ontologies {
341+ let iri = NamedNode :: new ( ont) . map_err ( |e| anyhow:: anyhow!( e. to_string( ) ) ) ?;
342+ let dependents = env. get_dependents ( & iri) ?;
343+ println ! ( "Dependents of {}: " , iri) ;
344+ for dep in dependents {
345+ println ! ( "{}" , dep) ;
346+ }
345347 }
346348 }
347349 }
348350 Commands :: Doctor => {
349- let env = env. as_mut ( ) . unwrap ( ) ;
350- env. doctor ( ) ;
351+ if let Some ( ref mut env) = env {
352+ env. doctor ( ) ;
353+ }
351354 }
352355 Commands :: Reset => {
353356 // remove .ontoenv directory
0 commit comments