-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds LazyRawTextReader support for reading strings (#614)
- Loading branch information
Showing
14 changed files
with
586 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use crate::text::text_formatter::IonValueFormatter; | ||
use crate::Str; | ||
use std::borrow::Cow; | ||
use std::fmt::{Display, Formatter}; | ||
use std::ops::Deref; | ||
|
||
#[derive(Clone, PartialEq, Debug)] | ||
pub struct StrRef<'data> { | ||
text: Cow<'data, str>, | ||
} | ||
|
||
impl<'data> StrRef<'data> { | ||
pub fn to_owned(&self) -> Str { | ||
Str::from(self.as_ref()) | ||
} | ||
|
||
pub fn into_owned(self) -> Str { | ||
Str::from(self) | ||
} | ||
|
||
pub fn text(&self) -> &str { | ||
self.as_ref() | ||
} | ||
} | ||
|
||
impl<'data> Deref for StrRef<'data> { | ||
type Target = str; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.text.as_ref() | ||
} | ||
} | ||
|
||
impl<'data> PartialEq<str> for StrRef<'data> { | ||
fn eq(&self, other: &str) -> bool { | ||
self.text() == other | ||
} | ||
} | ||
|
||
impl<'data> PartialEq<&str> for StrRef<'data> { | ||
fn eq(&self, other: &&str) -> bool { | ||
self.text() == *other | ||
} | ||
} | ||
|
||
impl<'data> PartialEq<StrRef<'data>> for str { | ||
fn eq(&self, other: &StrRef<'data>) -> bool { | ||
self == other.text() | ||
} | ||
} | ||
|
||
impl<'data> Display for StrRef<'data> { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
let mut formatter = IonValueFormatter { output: f }; | ||
formatter | ||
.format_string(self.text()) | ||
.map_err(|_| std::fmt::Error) | ||
} | ||
} | ||
|
||
impl<'a> From<&'a str> for StrRef<'a> { | ||
fn from(value: &'a str) -> Self { | ||
StrRef { | ||
text: Cow::from(value), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> From<String> for StrRef<'a> { | ||
fn from(value: String) -> Self { | ||
StrRef { | ||
text: Cow::from(value), | ||
} | ||
} | ||
} | ||
|
||
impl<'data> From<StrRef<'data>> for Str { | ||
fn from(str_ref: StrRef<'data>) -> Self { | ||
let text: String = str_ref.text.into_owned(); | ||
Str::from(text) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.