Compare commits

...
This repository has been archived on 2024-07-25. You can view files and clone it, but cannot push or open issues or pull requests.

2 commits

8 changed files with 265 additions and 26 deletions

View file

@ -2,11 +2,49 @@
//! string types
//!
// TODO: add more info and examples.
/// Format a value fragment into a ``EString``.
pub trait FormatFragment {
/// Format this type and wrap into ``EString``.
///
/// ``ToEString``s `to_estring` method is often used implicitly, through ``EString``s from.
///
/// # Examples
///
/// Basic implementation of ``ToEString`` on an example ``Point``.
///
/// ```rust
/// use std::fmt::Write;
/// use estring::{EString, ToEString};
///
/// #[derive(Debug, PartialEq)]
/// struct Point {
/// x: i32,
/// y: i32,
/// }
///
/// impl ToEString for Point {
/// fn to_estring(&self) -> EString {
/// let mut res = String::new();
/// write!(res, "({},{})", self.x, self.y).ok().expect("Cannot format Point into EString");
/// EString(res)
/// }
/// }
///
/// let point = Point { x: 1, y: 2 };
/// assert_eq!(point.to_estring(), EString::from("(1,2)"));
/// ```
///
pub trait ToEString {
/// Format this type and returns ``EString``.
fn fmt_frag(&self) -> EString;
///
/// # Examples
///
/// ```rust
/// use estring::{EString, ToEString};
///
/// let i = 5;
/// let five = EString::from(5);
/// assert_eq!(five, i.to_estring());
/// ```
fn to_estring(&self) -> EString;
}
/// Parse a value fragment from a ``EString``.
@ -70,11 +108,54 @@ pub trait ParseFragment: Sized {
fn parse_frag(es: EString) -> crate::Result<Self>;
}
/// Wrapper under String type.
/// Wrapper under ``String`` type.
///
/// # Examples
///
/// You can create a ``EString`` from a any type that implement ``ToEString`` with ``EString::from``
///
/// ```rust
/// # use estring::EString;
/// let hello = EString::from("Hello, world");
/// let num = EString::from("999");
/// ```
///
/// You can use ``ToEString::to_estring`` directly on the type.
///
/// ```rust
/// # use estring::ToEString;
/// let some_opt = Some(999).to_estring();
/// let none_opt = None::<i32>.to_estring();
/// ```
///
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct EString(pub String);
impl std::fmt::Display for EString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl EString {
/// Creates a new empty ``EString``.
///
/// This will not allocate any inital buffer.
///
/// # Examples
///
/// Basic usage:
///
/// ```rust
/// # use estring::EString;
/// let s = EString::new();
/// ```
#[must_use]
#[inline]
pub fn new() -> Self {
Self(String::new())
}
/// Parses this inner string into another type.
///
/// `parse` can parse into any type that implements the ``ParseFragment`` trait.
@ -110,11 +191,11 @@ impl EString {
impl<T> From<T> for EString
where
T: std::fmt::Display,
T: ToEString,
{
#[inline]
fn from(val: T) -> Self {
Self(val.to_string())
val.to_estring()
}
}
@ -129,22 +210,36 @@ impl std::ops::Deref for EString {
impl ParseFragment for EString {
#[inline]
fn parse_frag(value: EString) -> crate::Result<Self> {
Ok(value)
fn parse_frag(es: EString) -> crate::Result<Self> {
Ok(es)
}
}
impl ParseFragment for String {
#[inline]
fn parse_frag(s: EString) -> crate::Result<Self> {
Ok(s.0)
fn parse_frag(es: EString) -> crate::Result<Self> {
Ok(es.0)
}
}
impl ToEString for String {
#[inline]
fn to_estring(&self) -> EString {
EString(self.clone())
}
}
impl ParseFragment for &'static str {
#[inline]
fn parse_frag(s: EString) -> crate::Result<Self> {
Ok(Box::leak(s.0.into_boxed_str()))
fn parse_frag(es: EString) -> crate::Result<Self> {
Ok(Box::leak(es.0.into_boxed_str()))
}
}
impl<'a> ToEString for &'a str {
#[inline]
fn to_estring(&self) -> EString {
EString((*self).to_string())
}
}

View file

@ -1,4 +1,4 @@
use crate::{core::EString, ParseFragment};
use crate::core::{EString, ParseFragment, ToEString};
/// Wrapper that allow to trim substring before continue
///
@ -42,6 +42,15 @@ where
}
}
impl<T> ToEString for Trim<T>
where
T: ToEString,
{
fn to_estring(&self) -> EString {
self.0.to_estring()
}
}
#[cfg(test)]
mod tests {
use super::*;

View file

@ -1,4 +1,4 @@
use crate::core::{EString, ParseFragment};
use crate::core::{EString, ParseFragment, ToEString};
use crate::error::{Error, Reason};
impl ParseFragment for bool {
@ -12,6 +12,13 @@ impl ParseFragment for bool {
}
}
impl ToEString for bool {
#[inline]
fn to_estring(&self) -> EString {
EString(self.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
@ -52,4 +59,10 @@ mod tests {
_ => unreachable!(),
};
}
#[test]
fn should_format_bool() {
assert_eq!(true.to_estring(), EString(String::from("true")));
assert_eq!(false.to_estring(), EString(String::from("false")));
}
}

View file

@ -1,4 +1,4 @@
use crate::core::{EString, ParseFragment};
use crate::core::{EString, ParseFragment, ToEString};
use crate::error::{Error, Reason};
#[doc(hidden)]
@ -11,6 +11,13 @@ macro_rules! from_env_string_numbers_impl {
s.0.parse::<Self>().map_err(|_| Error(s, Reason::Parse))
}
}
impl ToEString for $ty {
#[inline]
fn to_estring(&self) -> EString {
EString(self.to_string())
}
}
)+
};
}
@ -56,4 +63,11 @@ mod tests {
_ => unreachable!(),
};
}
#[test]
fn should_format_number() {
assert_eq!((-1).to_estring(), EString(String::from("-1")));
assert_eq!(10.to_estring(), EString(String::from("10")));
assert_eq!(1.1.to_estring(), EString(String::from("1.1")));
}
}

