Skip to content

Commit

Permalink
impl Display for Object
Browse files Browse the repository at this point in the history
  • Loading branch information
dginev committed Aug 1, 2019
1 parent 08cdbd7 commit 2bdb367
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/xpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::cell::RefCell;
use std::ffi::{CStr, CString};
use std::rc::Rc;
use std::str;
use std::fmt;

///Thinly wrapped libxml2 xpath context
pub(crate) type ContextRef = Rc<RefCell<_Context>>;
Expand All @@ -36,6 +37,7 @@ pub struct Context {
}

///Essentially, the result of the evaluation of some xpath expression
#[derive(Debug)]
pub struct Object {
///libxml's `ObjectPtr`
pub ptr: xmlXPathObjectPtr,
Expand Down Expand Up @@ -236,15 +238,17 @@ impl Object {
}
vec
}
}

impl fmt::Display for Object {
/// use if the XPath used was meant to return a string, such as string(//foo/@attr)
pub fn to_string(&self) -> String {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
unsafe {
let receiver = xmlXPathCastToString(self.ptr);
let c_string = CStr::from_ptr(receiver as *const c_char);
let rust_string = str::from_utf8(c_string.to_bytes()).unwrap().to_owned();
libc::free(receiver as *mut c_void);
rust_string
write!(f, "{}", rust_string)
}
}
}
}

0 comments on commit 2bdb367

Please sign in to comment.