openlineage_client/
naming.rs1use url::Url;
7
8#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct DatasetName {
11 pub namespace: String,
13 pub name: String,
15}
16
17impl DatasetName {
18 pub fn from_location(url: &Url) -> Self {
25 let scheme = url.scheme();
26 match scheme {
27 "s3" | "s3a" | "gs" | "gcs" | "abfs" | "abfss" | "wasbs" => {
28 let bucket = url.host_str().unwrap_or_default();
29 let name = url.path().trim_start_matches('/').to_string();
30 DatasetName {
31 namespace: format!("{scheme}://{bucket}"),
32 name,
33 }
34 }
35 "file" => DatasetName {
36 namespace: "file".to_string(),
37 name: url.path().to_string(),
38 },
39 _ => {
40 let host = url.host_str().unwrap_or_default();
41 DatasetName {
42 namespace: format!("{scheme}://{host}"),
43 name: url.path().trim_start_matches('/').to_string(),
44 }
45 }
46 }
47 }
48
49 pub fn from_table_ref(default_namespace: &str, table_ref: &str) -> Self {
53 DatasetName {
54 namespace: default_namespace.to_string(),
55 name: table_ref.to_string(),
56 }
57 }
58}
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63
64 #[test]
65 fn s3_location() {
66 let url = Url::parse("s3://open-lakehouse/warehouse/t1").unwrap();
67 let n = DatasetName::from_location(&url);
68 assert_eq!(n.namespace, "s3://open-lakehouse");
69 assert_eq!(n.name, "warehouse/t1");
70 }
71
72 #[test]
73 fn file_location() {
74 let url = Url::parse("file:///tmp/data/t").unwrap();
75 let n = DatasetName::from_location(&url);
76 assert_eq!(n.namespace, "file");
77 assert_eq!(n.name, "/tmp/data/t");
78 }
79}