Add Value::get_uint() to handle BYTE, SHORT, and LONG transparently.
This commit is contained in:
parent
8f2ece2b01
commit
7c61ddf3f9
32
src/value.rs
32
src/value.rs
|
@ -76,6 +76,18 @@ impl<'a> Value<'a> {
|
||||||
pub fn display_as(&self, tag: ::tag_priv::Tag) -> Display {
|
pub fn display_as(&self, tag: ::tag_priv::Tag) -> Display {
|
||||||
::tag_priv::display_value_as(self, tag)
|
::tag_priv::display_value_as(self, tag)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the unsigned integer at the given position.
|
||||||
|
/// None is returned if the value type is not unsigned integer
|
||||||
|
/// (BYTE, SHORT, or LONG) or the position is out of bounds.
|
||||||
|
pub fn get_uint(&self, index: usize) -> Option<u32> {
|
||||||
|
match *self {
|
||||||
|
Value::Byte(ref v) if v.len() > index => Some(v[index] as u32),
|
||||||
|
Value::Short(ref v) if v.len() > index => Some(v[index] as u32),
|
||||||
|
Value::Long(ref v) if v.len() > index => Some(v[index]),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper struct for printing a value in a tag-specific format.
|
/// Helper struct for printing a value in a tag-specific format.
|
||||||
|
@ -596,6 +608,26 @@ mod tests {
|
||||||
assert_eq!(unitlen, 0);
|
assert_eq!(unitlen, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn get_uint() {
|
||||||
|
let v = Value::Byte(vec![1, 2]);
|
||||||
|
assert_eq!(v.get_uint(0), Some(1));
|
||||||
|
assert_eq!(v.get_uint(1), Some(2));
|
||||||
|
assert_eq!(v.get_uint(2), None);
|
||||||
|
let v = Value::Short(vec![1, 2]);
|
||||||
|
assert_eq!(v.get_uint(0), Some(1));
|
||||||
|
assert_eq!(v.get_uint(1), Some(2));
|
||||||
|
assert_eq!(v.get_uint(2), None);
|
||||||
|
let v = Value::Long(vec![1, 2]);
|
||||||
|
assert_eq!(v.get_uint(0), Some(1));
|
||||||
|
assert_eq!(v.get_uint(1), Some(2));
|
||||||
|
assert_eq!(v.get_uint(2), None);
|
||||||
|
let v = Value::SLong(vec![1, 2]);
|
||||||
|
assert_eq!(v.get_uint(0), None);
|
||||||
|
assert_eq!(v.get_uint(1), None);
|
||||||
|
assert_eq!(v.get_uint(2), None);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn rational_fmt_display() {
|
fn rational_fmt_display() {
|
||||||
let r = Rational { num: u32::max_value(), denom: u32::max_value() };
|
let r = Rational { num: u32::max_value(), denom: u32::max_value() };
|
||||||
|
|
Loading…
Reference in New Issue