Skip to content

Commit

Permalink
rename to is_well_formed_xpath; fmt+lint
Browse files Browse the repository at this point in the history
  • Loading branch information
dginev committed Mar 26, 2022
1 parent 8d0617a commit 185c719
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 106 deletions.
55 changes: 34 additions & 21 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
fn main() {
if let Ok(ref s) = std::env::var("LIBXML2") {
// println!("{:?}", std::env::vars());
// panic!("set libxml2.");
let p = std::path::Path::new(s);
let fname = std::path::Path::new(p.file_name().expect("no file name in LIBXML2 env"));
assert!(p.is_file());
println!("cargo:rustc-link-lib={}", fname.file_stem().unwrap().to_string_lossy().strip_prefix("lib").unwrap());
println!("cargo:rustc-link-search={}", p.parent().expect("no library path in LIBXML2 env").to_string_lossy());
// println!("{:?}", std::env::vars());
// panic!("set libxml2.");
let p = std::path::Path::new(s);
let fname = std::path::Path::new(p.file_name().expect("no file name in LIBXML2 env"));
assert!(p.is_file());
println!(
"cargo:rustc-link-lib={}",
fname
.file_stem()
.unwrap()
.to_string_lossy()
.strip_prefix("lib")
.unwrap()
);
println!(
"cargo:rustc-link-search={}",
p.parent()
.expect("no library path in LIBXML2 env")
.to_string_lossy()
);
} else {
#[cfg(any(target_family = "unix", target_os = "macos"))]
{
if pkg_config_dep::find() {
return;
}
}
{
if pkg_config_dep::find() {
return;
}
}

#[cfg(windows)]
{
if vcpkg_dep::find() {
return;
}
}
{
if vcpkg_dep::find() {
return;
}
}

panic!("Could not find libxml2.")
}
}

#[cfg(any(target_family="unix", target_os="macos"))]
#[cfg(any(target_family = "unix", target_os = "macos"))]
mod pkg_config_dep {
pub fn find() -> bool {
if pkg_config::find_library("libxml-2.0").is_ok() {
Expand All @@ -36,12 +49,12 @@ mod pkg_config_dep {
}
}

#[cfg(target_family="windows")]
#[cfg(target_family = "windows")]
mod vcpkg_dep {
pub fn find() -> bool {
if vcpkg::find_package("libxml2").is_ok() {
return true
return true;
}
false
}
}
}
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
//! Providing a more or less complete wrapper would be too much work.
#![deny(missing_docs)]
// Our new methods return Result<Self, _> types
#![allow(clippy::new_ret_no_self)]

#![allow(clippy::new_ret_no_self, clippy::result_unit_err)]
/// Bindings to the C interface
pub mod bindings;
mod c_helpers;
Expand Down
92 changes: 44 additions & 48 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,8 @@ pub struct ParserOptions<'a> {
pub encoding: Option<&'a str>,
}



