@@ -16,6 +16,7 @@ use serde_with::{serde_as, DeserializeAs, SerializeAs};
1616use std:: collections:: HashMap ;
1717use std:: hash:: Hash ;
1818use std:: path:: PathBuf ;
19+ use url:: Url ;
1920//
2021// custom derive for NamedNode
2122fn namednode_ser < S > ( namednode : & NamedNode , serializer : S ) -> Result < S :: Ok , S :: Error >
@@ -117,8 +118,13 @@ pub enum OntologyLocation {
117118impl std:: fmt:: Display for OntologyLocation {
118119 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
119120 match self {
120- OntologyLocation :: File ( p) => write ! ( f, "file://{}" , p. to_str( ) . unwrap_or_default( ) ) ,
121- OntologyLocation :: Url ( u) => write ! ( f, "{u}" ) ,
121+ OntologyLocation :: Url ( url) => write ! ( f, "{}" , url) ,
122+ OntologyLocation :: File ( path) => {
123+ let url_string = Url :: from_file_path ( path)
124+ . map_err ( |_| std:: fmt:: Error ) ? // Convert the error
125+ . to_string ( ) ;
126+ write ! ( f, "{}" , url_string)
127+ }
122128 }
123129 }
124130}
@@ -130,50 +136,6 @@ impl Default for OntologyLocation {
130136 }
131137}
132138
133- fn file_path_to_file_uri ( p : & PathBuf ) -> String {
134- #[ cfg( windows) ]
135- {
136- let mut s = p. to_string_lossy ( ) . to_string ( ) ;
137- if s. contains ( '\\' ) {
138- s = s. replace ( '\\' , "/" ) ;
139- }
140- // Ensure leading slash for drive-letter paths (e.g., C:/...)
141- if !s. starts_with ( '/' ) {
142- if s. len ( ) >= 2 && s. as_bytes ( ) [ 1 ] == b':' {
143- s = format ! ( "/{s}" ) ;
144- }
145- }
146- // Minimal percent-encoding for common problematic characters in paths
147- let mut encoded = String :: with_capacity ( s. len ( ) ) ;
148- for ch in s. chars ( ) {
149- match ch {
150- ' ' => encoded. push_str ( "%20" ) ,
151- '#' => encoded. push_str ( "%23" ) ,
152- '?' => encoded. push_str ( "%3F" ) ,
153- '%' => encoded. push_str ( "%25" ) ,
154- _ => encoded. push ( ch) ,
155- }
156- }
157- return format ! ( "file://{encoded}" ) ;
158- }
159- #[ cfg( not( windows) ) ]
160- {
161- let s = p. to_string_lossy ( ) ;
162- // Minimal percent-encoding for common problematic characters in paths
163- let mut encoded = String :: with_capacity ( s. len ( ) ) ;
164- for ch in s. chars ( ) {
165- match ch {
166- ' ' => encoded. push_str ( "%20" ) ,
167- '#' => encoded. push_str ( "%23" ) ,
168- '?' => encoded. push_str ( "%3F" ) ,
169- '%' => encoded. push_str ( "%25" ) ,
170- _ => encoded. push ( ch) ,
171- }
172- }
173- return format ! ( "file://{encoded}" ) ;
174- }
175- }
176-
177139impl OntologyLocation {
178140 pub fn as_str ( & self ) -> & str {
179141 match self {
@@ -221,7 +183,10 @@ impl OntologyLocation {
221183 pub fn to_iri ( & self ) -> NamedNode {
222184 match self {
223185 OntologyLocation :: File ( p) => {
224- let iri = file_path_to_file_uri ( p) ;
186+ // Use the Url crate, just like in the Display impl
187+ let iri = Url :: from_file_path ( p)
188+ . expect ( "Failed to create file URL for IRI" )
189+ . to_string ( ) ;
225190 NamedNode :: new ( iri) . unwrap ( )
226191 }
227192 OntologyLocation :: Url ( u) => {
0 commit comments