Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add encoding::LengthString #265

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,162 @@
}
}

#[derive(
Deref,
DerefMut,

Check warning on line 190 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L189-L190

Added lines #L189 - L190 were not covered by tests
Into,
Default,
Clone,
Debug,
FieldQuery,

Check warning on line 195 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L195

Added line #L195 was not covered by tests
PartialEq,
Hash,
Eq,
Describe,

Check warning on line 199 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L199

Added line #L199 was not covered by tests
Serialize,
Deserialize,

Check warning on line 201 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L201

Added line #L201 was not covered by tests
)]
#[serde(transparent)]
pub struct LengthString<P>
where
P: Encode + Decode + TryInto<usize> + Terminated + Clone + 'static,
{
#[serde(skip)]
len: P,

#[deref]
#[deref_mut]
#[into]
inner: String,
}

impl<P> Migrate for LengthString<P> where
P: Encode + Decode + TryInto<usize> + Terminated + Clone + 'static
{
}

impl<P> LengthString<P>
where
P: Encode + Decode + TryInto<usize> + Terminated + Clone,
{
pub fn new(len: P, inner: String) -> Self {
LengthString { len, inner }
}

Check warning on line 228 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L226-L228

Added lines #L226 - L228 were not covered by tests
}

impl<P> Decode for LengthString<P>
where
P: Encode + Decode + Terminated + TryInto<usize> + Clone,
{
fn decode<R: std::io::Read>(mut input: R) -> Result<Self> {
let len = P::decode(&mut input)?;

Check warning on line 236 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L235-L236

Added lines #L235 - L236 were not covered by tests

let len_usize = len
.clone()
.try_into()
.map_err(|_| Error::UnexpectedByte(80))?;

Check warning on line 241 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L238-L241

Added lines #L238 - L241 were not covered by tests

let mut inner = String::with_capacity(len_usize);
for _ in 0..len_usize {
let value = u8::decode(&mut input)?;
inner.push(value as char);

Check warning on line 246 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L243-L246

Added lines #L243 - L246 were not covered by tests
}

Ok(LengthString { len, inner })
}

Check warning on line 250 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L249-L250

Added lines #L249 - L250 were not covered by tests
}

impl<P> Encode for LengthString<P>
where
P: Encode + Decode + TryInto<usize> + Terminated + Clone,
{
fn encode_into<W: std::io::Write>(&self, mut out: &mut W) -> Result<()> {
self.len.encode_into(&mut out)?;
for c in self.inner.chars() {
(c as u8).encode_into(&mut out)?;

Check warning on line 260 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L257-L260

Added lines #L257 - L260 were not covered by tests
}

Ok(())
}

Check warning on line 264 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L263-L264

Added lines #L263 - L264 were not covered by tests

fn encoding_length(&self) -> Result<usize> {
let mut len = self.len.encoding_length()?;
for c in self.inner.chars() {
len += (c as u8).encoding_length()?;

Check warning on line 269 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L266-L269

Added lines #L266 - L269 were not covered by tests
}

Ok(len)
}

Check warning on line 273 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L272-L273

Added lines #L272 - L273 were not covered by tests
}

impl<P> Terminated for LengthString<P> where P: Encode + Decode + TryInto<usize> + Terminated + Clone
{}

impl<P> State for LengthString<P>
where
P: Encode + Decode + TryInto<usize> + Terminated + Clone + 'static,
{
fn attach(&mut self, _store: Store) -> crate::Result<()> {
Ok(())
}

Check warning on line 285 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L283-L285

Added lines #L283 - L285 were not covered by tests

fn flush<W: std::io::Write>(self, out: &mut W) -> crate::Result<()> {
self.len.encode_into(out)?;

Check warning on line 288 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L287-L288

Added lines #L287 - L288 were not covered by tests
// TODO: non-utf8 support?
self.inner.as_bytes().encode_into(out)?;

Check warning on line 290 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L290

Added line #L290 was not covered by tests

Ok(())
}

Check warning on line 293 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L292-L293

Added lines #L292 - L293 were not covered by tests

fn load(_store: Store, mut bytes: &mut &[u8]) -> crate::Result<Self> {
let len = P::decode(&mut bytes)?;
let len_usize = len
.clone()
.try_into()
.map_err(|_| Error::UnexpectedByte(80))?;

Check warning on line 300 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L295-L300

Added lines #L295 - L300 were not covered by tests

let mut inner = String::with_capacity(len_usize);
for _ in 0..len_usize {
let value = u8::decode(&mut bytes)?;
inner.push(value as char);

Check warning on line 305 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L302-L305

Added lines #L302 - L305 were not covered by tests
}

Ok(LengthString { len, inner })
}

Check warning on line 309 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L308-L309

Added lines #L308 - L309 were not covered by tests
}

impl<P> TryFrom<&str> for LengthString<P>
where
P: State + Encode + Decode + TryInto<usize> + TryFrom<usize> + Terminated + Clone,
{
type Error = crate::Error;

fn try_from(value: &str) -> crate::Result<Self> {
value.to_string().try_into()
}

Check warning on line 320 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L318-L320

Added lines #L318 - L320 were not covered by tests
}

// impl<P> Describe for LengthString<P>
// where
// P: State + Encode + Decode + TryInto<usize> + Terminated + Clone +
// 'static, {
// fn describe() -> crate::describe::Descriptor {
// crate::describe::Builder::new::<Self>().build()
// }
// }

impl<P> TryFrom<String> for LengthString<P>
where
P: State + Encode + Decode + TryInto<usize> + TryFrom<usize> + Terminated + Clone,
{
type Error = crate::Error;

fn try_from(inner: String) -> crate::Result<Self> {
let len = inner.len().try_into().map_err(|_| crate::Error::Overflow)?;
Ok(Self { len, inner })
}

Check warning on line 341 in src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

src/encoding.rs#L338-L341

Added lines #L338 - L341 were not covered by tests
}

#[derive(Clone, Debug)]
pub struct Adapter<T>(pub T);

Expand Down
Loading