Skip to content

Commit

Permalink
feat: parse time object into f64 and Duration
Browse files Browse the repository at this point in the history
  • Loading branch information
andoriyu committed Mar 16, 2020
1 parent 3c9e4ac commit 40f6bd4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//! an_integer = 69420
//! is_it_good = yes
//! buffer_size = 1KB
//! interval = 1s
//! "#;
//! parser.add_chunk_full(input, Priority::default(), DEFAULT_DUPLICATE_STRATEGY).unwrap();
//! let result = parser.get_object().unwrap();
Expand All @@ -32,6 +33,8 @@
//!
//! let lookup_result = result.lookup("buffer_size").unwrap().as_i64().unwrap();
//! assert_eq!(lookup_result, 1024);
//! let lookup_result = result.lookup("interval").unwrap().as_time().unwrap();
//! assert_eq!(lookup_result, 1.0f64);
//! ```
//!
//! In order to get around rust rules library implemets its own trait FromObject for some basic types:
Expand Down Expand Up @@ -60,6 +63,7 @@
//! use std::path::PathBuf;
//! use std::net::SocketAddr;
//! use std::collections::HashMap;
//! use std::time::Duration;
//!
//! #[derive(Debug,Uclicious)]
//! #[ucl(var(name = "test", value = "works"))]
Expand All @@ -80,6 +84,7 @@
//! #[ucl(default)]
//! option: Option<String>,
//! gates: HashMap<String, bool>,
//! interval: Duration,
//! }
//!
//! #[derive(Debug,Uclicious)]
Expand All @@ -102,6 +107,7 @@
//! subsection {
//! host = [host1, host2]
//! }
//! interval = 10ms
//! gates {
//! feature_1 = on
//! feature_2 = off
Expand Down
34 changes: 31 additions & 3 deletions src/raw/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use std::net::{AddrParseError, SocketAddr};
use std::num::TryFromIntError;
use std::ops::{Deref, DerefMut};
use std::path::PathBuf;
use bitflags::_core::hash::BuildHasher;
use std::hash::BuildHasher;
use std::time::Duration;

/// Errors that could be returned by `Object` or `ObjectRef` functions.
#[derive(Eq, PartialEq, Debug, Clone)]
Expand Down Expand Up @@ -213,6 +214,11 @@ impl ObjectRef {
self.kind == ucl_type_t::UCL_ARRAY
}

/// Returns `true` if this object is a time/duration.
pub fn is_time(&self) -> bool {
self.kind == ucl_type_t::UCL_TIME
}

/// Get priority assigned to the object.
pub fn priority(&self) -> Priority {
let out = unsafe { ucl_object_get_priority(self.object) };
Expand Down Expand Up @@ -279,9 +285,17 @@ impl ObjectRef {
}
}

/// Return a float value or None.
/// Return a float number of seconds. Only works if object is time.
pub fn as_time(&self) -> Option<f64> {
if !self.is_time() {
return None;
}
self.as_f64()
}

/// Return a float value or None. This function also works on time object.
pub fn as_f64(&self) -> Option<f64> {
if !self.is_float() {
if !(self.is_float() || self.is_time()) {
return None;
}
let mut ptr = MaybeUninit::zeroed();
Expand Down Expand Up @@ -623,6 +637,20 @@ where
}
}

impl FromObject<ObjectRef> for Duration {
fn try_from(value: ObjectRef) -> Result<Self, ObjectError> {
if let Some(seconds) = value.as_time() {
Ok(Duration::from_secs_f64(seconds))
} else {
return Err(ObjectError::WrongType {
key: value.key().unwrap_or_default(),
actual_type: value.kind,
wanted_type: ucl_type_t::UCL_TIME,
});
}
}
}

impl fmt::Debug for ObjectRef {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let ptr = unsafe { ucl_object_tostring_forced(self.as_ptr()) };
Expand Down

0 comments on commit 40f6bd4

Please sign in to comment.