View file

@ -1,4 +1,16 @@
use crate::core::{EString, ParseFragment};
use crate::core::{EString, ParseFragment, ToEString};
impl<T> ToEString for Option<T>
where
T: ToEString,
{
fn to_estring(&self) -> EString {
match self {
Some(inner) => inner.to_estring(),
None => EString::new(),
}
}
}
impl<T> ParseFragment for Option<T>
where
@ -20,7 +32,7 @@ mod tests {
#[test]
fn should_parse_empty_string_as_none() {
let estr = EString::from("");
let estr = EString::new();
match estr.parse::<Option<i32>>() {
Ok(res) => assert_eq!(res, None),
_ => unreachable!(),
@ -44,4 +56,10 @@ mod tests {
_ => unreachable!(),
}
}
#[test]
fn should_format_option() {
assert_eq!(None::<i32>.to_estring(), EString::new());
assert_eq!(Some(99).to_estring(), EString(String::from("99")));
}
}

View file

@ -1,7 +1,7 @@
//! Contains the implementations to pair tuple type
//!
use crate::core::{EString, ParseFragment};
use crate::core::{EString, ParseFragment, ToEString};
use crate::{Error, Reason};
use std::fmt::Write;
@ -39,13 +39,25 @@ where
B: std::fmt::Display,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0.to_string())?;
f.write_char(S1)?;
f.write_str(&self.1.to_string())
write!(f, "{}{}{}", self.0, S1, self.1)
}
}
impl<A, const S1: char, B> ParseFragment for Pair<A, S1, B>
impl<A, B, const S1: char> ToEString for Pair<A, S1, B>
where
A: ToEString,
B: ToEString,
{
fn to_estring(&self) -> EString {
let mut res = String::new();
write!(res, "{}{}{}", self.0.to_estring(), S1, self.1.to_estring())
.ok()
.expect("Cannot parse Pair to EString");
EString(res)
}
}
impl<A, B, const S1: char> ParseFragment for Pair<A, S1, B>
where
A: ParseFragment,
B: ParseFragment,
@ -102,4 +114,12 @@ hello=bar",
_ => unreachable!(),
};
}
#[test]
fn should_format_pair() {
let pair = Pair::<_, '+', _>(1, 2);
assert_eq!(pair.clone().to_estring(), EString(String::from("1+2")));
let pair_in_pair = Pair::<_, '=', _>(3, pair);
assert_eq!(pair_in_pair.to_estring(), EString(String::from("3=1+2")));
}
}

