#![cfg_attr(not(test), warn(missing_docs))]
use std::{
- borrow::{Borrow, Cow},
+ borrow::{Borrow, BorrowMut, Cow},
cmp::Ordering,
fmt::{Debug, Display, Formatter},
hash::Hash,
_ => self == other,
}
}
+
+ pub fn as_encoded<'a>(&'a self, encoding: &'static Encoding) -> EncodedDat<'a> {
+ match self {
+ Datum::Number(number) => EncodedDat::Number(*number),
+ Datum::String(raw_string) => {
+ EncodedDat::String(raw_string.borrow().as_encoded(encoding))
+ }
+ }
+ }
}
-impl Datum<RawString> {
+impl<B> Datum<B>
+where
+ B: BorrowMut<RawString>,
+{
/// Returns the string inside this datum as a mutable borrow, or `None` if
/// this is a numeric datum.
pub fn as_string_mut(&mut self) -> Option<&mut RawString> {
match self {
Self::Number(_) => None,
- Self::String(s) => Some(s),
+ Self::String(s) => Some(s.borrow_mut()),
}
}
- pub fn as_encoded<'a>(&'a self, encoding: &'static Encoding) -> EncodedDat<'a> {
- match self {
- Datum::Number(number) => EncodedDat::Number(*number),
- Datum::String(raw_string) => EncodedDat::String(raw_string.as_encoded(encoding)),
- }
+ /// Removes trailing ASCII spaces from this datum, if it is a string.
+ pub fn trim_end(&mut self) {
+ self.as_string_mut().map(|s| s.trim_end());
}
/// Resizes this datum to the given `width`. Returns an error, without
match (self, width) {
(Self::Number(_), VarWidth::Numeric) => Ok(()),
(Self::String(s), VarWidth::String(new_width)) => {
+ let s = s.borrow_mut();
if s.is_resizable(new_width as usize) {
s.resize(new_width as usize);
Ok(())
_ => Err(()),
}
}
+}
- /// Removes trailing ASCII spaces from this datum, if it is a string.
- pub fn trim_end(&mut self) {
- match self {
- Self::Number(_) => (),
- Self::String(s) => s.trim_end(),
- }
- }
-
+impl Datum<RawString> {
pub fn with_encoding(self, encoding: &'static Encoding) -> EncodedDatum {
match self {
Datum::Number(number) => EncodedDatum::Number(number),