aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAleksey Kuznetsov2016-04-03 20:00:53 +0600
committerEd Barnard2016-04-03 16:00:53 +0200
commit446f4ed641be02c5478ddfcb0b38590254f4a6e4 (patch)
tree0817abe77a36a1673786df04fb835f95c2193355
parent80a61a168dd64f432bbdbcc7cae47ad3cdea1b6f (diff)
downloadrust-plist-446f4ed641be02c5478ddfcb0b38590254f4a6e4.tar.bz2
Implement into_data and into_string methods for Plist
-rw-r--r--src/lib.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index a23f09c..4d6ccab 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -199,6 +199,18 @@ impl Plist {
}
}
+ /// If the `Plist` is a Data, returns the underlying Vec.
+ /// Returns None otherwise.
+ ///
+ /// This method consumes the `Plist`. If this is not desired, please use
+ /// `as_data` method.
+ pub fn into_data(self) -> Option<Vec<u8>> {
+ match self {
+ Plist::Data(data) => Some(data),
+ _ => None,
+ }
+ }
+
/// If the `Plist` is a Data, returns the associated Vec.
/// Returns None otherwise.
pub fn as_data(&self) -> Option<&[u8]> {
@@ -235,6 +247,18 @@ impl Plist {
}
}
+ /// If the `Plist` is a String, returns the underlying String.
+ /// Returns None otherwise.
+ ///
+ /// This method consumes the `Plist`. If this is not desired, please use
+ /// `as_string` method.
+ pub fn into_string(self) -> Option<String> {
+ match self {
+ Plist::String(v) => Some(v),
+ _ => None,
+ }
+ }
+
/// If the `Plist` is a String, returns the associated str.
/// Returns None otherwise.
pub fn as_string(&self) -> Option<&str> {
@@ -413,6 +437,7 @@ mod tests {
let slice: &[u8] = &[1, 2, 3];
assert_eq!(Plist::Data(slice.to_vec()).as_data(), Some(slice));
+ assert_eq!(Plist::Data(slice.to_vec()).into_data(), Some(slice.to_vec()));
let date: DateTime<UTC> = UTC::now();
assert_eq!(Plist::Date(date).as_date(), Some(date));
@@ -420,5 +445,6 @@ mod tests {
assert_eq!(Plist::Real(0.0).as_real(), Some(0.0));
assert_eq!(Plist::Integer(1).as_integer(), Some(1));
assert_eq!(Plist::String("2".to_owned()).as_string(), Some("2"));
+ assert_eq!(Plist::String("t".to_owned()).into_string(), Some("t".to_owned()));
}
}