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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//! Setters.
use crate::prelude::*;
// TODO: Optimize relatives.
impl FilePath<'_> {
/// [`resolve_file_path_range`].
fn resolve_range<B: RangeBounds<usize>>(&mut self, range: B) -> bool {
let mut temp = "/".into();
std::mem::swap(&mut self.0, &mut temp);
let (changed, mut temp) = resolve_file_path_range(temp, range);
std::mem::swap(&mut self.0, &mut temp);
changed
}
/// Append `value`.
/// # Examples
/// ```
/// use better_url::prelude::*;
///
/// let mut path = FilePath::new("/abc/def/ghi");
///
/// path.push("123"); assert_eq!(path, "/abc/def/ghi/123");
/// path.push(".." ); assert_eq!(path, "/abc/def/ghi/" );
/// path.push(".." ); assert_eq!(path, "/abc/def/ghi/" );
/// path.push("." ); assert_eq!(path, "/abc/def/ghi//" );
/// ```
pub fn push<'a, T: Into<FilePathSegment<'a>>>(&mut self, value: T) -> bool {
let start = self.len();
self.0.extend(["/", value.into().as_str()]);
let _ = self.resolve_range(start..);
true
}
/// Prepend `value`.
/// # Examples
/// ```
/// use better_url::prelude::*;
///
/// let mut path = FilePath::new("/abc/def/ghi");
///
/// path.prepend("123"); assert_eq!(path, "/123/abc/def/ghi");
/// path.prepend("." ); assert_eq!(path, "/123/abc/def/ghi");
/// path.prepend(".." ); assert_eq!(path, "/123/abc/def/ghi");
/// ```
pub fn prepend<'a, T: Into<FilePathSegment<'a>>>(&mut self, value: T) -> bool {
let new = value.into();
self.0.insert_with(0, ["/", new.as_str()]);
let _ = self.resolve_range(.. 1 + new.len());
true
}
/// Set, insert, or remove the `index`th segment.
///
/// If `value` is [`None`] and `index` is out of bounds, does nothing.
/// # Errors
/// If `value` is [`Some`] and `index` is more than 1 out of bounds, returns the error [`InsertNotFound`].
///
/// If `value` is [`None`] and `index` is the only segment, returns the error [`CantBeEmpty`].
/// # Examples
/// ```
/// use better_url::prelude::*;
///
/// let mut path = FilePath::new("/c:/abc/def/ghi");
///
/// path.set( 1, Some(".." )).unwrap(); assert_eq!(path, "/c:/def/ghi" );
/// path.set( 0, Some("e|" )).unwrap(); assert_eq!(path, "/e:/def/ghi" );
/// path.set(-4, Some("123")).unwrap(); assert_eq!(path, "/123/e:/def/ghi");
/// path.set( 2, Some(".." )).unwrap(); assert_eq!(path, "/123/ghi" );
/// path.set( 2, Some("456")).unwrap(); assert_eq!(path, "/123/ghi/456" );
/// ```
pub fn set<'a, T: Into<FilePathSegment<'a>>>(&mut self, index: isize, value: Option<T>) -> Result<bool, SetPathError> {
Ok(match value.map(Into::into) {
Some(new) => match (self.iter_strs().try_neg_nth(index), index) {
(Ok(old), _) => {
let start = old.addr() - self.as_str().addr() - 1;
self.0.replace_substr(old, new.as_str());
let _ = self.resolve_range(start .. start + 1 + new.len());
true
},
(Err(0), 0..) => self.push (new),
(Err(0), ..0) => self.prepend(new),
(Err(_), _) => Err(InsertNotFound)?,
},
None => match self.iter_strs().neg_nth(index) {
Some(temp) => {
if temp.len() + 1 == self.len() {
Err(CantBeEmpty)?;
}
let range = self.0.my_substr_range(temp);
self.0.replace_range(range.start - 1 .. range.end, "");
true
},
None => false
}
})
}
/// Set the range of segments.
/// # Errors
/// If [`Self::range_str`] returns [`None`], returns the error [`RangeNotFound`].
///
/// If `value` is [`None`] and `range` contains all segments, returns the error [`CantBeEmpty`].
/// # Examples
/// ```
/// use better_url::prelude::*;
///
/// let mut path = FilePath::new("/c:/abc/def/ghi");
///
/// path.set_range(1..2, Some(".././123/.")).unwrap(); assert_eq!(path, "/c:/123/def/ghi");
/// ```
pub fn set_range<'a, T: Into<FilePathSegments<'a>>, B: RangeBounds<isize>>(&mut self, range: B, value: Option<T>) -> Result<bool, SetPathError> {
Ok(match value.map(Into::into) {
Some(new) => {
let old = self.range_str(range).ok_or(RangeNotFound)?;
let start = old.addr() - self.as_str().addr() - 1;
self.0.replace_substr(old, new.as_str());
let _ = self.resolve_range(start .. start + 1 + new.len());
true
},
None => {
let old = self.range_str(range).ok_or(RangeNotFound)?;
if old.len() + 1 == self.len() {
Err(CantBeEmpty)?;
}
let Range {start, end} = self.0.my_substr_range(old);
self.0.replace_range(start - 1 .. end, "");
true
}
})
}
/// Insert a new `index`th segment.
/// # Errors
/// If the insert isn't found, returns the error [`InsertNotFound`].
/// # Examples
/// ```
/// use better_url::prelude::*;
///
/// let mut path = FilePath::new("/c:/abc/def/ghi");
///
/// path.insert( 2, "123").unwrap(); assert_eq!(path, "/c:/abc/123/def/ghi");
/// path.insert( 3, ".." ).unwrap(); assert_eq!(path, "/c:/abc/def/ghi" );
/// path.insert(-1, "123").unwrap(); assert_eq!(path, "/c:/abc/def/ghi/123");
/// ```
pub fn insert<'a, T: Into<FilePathSegment<'a>>>(&mut self, index: isize, value: T) -> Result<bool, SetPathError> {
let new = value.into();
let i = match (self.iter_strs().try_neg_nth(index), index) {
(Ok (x), ..0) => x.end_addr() - self.0.addr(),
(Ok (x), 0..) => x. addr() - self.0.addr() - 1,
(Err(0), 0..) => self.len(),
(Err(0), ..0) => 0,
(Err(_), _ ) => Err(InsertNotFound)?
};
self.0.insert_with(i, ["/", new.as_str()]);
let _ = self.resolve_range(i .. i + 1 + new.len());
Ok(true)
}
}