impl<'a> ParserOptions<'a> {
pub(crate) fn to_flags(&self, format: &ParseFormat) -> i32 {

macro_rules! to_option_flag {
(
$condition:expr => $variant:ident
Expand Down Expand Up @@ -103,21 +100,21 @@ impl<'a> ParserOptions<'a> {
}

impl<'a> Default for ParserOptions<'a> {
fn default() -> Self {
ParserOptions {
recover: true,
no_def_dtd: false,
no_error: true,
no_warning: true,
pedantic: false,
no_blanks: false,
no_net: false,
no_implied: false,
compact: false,
ignore_enc: false,
encoding: None,
}
fn default() -> Self {
ParserOptions {
recover: true,
no_def_dtd: false,
no_error: true,
no_warning: true,
pedantic: false,
no_blanks: false,
no_net: false,
no_implied: false,
compact: false,
ignore_enc: false,
encoding: None,
}
}
}

///Parser Errors
Expand All @@ -132,22 +129,23 @@ pub enum XmlParseError {

impl Error for XmlParseError {}

impl fmt::Debug for XmlParseError
{
impl fmt::Debug for XmlParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self)
}
}


impl fmt::Display for XmlParseError
{
impl fmt::Display for XmlParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", match self {
XmlParseError::GotNullPointer => "Got a Null pointer",
XmlParseError::FileOpenError => "Unable to open path to file.",
XmlParseError::DocumentTooLarge => "Document too large for i32.",
})
write!(
f,
"{}",
match self {
XmlParseError::GotNullPointer => "Got a Null pointer",
XmlParseError::FileOpenError => "Unable to open path to file.",
XmlParseError::DocumentTooLarge => "Document too large for i32.",
}
)
}
}

Expand Down Expand Up @@ -249,7 +247,8 @@ impl Parser {
};

// Process encoding.
let encoding_cstring: Option<CString> = parser_options.encoding.map(|v| CString::new(v).unwrap());
let encoding_cstring: Option<CString> =
parser_options.encoding.map(|v| CString::new(v).unwrap());
let encoding_ptr = match encoding_cstring {
Some(v) => v.as_ptr(),
None => DEFAULT_ENCODING,
Expand All @@ -263,28 +262,24 @@ impl Parser {
}

let options = parser_options.to_flags(&self.format);

match self.format {
ParseFormat::XML => {
unsafe {
let doc_ptr = xmlReadIO(ioread, ioclose, ioctx, url_ptr, encoding_ptr, options);
if doc_ptr.is_null() {
Err(XmlParseError::GotNullPointer)
} else {
Ok(Document::new_ptr(doc_ptr))
}
ParseFormat::XML => unsafe {
let doc_ptr = xmlReadIO(ioread, ioclose, ioctx, url_ptr, encoding_ptr, options);
if doc_ptr.is_null() {
Err(XmlParseError::GotNullPointer)
} else {
Ok(Document::new_ptr(doc_ptr))
}
}
ParseFormat::HTML => {
unsafe {
let doc_ptr = htmlReadIO(ioread, ioclose, ioctx, url_ptr, encoding_ptr, options);
if doc_ptr.is_null() {
Err(XmlParseError::GotNullPointer)
} else {
Ok(Document::new_ptr(doc_ptr))
}
},
ParseFormat::HTML => unsafe {
let doc_ptr = htmlReadIO(ioread, ioclose, ioctx, url_ptr, encoding_ptr, options);
if doc_ptr.is_null() {
Err(XmlParseError::GotNullPointer)
} else {
Ok(Document::new_ptr(doc_ptr))
}
}
},
}
}

Expand All @@ -306,7 +301,8 @@ impl Parser {
let input_len = try_usize_to_i32(input_bytes.len())?;

// Process encoding.
let encoding_cstring: Option<CString> = parser_options.encoding.map(|v| CString::new(v).unwrap());
let encoding_cstring: Option<CString> =
parser_options.encoding.map(|v| CString::new(v).unwrap());
let encoding_ptr = match encoding_cstring {
Some(v) => v.as_ptr(),
None => DEFAULT_ENCODING,
Expand Down
1 change: 0 additions & 1 deletion src/readonly/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ impl RoNode {
}
}


/// Returns all child nodes of the given node as a vector
pub fn get_child_nodes(self) -> Vec<RoNode> {
let mut children = Vec::new();
Expand Down
2 changes: 1 addition & 1 deletion src/schemas/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::tree::node::Node;
use crate::error::StructuredError;

use std::ffi::CString;
use std::os::raw::{c_char};
use std::os::raw::c_char;

/// Wrapper on xmlSchemaValidCtxt
pub struct SchemaValidationContext {
Expand Down
6 changes: 5 additions & 1 deletion src/tree/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ pub struct Namespace {

impl Namespace {
/// Creates a new namespace
pub fn new(prefix: &str, href: &str, node: &mut Node) -> Result<Self, Box<dyn Error + Send + Sync>> {
pub fn new(
prefix: &str,
href: &str,
node: &mut Node,
) -> Result<Self, Box<dyn Error + Send + Sync>> {
let c_href = CString::new(href).unwrap();
let c_prefix = CString::new(prefix).unwrap();
let c_prefix_ptr = if prefix.is_empty() {
Expand Down
60 changes: 47 additions & 13 deletions src/tree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,10 @@ impl Node {
}

/// Add a previous sibling
pub fn add_prev_sibling(&mut self, new_sibling: &mut Node) -> Result<(), Box<dyn Error + Send + Sync>> {
pub fn add_prev_sibling(
&mut self,
new_sibling: &mut Node,
) -> Result<(), Box<dyn Error + Send + Sync>> {
new_sibling.set_linked();
unsafe {
if xmlAddPrevSibling(self.node_ptr_mut()?, new_sibling.node_ptr_mut()?).is_null() {
Expand All @@ -347,7 +350,10 @@ impl Node {
}

/// Add a next sibling
pub fn add_next_sibling(&mut self, new_sibling: &mut Node) -> Result<(), Box<dyn Error + Send + Sync>> {
pub fn add_next_sibling(
&mut self,
new_sibling: &mut Node,
) -> Result<(), Box<dyn Error + Send + Sync>> {
new_sibling.set_linked();
unsafe {
if xmlAddNextSibling(self.node_ptr_mut()?, new_sibling.node_ptr_mut()?).is_null() {
Expand Down Expand Up @@ -470,8 +476,13 @@ impl Node {
pub fn has_property_ns(&self, name: &str, ns: &str) -> bool {
let c_name = CString::new(name).unwrap();
let c_ns = CString::new(ns).unwrap();
let value_ptr =
unsafe { xmlHasNsProp(self.node_ptr(), c_name.as_bytes().as_ptr(), c_ns.as_bytes().as_ptr()) };
let value_ptr = unsafe {
xmlHasNsProp(
self.node_ptr(),
c_name.as_bytes().as_ptr(),
c_ns.as_bytes().as_ptr(),
)
};
!value_ptr.is_null()
}
/// Alias for has_property
Expand All @@ -484,7 +495,11 @@ impl Node {
}

/// Sets the value of property `name` to `value`
pub fn set_property(&mut self, name: &str, value: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
pub fn set_property(
&mut self,
name: &str,
value: &str,
) -> Result<(), Box<dyn Error + Send + Sync>> {
let c_name = CString::new(name).unwrap();
let c_value = CString::new(value).unwrap();
unsafe {
Expand Down Expand Up @@ -540,14 +555,18 @@ impl Node {
}

/// Removes the property of given `name` and namespace (`ns`)
pub fn remove_property_ns(&mut self, name: &str, ns: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
pub fn remove_property_ns(
&mut self,
name: &str,
ns: &str,
) -> Result<(), Box<dyn Error + Send + Sync>> {
let c_name = CString::new(name).unwrap();
let c_ns = CString::new(ns).unwrap();
unsafe {
let attr_node = xmlHasNsProp(
self.node_ptr_mut()?,
c_name.as_bytes().as_ptr(),
c_ns.as_bytes().as_ptr(),
self.node_ptr_mut()?,
c_name.as_bytes().as_ptr(),
c_ns.as_bytes().as_ptr(),
);
if !attr_node.is_null() {
let remove_prop_status = xmlRemoveProp(attr_node);
Expand Down Expand Up @@ -582,7 +601,11 @@ impl Node {
}

/// Alias for set_property
pub fn set_attribute(&mut self, name: &str, value: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
pub fn set_attribute(
&mut self,
name: &str,
value: &str,
) -> Result<(), Box<dyn Error + Send + Sync>> {
self.set_property(name, value)
}
/// Alias for set_property_ns
Expand All @@ -601,7 +624,11 @@ impl Node {
}

/// Alias for remove_property_ns
pub fn remove_attribute_ns(&mut self, name: &str, ns: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
pub fn remove_attribute_ns(
&mut self,
name: &str,
ns: &str,
) -> Result<(), Box<dyn Error + Send + Sync>> {
self.remove_property_ns(name, ns)
}

Expand Down Expand Up @@ -691,7 +718,10 @@ impl Node {
}

/// Sets a `Namespace` for the node
pub fn set_namespace(&mut self, namespace: &Namespace) -> Result<(), Box<dyn Error + Send + Sync>> {
pub fn set_namespace(
&mut self,
namespace: &Namespace,
) -> Result<(), Box<dyn Error + Send + Sync>> {
unsafe {
xmlSetNs(self.node_ptr_mut()?, namespace.ns_ptr());
}
Expand Down Expand Up @@ -775,7 +805,11 @@ impl Node {
}

/// Creates a new `Node` as child to the self `Node`
pub fn new_child(&mut self, ns: Option<Namespace>, name: &str) -> Result<Node, Box<dyn Error + Send + Sync>> {
pub fn new_child(
&mut self,
ns: Option<Namespace>,
name: &str,
) -> Result<Node, Box<dyn Error + Send + Sync>> {
let c_name = CString::new(name).unwrap();
let ns_ptr = match ns {
None => ptr::null_mut(),
Expand Down

0 comments on commit 185c719

Please sign in to comment.