1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use std::{
    ffi::OsStr,
    path::{Component, Path, PathBuf},
    str,
};

use crate::errors::*;

/// Provides string manipulation extensions for the [`str`] and [`String`] types
pub trait StringExt
{
    /// Returns the length in characters rather than bytes i.e. this is a human understandable
    /// value. However it is more costly to perform.
    ///
    /// ### Examples
    /// ```
    /// use rivia::prelude::*;
    ///
    /// assert_eq!("foo".size(), 3);
    /// assert_eq!("ƒoo".len(), 4); // fancy f!
    /// assert_eq!("ƒoo".size(), 3); // fancy f!
    /// ```
    fn size(&self) -> usize;

    /// Returns a new [`String`] with the given `suffix` trimmed off else the original `String`.
    ///
    /// ### Examples
    /// ```
    /// use rivia::prelude::*;
    ///
    /// assert_eq!("/foo/bar".to_string().trim_suffix("/bar"), "/foo");
    /// ```
    fn trim_suffix<T: Into<String>>(&self, suffix: T) -> String;
}

impl StringExt for str
{
    fn size(&self) -> usize
    {
        self.chars().count()
    }

    fn trim_suffix<T: Into<String>>(&self, suffix: T) -> String
    {
        let target = suffix.into();
        match self.ends_with(&target) {
            true => self[..self.len() - target.len()].to_owned(),
            _ => self.to_owned(),
        }
    }
}

impl StringExt for String
{
    fn size(&self) -> usize
    {
        self.chars().count()
    }

    fn trim_suffix<T: Into<String>>(&self, suffix: T) -> String
    {
        let target = suffix.into();
        match self.ends_with(&target) {
            true => self[..self.len() - target.len()].to_owned(),
            _ => self.to_owned(),
        }
    }
}

/// Provides to_string extension for the [`Path`], [`OsStr`] and [`Component`] types
pub trait ToStringExt
{
    /// Returns a new [`String`] from the given type.
    ///
    /// ### Examples
    /// ```
    /// use rivia::prelude::*;
    ///
    /// assert_eq!(Path::new("/foo").to_string().unwrap(), "/foo");
    /// ```
    fn to_string(&self) -> RvResult<String>;
}

impl ToStringExt for Path
{
    fn to_string(&self) -> RvResult<String>
    {
        let _str = self.to_str().ok_or(PathError::failed_to_string(self))?;
        Ok(String::from(_str))
    }
}

impl ToStringExt for OsStr
{
    fn to_string(&self) -> RvResult<String>
    {
        Ok(String::from(self.to_str().ok_or(StringError::FailedToString)?))
    }
}

impl ToStringExt for Component<'_>
{
    fn to_string(&self) -> RvResult<String>
    {
        let mut path = PathBuf::new();
        path.push(self);
        path.to_string()
    }
}

// Unit tests
// -------------------------------------------------------------------------------------------------
#[cfg(test)]
mod tests
{
    use std::{
        ffi::OsStr,
        path::{Component, Path, PathBuf},
    };

    use crate::prelude::*;

    #[test]
    fn test_str_size()
    {
        assert_eq!("foo".size(), 3);
        assert_eq!("ƒoo".len(), 4); // fancy f!
        assert_eq!("ƒoo".size(), 3); // fancy f!
    }

    #[test]
    fn test_string_size()
    {
        assert_eq!("foo".to_string().size(), 3);
        assert_eq!("ƒoo".to_string().len(), 4); // fancy f!
        assert_eq!("ƒoo".to_string().size(), 3); // fancy f!
    }

    #[test]
    fn test_str_trim_suffix()
    {
        assert_eq!("foo".trim_suffix("boo"), "foo"); // no change
        assert_eq!("foo".trim_suffix("oo"), "f");
        assert_eq!("ƒoo".trim_suffix("o"), "ƒo"); // fancy f!
    }

    #[test]
    fn test_string_trim_suffix()
    {
        assert_eq!("foo".to_string().trim_suffix("boo"), "foo"); // no change
        assert_eq!("foo".to_string().trim_suffix("oo"), "f");
        assert_eq!("ƒoo".to_string().trim_suffix("o"), "ƒo"); // fancy f!
    }

    #[test]
    fn test_osstr_to_string()
    {
        assert_eq!(OsStr::new("foo").to_string().unwrap(), "foo");
    }

    #[test]
    fn test_path_to_string()
    {
        assert_eq!(Path::new("/foo").to_string().unwrap(), "/foo");
        assert_eq!(PathBuf::from("/foo").to_string().unwrap(), "/foo");
    }

    #[test]
    fn test_component_to_string()
    {
        assert_eq!(Component::RootDir.to_string().unwrap(), "/");
        assert_eq!(Component::CurDir.to_string().unwrap(), ".");
        assert_eq!(Component::ParentDir.to_string().unwrap(), "..");
        assert_eq!(Component::Normal(OsStr::new("foo")).to_string().unwrap(), "foo");
    }
}