Skip to content

Commit

Permalink
Change StringM Debug, String, and Debug representations to escaped (#186
Browse files Browse the repository at this point in the history
)
  • Loading branch information
leighmcculloch authored Dec 1, 2023
1 parent 64e7418 commit e90b9ee
Show file tree
Hide file tree
Showing 17 changed files with 391 additions and 544 deletions.
55 changes: 23 additions & 32 deletions lib/xdrgen/generators/rust/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,17 @@ impl<const MAX: u32> WriteXdr for BytesM<MAX> {

// StringM ------------------------------------------------------------------------

/// A string type that contains arbitrary bytes.
///
/// Convertible, fallibly, to/from a Rust UTF-8 String using
/// [`TryFrom`]/[`TryInto`]/[`StringM::to_utf8_string`].
///
/// Convertible, lossyly, to a Rust UTF-8 String using
/// [`StringM::to_utf8_string_lossy`].
///
/// Convertible to/from escaped printable-ASCII using
/// [`Display`]/[`ToString`]/[`FromStr`].
#[cfg(feature = "alloc")]
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(
Expand All @@ -1649,38 +1660,15 @@ pub struct StringM<const MAX: u32 = { u32::MAX }>(Vec<u8>);
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
pub struct StringM<const MAX: u32 = { u32::MAX }>(Vec<u8>);

/// `write_utf8_lossy` is a modified copy of the Rust stdlib docs examples here:
/// <https://doc.rust-lang.org/stable/core/str/struct.Utf8Error.html#examples>
fn write_utf8_lossy(f: &mut impl core::fmt::Write, mut input: &[u8]) -> core::fmt::Result {
loop {
match core::str::from_utf8(input) {
Ok(valid) => {
write!(f, "{valid}")?;
break;
}
Err(error) => {
let (valid, after_valid) = input.split_at(error.valid_up_to());
write!(f, "{}", core::str::from_utf8(valid).unwrap())?;
write!(f, "\u{FFFD}")?;

if let Some(invalid_sequence_length) = error.error_len() {
input = &after_valid[invalid_sequence_length..];
} else {
break;
}
}
}
}
Ok(())
}

impl<const MAX: u32> core::fmt::Display for StringM<MAX> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
#[cfg(feature = "alloc")]
let v = &self.0;
#[cfg(not(feature = "alloc"))]
let v = self.0;
write_utf8_lossy(f, v)?;
for b in escape_bytes::Escape::new(v) {
write!(f, "{}", b as char)?;
}
Ok(())
}
}
Expand All @@ -1692,7 +1680,9 @@ impl<const MAX: u32> core::fmt::Debug for StringM<MAX> {
#[cfg(not(feature = "alloc"))]
let v = self.0;
write!(f, "StringM(")?;
write_utf8_lossy(f, v)?;
for b in escape_bytes::Escape::new(v) {
write!(f, "{}", b as char)?;
}
write!(f, ")")?;
Ok(())
}
Expand All @@ -1702,7 +1692,8 @@ impl<const MAX: u32> core::fmt::Debug for StringM<MAX> {
impl<const MAX: u32> core::str::FromStr for StringM<MAX> {
type Err = Error;
fn from_str(s: &str) -> core::result::Result<Self, Self::Err> {
s.try_into()
let b = escape_bytes::unescape(s.as_bytes()).map_err(|_| Error::Invalid)?;
Ok(Self(b))
}
}

Expand Down Expand Up @@ -1750,24 +1741,24 @@ impl<const MAX: u32> StringM<MAX> {

impl<const MAX: u32> StringM<MAX> {
#[cfg(feature = "alloc")]
pub fn to_string(&self) -> Result<String> {
pub fn to_utf8_string(&self) -> Result<String> {
self.try_into()
}

#[cfg(feature = "alloc")]
pub fn into_string(self) -> Result<String> {
pub fn into_utf8_string(self) -> Result<String> {
self.try_into()
}

#[cfg(feature = "alloc")]
#[must_use]
pub fn to_string_lossy(&self) -> String {
pub fn to_utf8_string_lossy(&self) -> String {
String::from_utf8_lossy(&self.0).into_owned()
}

#[cfg(feature = "alloc")]
#[must_use]
pub fn into_string_lossy(self) -> String {
pub fn into_utf8_string_lossy(self) -> String {
String::from_utf8_lossy(&self.0).into_owned()
}
}
Expand Down
55 changes: 23 additions & 32 deletions spec/output/generator_spec_rust/block_comments.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1645,6 +1645,17 @@ impl<const MAX: u32> WriteXdr for BytesM<MAX> {

// StringM ------------------------------------------------------------------------

/// A string type that contains arbitrary bytes.
///
/// Convertible, fallibly, to/from a Rust UTF-8 String using
/// [`TryFrom`]/[`TryInto`]/[`StringM::to_utf8_string`].
///
/// Convertible, lossyly, to a Rust UTF-8 String using
/// [`StringM::to_utf8_string_lossy`].
///
/// Convertible to/from escaped printable-ASCII using
/// [`Display`]/[`ToString`]/[`FromStr`].
#[cfg(feature = "alloc")]
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(
Expand All @@ -1659,38 +1670,15 @@ pub struct StringM<const MAX: u32 = { u32::MAX }>(Vec<u8>);
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
pub struct StringM<const MAX: u32 = { u32::MAX }>(Vec<u8>);

/// `write_utf8_lossy` is a modified copy of the Rust stdlib docs examples here:
/// <https://doc.rust-lang.org/stable/core/str/struct.Utf8Error.html#examples>
fn write_utf8_lossy(f: &mut impl core::fmt::Write, mut input: &[u8]) -> core::fmt::Result {
loop {
match core::str::from_utf8(input) {
Ok(valid) => {
write!(f, "{valid}")?;
break;
}
Err(error) => {
let (valid, after_valid) = input.split_at(error.valid_up_to());
write!(f, "{}", core::str::from_utf8(valid).unwrap())?;
write!(f, "\u{FFFD}")?;

if let Some(invalid_sequence_length) = error.error_len() {
input = &after_valid[invalid_sequence_length..];
} else {
break;
}
}
}
}
Ok(())
}

impl<const MAX: u32> core::fmt::Display for StringM<MAX> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
#[cfg(feature = "alloc")]
let v = &self.0;
#[cfg(not(feature = "alloc"))]
let v = self.0;
write_utf8_lossy(f, v)?;
for b in escape_bytes::Escape::new(v) {
write!(f, "{}", b as char)?;
}
Ok(())
}
}
Expand All @@ -1702,7 +1690,9 @@ impl<const MAX: u32> core::fmt::Debug for StringM<MAX> {
#[cfg(not(feature = "alloc"))]
let v = self.0;
write!(f, "StringM(")?;
write_utf8_lossy(f, v)?;
for b in escape_bytes::Escape::new(v) {
write!(f, "{}", b as char)?;
}
write!(f, ")")?;
Ok(())
}
Expand All @@ -1712,7 +1702,8 @@ impl<const MAX: u32> core::fmt::Debug for StringM<MAX> {
impl<const MAX: u32> core::str::FromStr for StringM<MAX> {
type Err = Error;
fn from_str(s: &str) -> core::result::Result<Self, Self::Err> {
s.try_into()
let b = escape_bytes::unescape(s.as_bytes()).map_err(|_| Error::Invalid)?;
Ok(Self(b))
}
}

Expand Down Expand Up @@ -1760,24 +1751,24 @@ impl<const MAX: u32> StringM<MAX> {

impl<const MAX: u32> StringM<MAX> {
#[cfg(feature = "alloc")]
pub fn to_string(&self) -> Result<String> {
pub fn to_utf8_string(&self) -> Result<String> {
self.try_into()
}

#[cfg(feature = "alloc")]
pub fn into_string(self) -> Result<String> {
pub fn into_utf8_string(self) -> Result<String> {
self.try_into()
}

#[cfg(feature = "alloc")]
#[must_use]
pub fn to_string_lossy(&self) -> String {
pub fn to_utf8_string_lossy(&self) -> String {
String::from_utf8_lossy(&self.0).into_owned()
}

#[cfg(feature = "alloc")]
#[must_use]
pub fn into_string_lossy(self) -> String {
pub fn into_utf8_string_lossy(self) -> String {
String::from_utf8_lossy(&self.0).into_owned()
}
}
Expand Down
55 changes: 23 additions & 32 deletions spec/output/generator_spec_rust/const.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1645,6 +1645,17 @@ impl<const MAX: u32> WriteXdr for BytesM<MAX> {

// StringM ------------------------------------------------------------------------

/// A string type that contains arbitrary bytes.
///
/// Convertible, fallibly, to/from a Rust UTF-8 String using
/// [`TryFrom`]/[`TryInto`]/[`StringM::to_utf8_string`].
///
/// Convertible, lossyly, to a Rust UTF-8 String using
/// [`StringM::to_utf8_string_lossy`].
///
/// Convertible to/from escaped printable-ASCII using
/// [`Display`]/[`ToString`]/[`FromStr`].
#[cfg(feature = "alloc")]
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(
Expand All @@ -1659,38 +1670,15 @@ pub struct StringM<const MAX: u32 = { u32::MAX }>(Vec<u8>);
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
pub struct StringM<const MAX: u32 = { u32::MAX }>(Vec<u8>);

/// `write_utf8_lossy` is a modified copy of the Rust stdlib docs examples here:
/// <https://doc.rust-lang.org/stable/core/str/struct.Utf8Error.html#examples>
fn write_utf8_lossy(f: &mut impl core::fmt::Write, mut input: &[u8]) -> core::fmt::Result {
loop {
match core::str::from_utf8(input) {
Ok(valid) => {
write!(f, "{valid}")?;
break;
}
Err(error) => {
let (valid, after_valid) = input.split_at(error.valid_up_to());
write!(f, "{}", core::str::from_utf8(valid).unwrap())?;
write!(f, "\u{FFFD}")?;

if let Some(invalid_sequence_length) = error.error_len() {
input = &after_valid[invalid_sequence_length..];
} else {
break;
}
}
}
}
Ok(())
}

impl<const MAX: u32> core::fmt::Display for StringM<MAX> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
#[cfg(feature = "alloc")]
let v = &self.0;
#[cfg(not(feature = "alloc"))]
let v = self.0;
write_utf8_lossy(f, v)?;
for b in escape_bytes::Escape::new(v) {
write!(f, "{}", b as char)?;
}
Ok(())
}
}
Expand All @@ -1702,7 +1690,9 @@ impl<const MAX: u32> core::fmt::Debug for StringM<MAX> {
#[cfg(not(feature = "alloc"))]
let v = self.0;
write!(f, "StringM(")?;
write_utf8_lossy(f, v)?;
for b in escape_bytes::Escape::new(v) {
write!(f, "{}", b as char)?;
}
write!(f, ")")?;
Ok(())
}
Expand All @@ -1712,7 +1702,8 @@ impl<const MAX: u32> core::fmt::Debug for StringM<MAX> {
impl<const MAX: u32> core::str::FromStr for StringM<MAX> {
type Err = Error;
fn from_str(s: &str) -> core::result::Result<Self, Self::Err> {
s.try_into()
let b = escape_bytes::unescape(s.as_bytes()).map_err(|_| Error::Invalid)?;
Ok(Self(b))
}
}

Expand Down Expand Up @@ -1760,24 +1751,24 @@ impl<const MAX: u32> StringM<MAX> {

impl<const MAX: u32> StringM<MAX> {
#[cfg(feature = "alloc")]
pub fn to_string(&self) -> Result<String> {
pub fn to_utf8_string(&self) -> Result<String> {
self.try_into()
}

#[cfg(feature = "alloc")]
pub fn into_string(self) -> Result<String> {
pub fn into_utf8_string(self) -> Result<String> {
self.try_into()
}

#[cfg(feature = "alloc")]
#[must_use]
pub fn to_string_lossy(&self) -> String {
pub fn to_utf8_string_lossy(&self) -> String {
String::from_utf8_lossy(&self.0).into_owned()
}

#[cfg(feature = "alloc")]
#[must_use]
pub fn into_string_lossy(self) -> String {
pub fn into_utf8_string_lossy(self) -> String {
String::from_utf8_lossy(&self.0).into_owned()
}
}
Expand Down
Loading

0 comments on commit e90b9ee

Please sign in to comment.