use indexmap::IndexMap;
use kyu_types::{LogicalType, TypedValue};
#[derive(Clone, Debug)]
pub struct Triple {
pub subject: String,
pub predicate: String,
pub object: RdfObject,
}
#[derive(Clone, Debug)]
pub enum RdfObject {
Uri(String),
Literal {
value: String,
datatype: Option<String>,
lang: Option<String>,
},
}
#[derive(Clone, Debug)]
pub struct RdfSchema {
pub node_tables: Vec<RdfNodeTable>,
pub rel_tables: Vec<RdfRelTable>,
pub prefixes: Vec<(String, String)>,
}
#[derive(Clone, Debug)]
pub struct RdfNodeTable {
pub name: String,
pub type_uri: String,
pub properties: IndexMap<String, LogicalType>,
pub rows: Vec<(String, Vec<TypedValue>)>,
}
#[derive(Clone, Debug)]
pub struct RdfRelTable {
pub name: String,
pub predicate_uri: String,
pub from_table: String,
pub to_table: String,
pub edges: Vec<(String, String)>,
}
pub fn local_name(uri: &str) -> &str {
uri.rsplit_once('#')
.or_else(|| uri.rsplit_once('/'))
.map(|(_, name)| name)
.unwrap_or(uri)
}
pub const RDF_TYPE: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
const XSD: &str = "http://www.w3.org/2001/XMLSchema#";
pub fn xsd_to_logical_type(datatype: Option<&str>) -> LogicalType {
match datatype {
Some(dt) => {
let local = dt.strip_prefix(XSD).unwrap_or(dt);
match local {
"integer" | "int" | "long" | "short" | "byte" | "nonNegativeInteger"
| "positiveInteger" | "nonPositiveInteger" | "negativeInteger" | "unsignedLong"
| "unsignedInt" | "unsignedShort" | "unsignedByte" => LogicalType::Int64,
"float" => LogicalType::Float,
"double" | "decimal" => LogicalType::Double,
"boolean" => LogicalType::Bool,
_ => LogicalType::String,
}
}
None => LogicalType::String,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_local_name() {
assert_eq!(local_name("http://xmlns.com/foaf/0.1/Person"), "Person");
assert_eq!(
local_name("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
"type"
);
assert_eq!(local_name("Person"), "Person");
}
#[test]
fn test_xsd_to_logical_type() {
assert_eq!(
xsd_to_logical_type(Some("http://www.w3.org/2001/XMLSchema#integer")),
LogicalType::Int64
);
assert_eq!(
xsd_to_logical_type(Some("http://www.w3.org/2001/XMLSchema#double")),
LogicalType::Double
);
assert_eq!(
xsd_to_logical_type(Some("http://www.w3.org/2001/XMLSchema#boolean")),
LogicalType::Bool
);
assert_eq!(xsd_to_logical_type(None), LogicalType::String);
}
}