diff --git a/Cargo.toml b/Cargo.toml index 98de35c..783e025 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ env_logger = "0.10.0" mime = "0.3.16" lazy_static = "1.4.0" thiserror = "1.0.31" -regex = "1.6.0" +fancy-regex = "0.10.0" colored = "2.0.0" ignore = "0.4.18" iref = "2.1.3" diff --git a/LICENSE b/LICENSE index ab50f83..53235e4 100644 --- a/LICENSE +++ b/LICENSE @@ -1,7 +1,7 @@ MIT License Copyright (c) 2022 Enterprise Knowledge Graph Foundation. -Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/class.rs b/src/class.rs index 99b2ae4..2106f02 100644 --- a/src/class.rs +++ b/src/class.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- use std::{ops::Deref, sync::Arc}; @@ -6,11 +6,11 @@ use std::{ops::Deref, sync::Arc}; use indoc::formatdoc; use crate::{ + prefix::Prefix, Error, FactDomain, GraphConnection, Parameters, - Prefix, Prefixes, Statement, Transaction, @@ -120,7 +120,7 @@ impl Class { #[cfg(test)] mod tests { - use crate::{class::Class, Prefix}; + use crate::{class::Class, prefix::Prefix}; #[test] fn test_a_class_01() { diff --git a/src/connectable_data_store.rs b/src/connectable_data_store.rs index 1fc404c..c12ec6a 100644 --- a/src/connectable_data_store.rs +++ b/src/connectable_data_store.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- use std::sync::{ diff --git a/src/cursor/cursor.rs b/src/cursor/cursor.rs index 949783b..a3563e6 100644 --- a/src/cursor/cursor.rs +++ b/src/cursor/cursor.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- use std::{ffi::CString, fmt::Debug, ptr, sync::Arc}; diff --git a/src/cursor/cursor_row.rs b/src/cursor/cursor_row.rs index 4754f89..863e16b 100644 --- a/src/cursor/cursor_row.rs +++ b/src/cursor/cursor_row.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- use { diff --git a/src/cursor/mod.rs b/src/cursor/mod.rs index 40e2961..3f08a0a 100644 --- a/src/cursor/mod.rs +++ b/src/cursor/mod.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- use std::ffi::CStr; diff --git a/src/cursor/opened_cursor.rs b/src/cursor/opened_cursor.rs index 2217402..381dc70 100644 --- a/src/cursor/opened_cursor.rs +++ b/src/cursor/opened_cursor.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- use { diff --git a/src/data_store_connection.rs b/src/data_store_connection.rs index 561a83c..acadc7d 100644 --- a/src/data_store_connection.rs +++ b/src/data_store_connection.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- use std::{ @@ -14,11 +14,11 @@ use std::{ }; use colored::Colorize; +use fancy_regex::Regex; use ignore::{types::TypesBuilder, WalkBuilder}; use indoc::formatdoc; use iref::Iri; use mime::Mime; -use regex::Regex; use crate::{ database_call, @@ -39,7 +39,6 @@ use crate::{ FactDomain, Graph, Parameters, - Prefix, Prefixes, Statement, Streamer, @@ -50,7 +49,7 @@ use crate::{ LOG_TARGET_FILES, TEXT_TURTLE, }; -use crate::{error::Error, ServerConnection}; +use crate::{error::Error, prefix::Prefix, ServerConnection}; #[derive(Debug)] pub struct DataStoreConnection { diff --git a/src/error.rs b/src/error.rs index 81af078..50510a4 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- extern crate alloc; diff --git a/src/exception.rs b/src/exception.rs index 47d91ea..31874fa 100644 --- a/src/exception.rs +++ b/src/exception.rs @@ -1,20 +1,22 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- // extern crate libc; -use std::{ - ffi::CStr, - fmt::{Display, Formatter}, - panic::catch_unwind, - str::Utf8Error, +use { + crate::{ + root::{CException_getExceptionName, CException_what}, + Error::{self}, + }, + std::{ + ffi::CStr, + fmt::{Display, Formatter}, + panic::catch_unwind, + str::Utf8Error, + }, }; pub use crate::root::CException; -use crate::{ - root::{CException_getExceptionName, CException_what}, - Error::{self}, -}; impl CException { pub fn handle(action: &str, f: F) -> Result<(), Error> @@ -36,7 +38,8 @@ impl CException { match res { Ok(..) => Ok(()), Err(err) => { - panic!("{err:}") + // panic!("{err:}") + Err(err) }, } }, diff --git a/src/graph.rs b/src/graph.rs index ac6ae4c..4882735 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -1,11 +1,11 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- use std::ffi::CString; use lazy_static::lazy_static; -use crate::{LexicalValue, Prefix}; +use crate::{prefix::Prefix, LexicalValue}; lazy_static! { pub static ref NS_RDFOX: Prefix = diff --git a/src/graph_connection.rs b/src/graph_connection.rs index 502ae45..155b3e5 100644 --- a/src/graph_connection.rs +++ b/src/graph_connection.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- use std::{ diff --git a/src/lib.rs b/src/lib.rs index 9bf3acc..2526ce0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- #![feature(rustc_private)] #![feature(cstr_from_bytes_until_nul)] @@ -23,7 +23,8 @@ pub use mime::Mime; pub use namespace::*; pub use parameters::{FactDomain, Parameters, PersistenceMode}; pub use predicate::Predicate; -pub use prefixes::{Prefix, Prefixes, PrefixesBuilder}; +pub use prefix::Prefix; +pub use prefixes::{Prefixes, PrefixesBuilder}; pub use rdf::{DataType, LexicalValue, ResourceValue, Term}; pub use role_creds::RoleCreds; pub use server::Server; @@ -82,6 +83,7 @@ mod license; mod namespace; mod parameters; mod predicate; +mod prefix; mod prefixes; mod rdf; mod role_creds; diff --git a/src/license.rs b/src/license.rs index 4b9992e..c49d248 100644 --- a/src/license.rs +++ b/src/license.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- use std::path::{Path, PathBuf}; @@ -9,15 +9,23 @@ pub const RDFOX_DEFAULT_LICENSE_FILE_NAME: &str = "RDFox.lic"; pub fn find_license(dir: &Path) -> Result { if dir.exists() { let license = dir.join(RDFOX_DEFAULT_LICENSE_FILE_NAME); - tracing::debug!("Checking license file {license:?}"); + tracing::debug!( + target: crate::LOG_TARGET_DATABASE, + "Checking license file {license:?}" + ); if license.exists() { return Ok(license) } } // Now check home directory ~/.RDFox/RDFox.lic // - let license = PathBuf::from(format!("{RDFOX_HOME}/{RDFOX_DEFAULT_LICENSE_FILE_NAME}")); - tracing::debug!("Checking license file {license:?}"); + let license = PathBuf::from(format!( + "{RDFOX_HOME}/{RDFOX_DEFAULT_LICENSE_FILE_NAME}" + )); + tracing::debug!( + target: crate::LOG_TARGET_DATABASE, + "Checking license file {license:?}" + ); if license.exists() { return Ok(license) } diff --git a/src/namespace.rs b/src/namespace.rs index d0484da..de596ad 100644 --- a/src/namespace.rs +++ b/src/namespace.rs @@ -1,10 +1,10 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- use iref::Iri; use lazy_static::lazy_static; -use crate::Prefix; +use crate::prefix::Prefix; type PrefixName<'a> = &'a str; diff --git a/src/parameters.rs b/src/parameters.rs index 2573ba6..264d933 100644 --- a/src/parameters.rs +++ b/src/parameters.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- extern crate alloc; diff --git a/src/predicate.rs b/src/predicate.rs index 3472e72..3a99ee8 100644 --- a/src/predicate.rs +++ b/src/predicate.rs @@ -1,4 +1,4 @@ -use crate::Prefix; +use crate::prefix::Prefix; pub struct Predicate<'a> { pub namespace: &'a Prefix, @@ -54,7 +54,7 @@ impl<'a> Predicate<'a> { mod tests { use iref::Iri; - use crate::{predicate::Predicate, Prefix}; + use crate::{predicate::Predicate, prefix::Prefix}; #[test] fn test_predicate() { diff --git a/src/prefix.rs b/src/prefix.rs new file mode 100644 index 0000000..e93419f --- /dev/null +++ b/src/prefix.rs @@ -0,0 +1,63 @@ +use iref::{Iri, IriBuf}; + +#[derive(Debug, PartialEq, Eq, Clone)] +pub struct Prefix { + /// assumed to end with ':' + pub name: String, + /// assumed to end with either '/' or '#' + pub iri: IriBuf, +} + +impl std::fmt::Display for Prefix { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{} <{}>", self.name.as_str(), self.iri.as_str()) + } +} + +impl Prefix { + pub fn declare<'a, Base: Into>>(name: &str, iri: Base) -> Self { + let iri = iri.into(); + match iri.as_str().chars().last() { + Some('/') | Some('#') => { + Self { + name: name.to_string(), + iri: IriBuf::from(iri), + } + }, + _ => { + Self { + name: name.to_string(), + iri: IriBuf::from_string(format!("{}/", iri)).unwrap(), + } + }, + } + } + + pub fn declare_from_str(name: &str, iri: &str) -> Self { + Self::declare(name, Iri::from_str(iri).unwrap()) + } + + pub fn with_local_name(&self, name: &str) -> Result { + let binding = self.iri.as_iri_ref(); + match binding.path().as_str().chars().last() { + Some(char) if char == '/' => { + IriBuf::new(format!("{}{}", self.iri.as_str(), name).as_str()) + }, + Some(char) if char == '#' => { + IriBuf::new(format!("{}{}", self.iri.as_str(), name).as_str()) + }, + _ => IriBuf::new(format!("{}/{}", self.iri.as_str(), name).as_str()), + } + } + + #[cfg(feature = "rdftk_support")] + pub fn as_rdftk_iri_ref(&self) -> Result { + Ok(rdftk_iri::IRIRef::new(self.as_rdftk_iri()?)) + } + + #[cfg(feature = "rdftk_support")] + pub fn as_rdftk_iri(&self) -> Result { + use std::str::FromStr; + rdftk_iri::IRI::from_str(self.iri.as_str()) + } +} diff --git a/src/prefixes.rs b/src/prefixes.rs index 97f0bcb..b270453 100644 --- a/src/prefixes.rs +++ b/src/prefixes.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- use std::{ @@ -9,12 +9,13 @@ use std::{ sync::{Arc, Mutex}, }; -use iref::{Iri, IriBuf}; +use iref::Iri; use crate::{ database_call, error::Error, namespace::{PREFIX_OWL, PREFIX_RDF, PREFIX_RDFS, PREFIX_XSD}, + prefix::Prefix, root::{ CPrefixes, CPrefixes_DeclareResult as PrefixDeclareResult, @@ -162,68 +163,6 @@ impl Prefixes { pub fn c_mut_ptr(&self) -> *mut CPrefixes { self.inner } } -#[derive(Debug, PartialEq, Eq, Clone)] -pub struct Prefix { - /// assumed to end with ':' - pub name: String, - /// assumed to end with either '/' or '#' - pub iri: IriBuf, -} - -impl std::fmt::Display for Prefix { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{} <{}>", self.name.as_str(), self.iri.as_str()) - } -} - -impl Prefix { - pub fn declare<'a, Base: Into>>(name: &str, iri: Base) -> Self { - let iri = iri.into(); - match iri.as_str().chars().last() { - Some('/') | Some('#') => { - Self { - name: name.to_string(), - iri: IriBuf::from(iri), - } - }, - _ => { - Self { - name: name.to_string(), - iri: IriBuf::from_string(format!("{}/", iri)).unwrap(), - } - }, - } - } - - pub fn declare_from_str(name: &str, iri: &str) -> Self { - Self::declare(name, Iri::from_str(iri).unwrap()) - } - - pub fn with_local_name(&self, name: &str) -> Result { - let binding = self.iri.as_iri_ref(); - match binding.path().as_str().chars().last() { - Some(char) if char == '/' => { - IriBuf::new(format!("{}{}", self.iri.as_str(), name).as_str()) - }, - Some(char) if char == '#' => { - IriBuf::new(format!("{}{}", self.iri.as_str(), name).as_str()) - }, - _ => IriBuf::new(format!("{}/{}", self.iri.as_str(), name).as_str()), - } - } - - #[cfg(feature = "rdftk_support")] - pub fn as_rdftk_iri_ref(&self) -> Result { - Ok(rdftk_iri::IRIRef::new(self.as_rdftk_iri()?)) - } - - #[cfg(feature = "rdftk_support")] - pub fn as_rdftk_iri(&self) -> Result { - use std::str::FromStr; - rdftk_iri::IRI::from_str(self.iri.as_str()) - } -} - #[derive(Default)] pub struct PrefixesBuilder { prefixes: Vec, @@ -259,7 +198,7 @@ impl<'a> PrefixesBuilder { mod tests { use iref::Iri; - use crate::Prefix; + use crate::prefix::Prefix; #[test] fn test_a_prefix() -> Result<(), iref::Error> { diff --git a/src/rdf/data_type.rs b/src/rdf/data_type.rs index dd4f53f..3279b8b 100644 --- a/src/rdf/data_type.rs +++ b/src/rdf/data_type.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- use { crate::{Error, Error::UnknownDataType}, diff --git a/src/rdf/lexical_value.rs b/src/rdf/lexical_value.rs index fb6cac2..914794a 100644 --- a/src/rdf/lexical_value.rs +++ b/src/rdf/lexical_value.rs @@ -1,21 +1,21 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- -use { - crate::{ - rdf::LexicalValueUnion, - DataType, - Error::{self, Unknown}, - Term, - }, - iref::{Iri, IriBuf}, - iri_string::types::IriReferenceString, - serde::{Serialize, Serializer}, - std::{ - fmt::{Debug, Display, Formatter}, - mem::ManuallyDrop, - str::FromStr, - }, +use std::{ + fmt::{Debug, Display, Formatter}, + mem::ManuallyDrop, + str::FromStr, +}; + +use iref::{Iri, IriBuf}; +use iri_string::types::IriReferenceString; +use serde::{Serialize, Serializer}; + +use crate::{ + rdf::LexicalValueUnion, + DataType, + Error::{self, Unknown}, + Term, }; #[derive(Default)] @@ -203,10 +203,7 @@ impl Clone for LexicalValue { todo!("the situation where the duration value is not a duration") } } else { - todo!( - "dealing with other datatypes: {:?}", - self.data_type - ) + todo!("dealing with other datatypes: {:?}", self.data_type) } } } @@ -278,9 +275,9 @@ impl LexicalValue { pub fn as_local_name(&self) -> Option { self.as_iri().as_ref().and_then(|iri| { let iri_str = iri.as_str(); - match regex::Regex::new(r#"(?:.*)[#/](.*)"#) { + match fancy_regex::Regex::new(r#"(?:.*)[#/](.*)"#) { Ok(re) => { - if let Some(captures) = re.captures(iri_str) { + if let Ok(Some(captures)) = re.captures(iri_str) { if let Some(mat) = captures.get(1) { Some(String::from(mat.as_str())) } else { @@ -416,7 +413,11 @@ impl LexicalValue { data_type, )?)) }, - _ => Err(Error::UnknownNTriplesValue { value: buffer.to_string() }), + _ => { + Err(Error::UnknownNTriplesValue { + value: buffer.to_string(), + }) + }, } }, DataType::String | DataType::PlainLiteral => { @@ -436,9 +437,10 @@ impl LexicalValue { DataType::Long | DataType::Short => { let signed_integer: i64 = buffer.parse().unwrap(); // TODO: Remove unwrap - Ok(Some( - LexicalValue::new_signed_integer_with_datatype(signed_integer, data_type)?, - )) + Ok(Some(LexicalValue::new_signed_integer_with_datatype( + signed_integer, + data_type, + )?)) }, DataType::PositiveInteger | DataType::NonNegativeInteger | @@ -447,9 +449,10 @@ impl LexicalValue { DataType::UnsignedShort | DataType::UnsignedLong => { let unsigned_integer: u64 = buffer.parse().unwrap(); // TODO: Remove unwrap - Ok(Some( - LexicalValue::new_unsigned_integer_with_datatype(unsigned_integer, data_type)?, - )) + Ok(Some(LexicalValue::new_unsigned_integer_with_datatype( + unsigned_integer, + data_type, + )?)) }, DataType::Decimal => { Ok(Some(LexicalValue::new_decimal_with_datatype( @@ -472,7 +475,9 @@ impl LexicalValue { pub fn from_iri(iri: &Iri) -> Result { Ok(LexicalValue { data_type: DataType::IriReference, - value: LexicalValueUnion { iri: ManuallyDrop::new(IriBuf::from(iri)) }, + value: LexicalValueUnion { + iri: ManuallyDrop::new(IriBuf::from(iri)), + }, }) } @@ -481,17 +486,11 @@ impl LexicalValue { } pub fn new_plain_literal_boolean(boolean: bool) -> Result { - Self::new_string_with_datatype( - boolean.to_string().as_str(), - DataType::PlainLiteral, - ) + Self::new_string_with_datatype(boolean.to_string().as_str(), DataType::PlainLiteral) } pub fn new_string_with_datatype(str: &str, data_type: DataType) -> Result { - assert!( - &data_type.is_string(), - "{data_type:?} is not a string type" - ); + assert!(&data_type.is_string(), "{data_type:?} is not a string type"); Ok(LexicalValue { data_type, value: LexicalValueUnion::new_string(str), @@ -499,10 +498,7 @@ impl LexicalValue { } pub fn new_date_time_with_datatype(str: &str, data_type: DataType) -> Result { - assert!( - &data_type.is_date_time(), - "{data_type:?} is not a dateTime" - ); + assert!(&data_type.is_date_time(), "{data_type:?} is not a dateTime"); Ok(LexicalValue { data_type, value: LexicalValueUnion::new_string(str), @@ -510,10 +506,7 @@ impl LexicalValue { } pub fn new_decimal_with_datatype(str: &str, data_type: DataType) -> Result { - assert!( - &data_type.is_decimal(), - "{data_type:?} is not a decimal" - ); + assert!(&data_type.is_decimal(), "{data_type:?} is not a decimal"); Ok(LexicalValue { data_type, value: LexicalValueUnion::new_string(str), @@ -521,10 +514,7 @@ impl LexicalValue { } pub fn new_duration_with_datatype(str: &str, data_type: DataType) -> Result { - assert!( - &data_type.is_duration(), - "{data_type:?} is not a duration" - ); + assert!(&data_type.is_duration(), "{data_type:?} is not a duration"); Ok(LexicalValue { data_type, value: LexicalValueUnion::new_string(str), @@ -540,11 +530,11 @@ impl LexicalValue { } pub fn new_iri_with_datatype(iri: &Iri, data_type: DataType) -> Result { - assert!( - &data_type.is_iri(), - "{data_type:?} is not an IRI type" - ); - Ok(LexicalValue { data_type, value: LexicalValueUnion::new_iri(iri) }) + assert!(&data_type.is_iri(), "{data_type:?} is not an IRI type"); + Ok(LexicalValue { + data_type, + value: LexicalValueUnion::new_iri(iri), + }) } pub fn new_blank_node_with_datatype(id: &str, data_type: DataType) -> Result { @@ -574,7 +564,10 @@ impl LexicalValue { "true" => Self::new_boolean_with_datatype(true, data_type), "false" => Self::new_boolean_with_datatype(false, data_type), &_ => { - Err(Error::UnknownValueForDataType { data_type, value: boolean_string.to_string() }) + Err(Error::UnknownValueForDataType { + data_type, + value: boolean_string.to_string(), + }) }, } } @@ -650,19 +643,11 @@ impl LexicalValue { } else if data_type.is_unsigned_integer() { write!(f, "{}", self.0.value.unsigned_integer)? } else if data_type.is_date_time() { - write!( - f, - "\"{}\"^^xsd:dateTime", - self.0.value.string.as_str() - )? + write!(f, "\"{}\"^^xsd:dateTime", self.0.value.string.as_str())? } else if data_type.is_decimal() { write!(f, "{}", self.0.value.string.as_str())? } else if data_type.is_duration() { - write!( - f, - "\"{}\"^^xsd:duration", - self.0.value.string.as_str() - )? + write!(f, "\"{}\"^^xsd:duration", self.0.value.string.as_str())? } else { panic!("Cannot format for turtle, unimplemented datatype {data_type:?}") } @@ -721,11 +706,11 @@ impl FromStr for LexicalValue { #[cfg(test)] mod tests { - use { - crate::{Error, LexicalValue}, - iref::IriBuf, - std::str::FromStr, - }; + use std::str::FromStr; + + use iref::IriBuf; + + use crate::{Error, LexicalValue}; #[test] fn test_as_local_name_01() -> Result<(), Error> { diff --git a/src/rdf/lexical_value_union.rs b/src/rdf/lexical_value_union.rs index 7e68374..7f66eb2 100644 --- a/src/rdf/lexical_value_union.rs +++ b/src/rdf/lexical_value_union.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- use std::mem::ManuallyDrop; diff --git a/src/rdf/mod.rs b/src/rdf/mod.rs index 9734312..95a9d8c 100644 --- a/src/rdf/mod.rs +++ b/src/rdf/mod.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- mod data_type; mod lexical_value; diff --git a/src/rdf/resource_value.rs b/src/rdf/resource_value.rs index 37730b8..ec80b5e 100644 --- a/src/rdf/resource_value.rs +++ b/src/rdf/resource_value.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- use super::DataType; use crate::{cursor, Error, Error::Unknown}; diff --git a/src/rdf/term.rs b/src/rdf/term.rs index 61c370d..1fa8ebc 100644 --- a/src/rdf/term.rs +++ b/src/rdf/term.rs @@ -1,6 +1,6 @@ use std::str::FromStr; -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- use iref::Iri; diff --git a/src/role_creds.rs b/src/role_creds.rs index b182271..afe239d 100644 --- a/src/role_creds.rs +++ b/src/role_creds.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- const RDFOX_DEFAULT_ROLE_USERID: &str = "admin"; diff --git a/src/server.rs b/src/server.rs index 74d6929..48d61c8 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- use std::{ diff --git a/src/server_connection/mod.rs b/src/server_connection/mod.rs index 7d0d57a..60bd0d2 100644 --- a/src/server_connection/mod.rs +++ b/src/server_connection/mod.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- pub use connection::ServerConnection; diff --git a/src/statement.rs b/src/statement.rs index d185efe..752e932 100644 --- a/src/statement.rs +++ b/src/statement.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- use core::fmt::{Display, Formatter}; @@ -81,18 +81,31 @@ impl Statement { pub fn no_comments(string: &str) -> String { use std::fmt::Write; - use regex::Regex; - let re = Regex::new(r"(.*)#.*$").unwrap(); - let mut output = String::new(); - for line in string.lines() { + let re = fancy_regex::Regex::new(r"(.*)(?!#>)#.*$").unwrap(); + + let do_line = |line: &str| -> (bool, String) { let caps = re.captures(line); - if let Some(caps) = caps { + if let Ok(Some(caps)) = caps { let mat = caps.get(1).unwrap(); - let line = line[mat.start() .. mat.end()].trim_end(); - write!(&mut output, "{line}\n").unwrap(); + (true, line[mat.start() .. mat.end()].trim_end().to_string()) } else { - let line = line.trim_end(); - write!(&mut output, "{line}\n").unwrap(); + (false, line.trim_end().to_string()) + } + }; + + let mut output = String::new(); + for line in string.lines() { + let mut line = line.to_string(); + loop { + let (again, result) = do_line(line.as_str()); + if again { + // Repeat the call to do_line again to make sure that all #-comments are removed + // (there could be multiple on one line) + line = result; + } else { + write!(&mut output, "{result}\n").unwrap(); + break + } } } output @@ -107,10 +120,12 @@ mod tests { #[test_log::test] fn test_no_comments() { let sparql = formatdoc! {r##" + PREFIX abc: # focus on this and the next line + PREFIX owl: SELECT DISTINCT ?thing WHERE {{ {{ # some comment - GRAPH ?graph {{ # more + GRAPH ?graph {{ # more # and more ?thing a Whatever# }} }} UNION {{ @@ -122,6 +137,8 @@ mod tests { "## }; let expected = formatdoc! {r##" + PREFIX abc: + PREFIX owl: SELECT DISTINCT ?thing WHERE {{ {{ @@ -136,7 +153,7 @@ mod tests { }} "## }; - let result = no_comments(sparql.as_str()); - assert_eq!(result.as_str(), expected.as_str()); + let actual = no_comments(sparql.as_str()); + assert_eq!(actual.as_str(), expected.as_str()); } } diff --git a/src/streamer.rs b/src/streamer.rs index bbaf497..1a4a816 100644 --- a/src/streamer.rs +++ b/src/streamer.rs @@ -11,6 +11,7 @@ use mime::Mime; use crate::{ cursor::ptr_to_cstr, database_call, + prefix::Prefix, root::{ CDataStoreConnection, CDataStoreConnection_evaluateStatement, @@ -20,7 +21,6 @@ use crate::{ DataStoreConnection, Error, Parameters, - Prefix, Statement, }; diff --git a/src/transaction.rs b/src/transaction.rs index b7ecfc3..c2c9908 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. +// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved. //--------------------------------------------------------------- use {