View file

@ -1,7 +1,7 @@
//! Contains the implementations to vec type
//!
use crate::core::{EString, ParseFragment};
use crate::core::{EString, ParseFragment, ToEString};
use std::fmt::Write;
/// Wrapper for ``Vec`` to split string by a separator (`SEP`).
@ -51,11 +51,32 @@ where
f.write_char(SEP)?;
}
f.write_str(&part.to_string())
write!(f, "{}", part)
})
}
}
impl<T, const SEP: char> ToEString for SepVec<T, SEP>
where
T: ToEString,
{
fn to_estring(&self) -> EString {
self.0
.iter()
.enumerate()
.try_fold(String::new(), |mut res, (i, part)| {
if i != 0 {
res.write_char(SEP).ok()?;
}
write!(res, "{}", part.to_estring()).ok()?;
Some(res)
})
.map(EString)
.expect("Cannot format SepVec ${self.0} to EString")
}
}
impl<T, const SEP: char> ParseFragment for SepVec<T, SEP>
where
T: ParseFragment,
@ -74,6 +95,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::Pair;
use crate::{Error, Reason};
const COMMA: char = ',';
@ -139,4 +161,14 @@ d,e";
_ => unreachable!(),
};
}
#[test]
fn should_format_vec() {
type PlusPair<T> = Pair<T, '+', T>;
let vec = SepVec::<_, ','>::from(vec![1, 2, 3]);
assert_eq!(vec.to_estring(), EString(String::from("1,2,3")));
let vec = SepVec::<_, ','>::from(vec![PlusPair::from((1, 2)), PlusPair::from((3, 4))]);
assert_eq!(vec.to_estring(), EString(String::from("1+2,3+4")));
}
}

View file

@ -2,7 +2,7 @@
//!
use super::Pair;
use crate::core::{EString, ParseFragment};
use crate::core::{EString, ParseFragment, ToEString};
use std::fmt::Write;
/// Wrapper for trio (A, B, C) tuple to split string by separators (`S1` and `S2`).
@ -46,6 +46,29 @@ where
}
}
impl<A, B, C, const S1: char, const S2: char> ToEString for Trio<A, S1, B, S2, C>
where
A: ToEString,
B: ToEString,
C: ToEString,
{
fn to_estring(&self) -> EString {
let mut res = String::new();
write!(
res,
"{}{}{}{}{}",
self.0.to_estring(),
S1,
self.1.to_estring(),
S2,
self.2.to_estring()
)
.ok()
.expect("Cannot parse Pair to EString");
EString(res)
}
}
impl<A, const S1: char, B, const S2: char, C> ParseFragment for Trio<A, S1, B, S2, C>
where
A: ParseFragment,
@ -91,4 +114,19 @@ mod tests {
_ => unreachable!(),
};
}
#[test]
fn should_format_trio() {
let trio = Trio::<_, '+', _, '-', _>::from(("foo", "baz", "bar"));
assert_eq!(
trio.clone().to_estring(),
EString(String::from("foo+baz-bar"))
);
let trio_in_trio = Trio::<_, '*', _, '=', _>::from(("foo", "baz", trio));
assert_eq!(
trio_in_trio.clone().to_estring(),
EString(String::from("foo*baz=foo+baz-bar"))
);
}
}