Rebase main

pull/522/head v0.20.0-beta.0
Arijit Basu 2 years ago committed by Arijit Basu
parent 85c4253782
commit f8b16a7ddd

@ -2563,4 +2563,3 @@ end
-- You can also use nested tables such as
-- `xplr.fn.custom.my_plugin.my_function` to define custom functions.
xplr.fn.custom = {}

@ -17,7 +17,6 @@ pub mod pipe;
pub mod pwd_watcher;
pub mod runner;
pub mod ui;
pub mod newlines;
#[cfg(test)]
mod tests {

@ -1,79 +0,0 @@
struct UnescapedString<'a> {
s: std::str::Chars<'a>,
}
impl<'a> UnescapedString<'a> {
fn new(s: &'a str) -> Self {
Self { s: s.chars() }
}
}
impl Iterator for UnescapedString<'_> {
type Item = Result<char, Error>;
fn next(&mut self) -> Option<Self::Item> {
self.s.next().map(|c| match c {
'\\' => match self.s.next() {
None => Err(Error::EscapeAtEndOfString),
Some('n') => Ok('\n'),
Some('\\') => Ok('\\'),
Some(c) => Err(Error::UnrecognizedEscapedChar(c)),
},
c => Ok(c),
})
}
}
#[derive(Debug, PartialEq)]
pub enum Error {
EscapeAtEndOfString,
UnrecognizedEscapedChar(char),
}
use std::fmt;
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::EscapeAtEndOfString => {
write!(f, "Escape character at the end of the string")
}
Error::UnrecognizedEscapedChar(c) => {
write!(f, "Unrecognized escaped char: '{}'", c)
}
}
}
}
impl std::error::Error for Error {}
struct EscapedString<'a> {
s: std::str::Chars<'a>,
}
impl<'a> EscapedString<'a> {
fn new(s: &'a str) -> Self {
Self { s: s.chars() }
}
}
impl Iterator for EscapedString<'_> {
type Item = String;
fn next(&mut self) -> Option<Self::Item> {
match self.s.next() {
None => None,
Some('\\') => Some(String::from("\\\\")),
Some('\n') => Some(String::from("\\n")),
Some(c) => Some(String::from(c)),
}
}
}
pub fn escape_string(s: &str) -> String {
EscapedString::new(s).collect()
}
pub fn unescape_string(s: &str) -> Result<String, Error> {
UnescapedString::new(s).collect()
}
Loading…
Cancel
Save