@@ -801,35 +801,24 @@ fn main() -> Result<()> {
801801 let mut all: BTreeMap < String , Vec < Vec < String > > > = BTreeMap :: new ( ) ;
802802 for ont in ontologies {
803803 let iri = NamedNode :: new ( ont) . map_err ( |e| anyhow:: anyhow!( e. to_string( ) ) ) ?;
804- let paths = env. get_import_paths ( & iri) ?;
805- let mut unique: BTreeSet < Vec < String > > = BTreeSet :: new ( ) ;
806- for p in paths {
807- unique. insert ( p. into_iter ( ) . map ( |id| id. to_uri_string ( ) ) . collect ( ) ) ;
808- }
809- let arr: Vec < Vec < String > > = unique. into_iter ( ) . collect ( ) ;
810- all. insert ( iri. to_uri_string ( ) , arr) ;
804+ let ( paths, missing) = match env. explain_import ( & iri) ? {
805+ ontoenv:: api:: ImportPaths :: Present ( paths) => ( paths, false ) ,
806+ ontoenv:: api:: ImportPaths :: Missing { importers } => ( importers, true ) ,
807+ } ;
808+ let formatted = format_import_paths ( & iri, paths, missing) ;
809+ all. insert ( iri. to_uri_string ( ) , formatted) ;
811810 }
812811 println ! ( "{}" , serde_json:: to_string_pretty( & all) ?) ;
813812 } else {
814813 for ont in ontologies {
815814 let iri = NamedNode :: new ( ont) . map_err ( |e| anyhow:: anyhow!( e. to_string( ) ) ) ?;
816- let paths = env. get_import_paths ( & iri) ?;
817- if paths. is_empty ( ) {
818- println ! ( "No importers found for {}" , iri. to_uri_string( ) ) ;
819- continue ;
820- }
821- println ! ( "Why {}:" , iri. to_uri_string( ) ) ;
822- let mut lines: BTreeSet < String > = BTreeSet :: new ( ) ;
823- for p in paths {
824- let line = p
825- . into_iter ( )
826- . map ( |id| id. to_uri_string ( ) )
827- . collect :: < Vec < _ > > ( )
828- . join ( " -> " ) ;
829- lines. insert ( line) ;
830- }
831- for line in lines {
832- println ! ( "{}" , line) ;
815+ match env. explain_import ( & iri) ? {
816+ ontoenv:: api:: ImportPaths :: Present ( paths) => {
817+ print_import_paths ( & iri, paths, false ) ;
818+ }
819+ ontoenv:: api:: ImportPaths :: Missing { importers } => {
820+ print_import_paths ( & iri, importers, true ) ;
821+ }
833822 }
834823 }
835824 }
@@ -874,3 +863,58 @@ fn require_ontoenv(env: Option<OntoEnv>) -> Result<OntoEnv> {
874863 anyhow:: anyhow!( "OntoEnv not found. Run `ontoenv init` to create a new OntoEnv or use -t/--temporary to use a temporary environment." )
875864 } )
876865}
866+
867+ fn format_import_paths (
868+ target : & NamedNode ,
869+ paths : Vec < Vec < GraphIdentifier > > ,
870+ missing : bool ,
871+ ) -> Vec < Vec < String > > {
872+ let mut unique: BTreeSet < Vec < String > > = BTreeSet :: new ( ) ;
873+ if paths. is_empty ( ) {
874+ if missing {
875+ unique. insert ( vec ! [ format!( "{} (missing)" , target. to_uri_string( ) ) ] ) ;
876+ }
877+ return unique. into_iter ( ) . collect ( ) ;
878+ }
879+ for path in paths {
880+ let mut entries: Vec < String > = path. into_iter ( ) . map ( |id| id. to_uri_string ( ) ) . collect ( ) ;
881+ if missing {
882+ entries. push ( format ! ( "{} (missing)" , target. to_uri_string( ) ) ) ;
883+ }
884+ unique. insert ( entries) ;
885+ }
886+ unique. into_iter ( ) . collect ( )
887+ }
888+
889+ fn print_import_paths ( target : & NamedNode , paths : Vec < Vec < GraphIdentifier > > , missing : bool ) {
890+ if paths. is_empty ( ) {
891+ if missing {
892+ println ! (
893+ "Ontology {} is missing but no importers reference it." ,
894+ target. to_uri_string( )
895+ ) ;
896+ } else {
897+ println ! ( "No importers found for {}" , target. to_uri_string( ) ) ;
898+ }
899+ return ;
900+ }
901+
902+ println ! (
903+ "Why {}{}:" ,
904+ target. to_uri_string( ) ,
905+ if missing { " (missing)" } else { "" }
906+ ) ;
907+
908+ let mut lines: BTreeSet < String > = BTreeSet :: new ( ) ;
909+ for path in paths {
910+ let mut segments: Vec < String > = path. into_iter ( ) . map ( |id| id. to_uri_string ( ) ) . collect ( ) ;
911+ if missing {
912+ segments. push ( format ! ( "{} (missing)" , target. to_uri_string( ) ) ) ;
913+ }
914+ lines. insert ( segments. join ( " -> " ) ) ;
915+ }
916+
917+ for line in lines {
918+ println ! ( "{}" , line) ;
919+ }
920+ }
0 commit comments