diff --git a/tripod-id/src/double.rs b/tripod-id/src/double.rs index 2fe2d79..45a0bcc 100644 --- a/tripod-id/src/double.rs +++ b/tripod-id/src/double.rs @@ -4,7 +4,7 @@ use rand::{distributions::Standard, prelude::Distribution, Rng}; use crate::{utils::is_delimiter, Error, TripodId, Single}; -#[derive(Debug, Clone, PartialEq)] +#[derive(Copy, Clone, Debug, PartialEq)] pub struct Double{ inner: (Single, Single) } @@ -36,7 +36,7 @@ impl TripodId for Double{ #[cfg(test)] fn is_valid(&self) -> bool { - self.inner.0.is_valid() && self.inner.1.is_valid() && (u32::from(self) < Self::SIZE) + self.inner.0.is_valid() && self.inner.1.is_valid() && (u32::from(*self) < Self::SIZE) } } @@ -98,16 +98,16 @@ impl TryFrom for Double { )}) } else { Err(Error::OutsideOfRange{ - expected: Self::SIZE as usize, - found: value as usize + expected: Self::SIZE as u64, + found: value as u64 }) } } } -impl From<&Double> for u32 { - fn from(value: &Double) -> Self { - u32::from(u16::from(&value.inner.0)) * u32::from(Single::SIZE) + u32::from(u16::from(&value.inner.1)) +impl From for u32 { + fn from(value: Double) -> Self { + u32::from(u16::from(value.inner.0)) * u32::from(Single::SIZE) + u32::from(u16::from(value.inner.1)) } } @@ -122,7 +122,7 @@ mod tests { let id: Double = rand.r#gen(); assert!(id.is_valid()); assert_eq!(id,Double::from_str(&id.to_string()).unwrap()); - assert_eq!(id, Double::try_from(u32::from(&id)).unwrap()) + assert_eq!(id, Double::try_from(u32::from(id)).unwrap()) } #[test] fn random_x10() { diff --git a/tripod-id/src/error.rs b/tripod-id/src/error.rs index 5febe6c..795fe18 100644 --- a/tripod-id/src/error.rs +++ b/tripod-id/src/error.rs @@ -2,8 +2,8 @@ pub enum Error { #[error("expected under {expected}, found {found}")] OutsideOfRange{ - expected: usize, - found: usize, + expected: u64, + found: u64, }, #[error("Invalid chunk: {0}")] InvalidChunk(String), diff --git a/tripod-id/src/lib.rs b/tripod-id/src/lib.rs index cdce4ae..6d9da23 100644 --- a/tripod-id/src/lib.rs +++ b/tripod-id/src/lib.rs @@ -15,6 +15,8 @@ pub use error::*; #[cfg(feature="prost")] pub mod prost; +#[cfg(feature="prost")] +pub use prost::{ SingleMessage, DoubleMessage, TripleMessage }; pub trait TripodId { type SizeType; @@ -24,4 +26,31 @@ pub trait TripodId { #[cfg(test)] fn is_valid(&self) -> bool; } +#[cfg(test)] +mod tests { + use std::{fmt::Display, fmt::Debug, str::FromStr}; + use super::*; + + #[cfg(feature="prost")] + fn assert_prost(id: T) where + T: TripodId + Debug + Display + FromStr + PartialEq + TryFrom + Copy, + I: From { + use crate::SingleMessage; + let message = SingleMessage::from(*id); + assert_eq!(message.is_valid()); + let result = Single::try_from(message).unwrap(); + assert_eq!(id, result); + } + + fn assert_id (id: T) where + T: TripodId + Debug + Display + FromStr + PartialEq + TryFrom + Copy, + I: From + { + assert!(id.is_valid()); + let s = id.to_string(); + assert_eq!(id,T::from_str(&s).unwrap()); + let i = T::SizeType::from(id); + assert_eq!(id, T::try_from(i).unwrap()); + } +} \ No newline at end of file diff --git a/tripod-id/src/prost.rs b/tripod-id/src/prost.rs deleted file mode 100644 index b7673ff..0000000 --- a/tripod-id/src/prost.rs +++ /dev/null @@ -1,7 +0,0 @@ - { - include!(concat!(env!("OUT_DIR"), "/fireturtle.tripod_id.rs")); - } - pub use crate::fireturtle::tripod_id::*; - type SingleMessage = Single; - type DoubleMessage = Double; - type TripleMessage = Triple; \ No newline at end of file diff --git a/tripod-id/src/prost/double.rs b/tripod-id/src/prost/double.rs new file mode 100644 index 0000000..98427fb --- /dev/null +++ b/tripod-id/src/prost/double.rs @@ -0,0 +1,34 @@ +use prost::Name; + +use crate::{prost::Double, Error, TripodId}; + +impl Name for Double { + const NAME: &'static str = "Double"; + const PACKAGE: &'static str = super::PACKAGE_NAME; +} + +impl Double { + #[cfg(test)] + pub fn is_valid(&self) -> bool { + use crate::TripodId; + + self.id < u32::from(crate::Double::SIZE) + } +} + +impl From for Double { + fn from(value: crate::Double) -> Self { + Self { + id: u32::from(value) + } + } +} +impl TryFrom for crate::Double { + type Error = Error; + + fn try_from(value: Double) -> Result { + Self::try_from( + value.id + ) + } +} \ No newline at end of file diff --git a/tripod-id/src/prost/mod.rs b/tripod-id/src/prost/mod.rs new file mode 100644 index 0000000..81fe490 --- /dev/null +++ b/tripod-id/src/prost/mod.rs @@ -0,0 +1,20 @@ +mod generated { + include!(concat!(env!("OUT_DIR"), "/fireturtle.tripod_id.rs")); +} + +mod single; +mod double; +mod triple; + +pub use generated::*; +const PACKAGE_NAME: &'static str = "fireturtle.tripod_id"; +pub type SingleMessage = Single; +pub type DoubleMessage = Double; +pub type TripleMessage = Triple; + +pub trait TrypodIdMessage: From { + type TrypodId: crate::TripodId + TryFrom; + + #[cfg(test)] + fn is_valid(&self) -> bool; +} \ No newline at end of file diff --git a/tripod-id/src/prost/single.rs b/tripod-id/src/prost/single.rs new file mode 100644 index 0000000..6a7b3a2 --- /dev/null +++ b/tripod-id/src/prost/single.rs @@ -0,0 +1,37 @@ +use prost::Name; + +use crate::{prost::Single, Error, TripodId}; + +impl Name for Single { + const NAME: &'static str = "Single"; + const PACKAGE: &'static str = super::PACKAGE_NAME; +} + +impl Single { + #[cfg(test)] + pub fn is_valid(&self) -> bool { + use crate::TripodId; + + self.id < u32::from(crate::Single::SIZE) + } +} + +impl From for Single { + fn from(value: crate::Single) -> Self { + Self { + id: u32::from(u16::from(value)) + } + } +} +impl TryFrom for crate::Single { + type Error = Error; + + fn try_from(value: Single) -> Result { + Self::try_from( + u16::try_from(value.id).or(Err(Error::OutsideOfRange { + expected: u64::from(crate::Single::SIZE), + found: u64::from(value.id) + }))? + ) + } +} diff --git a/tripod-id/src/prost/triple.rs b/tripod-id/src/prost/triple.rs new file mode 100644 index 0000000..0e92bc2 --- /dev/null +++ b/tripod-id/src/prost/triple.rs @@ -0,0 +1,34 @@ +use prost::Name; + +use crate::{prost::Triple, Error, TripodId}; + +impl Name for Triple { + const NAME: &'static str = "Triple"; + const PACKAGE: &'static str = super::PACKAGE_NAME; +} + +impl Triple { + #[cfg(test)] + pub fn is_valid(&self) -> bool { + use crate::TripodId; + + self.id < crate::Triple::SIZE + } +} + +impl From for Triple { + fn from(value: crate::Triple) -> Self { + Self { + id: u64::from(value) + } + } +} +impl TryFrom for crate::Triple { + type Error = Error; + + fn try_from(value: Triple) -> Result { + Self::try_from( + value.id + ) + } +} \ No newline at end of file diff --git a/tripod-id/src/rusqlite.rs b/tripod-id/src/rusqlite.rs index f721bc8..0dc6d8e 100644 --- a/tripod-id/src/rusqlite.rs +++ b/tripod-id/src/rusqlite.rs @@ -13,7 +13,7 @@ impl FromSql for Single { impl ToSql for Single { fn to_sql(&self) -> rusqlite::Result> { - Ok(u16::from(self).into()) + Ok(u16::from(*self).into()) } } @@ -28,7 +28,7 @@ impl FromSql for Double { impl ToSql for Double { fn to_sql(&self) -> rusqlite::Result> { - Ok(u32::from(self).into()) + Ok(u32::from(*self).into()) } } @@ -44,7 +44,7 @@ impl FromSql for Triple { impl ToSql for Triple { fn to_sql(&self) -> rusqlite::Result> { Ok(rusqlite::types::ToSqlOutput::Owned(rusqlite::types::Value::Integer( - i64::try_from(u64::from(self)).map_err( + i64::try_from(u64::from(*self)).map_err( |err| Error::ToSqlConversionFailure(err.into()) )? ))) diff --git a/tripod-id/src/single.rs b/tripod-id/src/single.rs index 0d8c569..016d856 100644 --- a/tripod-id/src/single.rs +++ b/tripod-id/src/single.rs @@ -91,8 +91,8 @@ fn str_to_u16(s: &str) -> Result { fn u16_to_string(int: u16) -> Result { if int >= CUBED_BASE { return Err(Error::OutsideOfRange{ - expected: CUBED_BASE as usize, - found: int as usize + expected: CUBED_BASE as u64, + found: int as u64 }) } let first_char = char::from(CHARACTERS[usize::try_from(int / SQUARED_BASE).unwrap()]); @@ -101,7 +101,7 @@ fn u16_to_string(int: u16) -> Result { Ok(format!("{}{}{}", first_char, second_char, third_char)) } -#[derive(Clone, Debug, PartialEq)] +#[derive(Copy, Clone, Debug, PartialEq)] pub struct Single{ inner: u16 } @@ -171,15 +171,15 @@ impl TryFrom for Single { Ok(Self{inner: value}) } else { Err(Error::OutsideOfRange{ - expected: Self::SIZE as usize, - found: value as usize + expected: Self::SIZE as u64, + found: value as u64 }) } } } -impl From<&Single> for u16 { - fn from(value: &Single) -> Self { +impl From for u16 { + fn from(value: Single) -> Self { value.inner } } @@ -190,16 +190,29 @@ impl From<&Single> for u16 { mod tests { use super::*; + #[cfg(feature="prost")] + fn assert_prost(id: Single) { + use crate::SingleMessage; + let message = SingleMessage::from(*id); + assert_eq!(message.is_valid()); + let result = Single::try_from(message).unwrap(); + assert_eq!(id, result); + } + + fn assert_id(id: Single) { + assert!(id.is_valid()); + let s = id.to_string(); + assert_eq!(id,Single::from_str(&s).unwrap()); + let i = u16::from(id); + assert_eq!(id, Single::try_from(i).unwrap()); + } + fn assert_random(rand: &mut R) where R: Rng { let chunk: Single = rand.r#gen(); - assert!(chunk.is_valid()); - let s = chunk.to_string(); - assert_eq!(chunk,Single::from_str(&s).unwrap()); - let i = u16::from(&chunk); - assert_eq!(chunk, Single::try_from(i).unwrap()); + } #[test] fn random_x10() { diff --git a/tripod-id/src/triple.rs b/tripod-id/src/triple.rs index b46502c..d72130b 100644 --- a/tripod-id/src/triple.rs +++ b/tripod-id/src/triple.rs @@ -6,7 +6,7 @@ use rand::{distributions::Standard, prelude::Distribution, Rng}; use crate::TripodId; -#[derive(Debug, Clone, PartialEq)] +#[derive(Copy, Clone, Debug, PartialEq)] pub struct Triple { inner: (Single, Single, Single) } @@ -38,7 +38,7 @@ impl TripodId for Triple{ #[cfg(test)] fn is_valid(&self) -> bool { - self.inner.0.is_valid() && self.inner.1.is_valid() && self.inner.2.is_valid() && (u64::from(self) < Self::SIZE) + self.inner.0.is_valid() && self.inner.1.is_valid() && self.inner.2.is_valid() && (u64::from(*self) < Self::SIZE) } } @@ -106,18 +106,18 @@ impl TryFrom for Triple { )}) } else { Err(Error::OutsideOfRange{ - expected: Self::SIZE as usize, - found: value as usize + expected: Self::SIZE as u64, + found: value as u64 }) } } } -impl From<&Triple> for u64 { - fn from(value: &Triple) -> Self { - (u16::from(&value.inner.0) as u64) * (Double::SIZE as u64) - + (u16::from(&value.inner.1) as u64) * (Single::SIZE as u64) - + (u16::from(&value.inner.2) as u64) +impl From for u64 { + fn from(value: Triple) -> Self { + (u16::from(value.inner.0) as u64) * (Double::SIZE as u64) + + (u16::from(value.inner.1) as u64) * (Single::SIZE as u64) + + (u16::from(value.inner.2) as u64) } } @@ -132,7 +132,7 @@ mod tests { let id: Triple = rand.r#gen(); assert!(id.is_valid()); assert_eq!(id, Triple::from_str(&id.to_string()).unwrap()); - assert_eq!(id, Triple::try_from(u64::from(&id)).unwrap()); + assert_eq!(id, Triple::try_from(u64::from(id)).unwrap()); } #[test] fn random_x10() {