oxedyne/fe2o3/fe2o3_text/src/core.rs
952 bytes, 3 runs
created by r1870400018:1097, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use oxedyne_fe2o3_core::prelude::*; |
| 2 | |
| 3 | use std::fmt; |
| 4 | |
| 5 | |
| 6 | #[derive(Clone, Debug, Default)] |
| 7 | pub struct Text<T: Clone + fmt::Debug + Default> { |
| 8 | pub typ: T, |
| 9 | pub txt: String, |
| 10 | } |
| 11 | |
| 12 | impl< |
| 13 | T: Clone + fmt::Debug + Default |
| 14 | > |
| 15 | fmt::Display for Text<T> |
| 16 | { |
| 17 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 18 | write!(f, "{}", self.txt) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | impl< |
| 23 | T: Clone + fmt::Debug + Default |
| 24 | > |
| 25 | From<&str> for Text<T> |
| 26 | { |
| 27 | fn from(s: &str) -> Self { |
| 28 | Self::new(s, None) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | impl< |
| 33 | T: Clone + fmt::Debug + Default |
| 34 | > |
| 35 | Text<T> |
| 36 | { |
| 37 | pub fn new<S: Into<String>>(txt: S, typ: Option<T>) -> Self { |
| 38 | Self { |
| 39 | typ: if let Some(typ) = typ { typ } else { T::default() }, |
| 40 | txt: txt.into(), |
| 41 | } |
| 42 | } |
| 43 | pub fn typ(&self) -> &T { |
| 44 | &self.typ |
| 45 | } |
| 46 | pub fn len(&self) -> usize { |
| 47 | self.txt.chars().count() |
| 48 | } |
| 49 | pub fn is_empty(&self) -> bool { |
| 50 | self.txt.is_empty() |
| 51 | } |
| 52 | } |