caretta-sync/tripod-id/src/lib.rs

82 lines
2.1 KiB
Rust
Raw Normal View History

2025-09-13 08:39:06 +09:00
mod single;
2025-09-12 09:27:14 +09:00
mod double;
mod error;
mod triple;
2025-09-13 12:29:59 +09:00
mod utils;
#[cfg(feature="rusqlite")]
mod rusqlite;
#[cfg(feature="serde")]
mod serde;
2025-09-12 09:27:14 +09:00
2025-09-25 08:04:15 +09:00
use std::{fmt::Display, str::FromStr};
2025-09-13 08:39:06 +09:00
pub use single::*;
pub use double::*;
pub use triple::*;
pub use error::*;
2025-09-23 22:20:18 +09:00
#[cfg(feature="prost")]
pub mod prost;
#[cfg(feature="prost")]
pub use prost::{ SingleMessage, DoubleMessage, TripleMessage };
2025-09-23 22:20:18 +09:00
2025-09-25 08:04:15 +09:00
pub trait TripodId: Copy + Display + TryFrom<Self::Integer, Error=Error> + FromStr<Err=Error> + PartialEq {
type Integer: From<Self>;
2025-09-13 08:39:06 +09:00
const NIL: Self;
const MAX: Self;
2025-09-25 08:04:15 +09:00
const CAPACITY: Self::Integer;
#[cfg(test)]
fn validate_inner(self) -> bool;
#[cfg(test)]
fn validate_string_convertion(self) -> Result<bool, Error> {
Ok(self == Self::from_str(&self.to_string())?)
}
#[cfg(test)]
fn validate_integer_conversion(self) -> Result<bool, Error> {
Ok(self == Self::try_from(Self::Integer::from(self))?)
}
2025-09-13 12:29:59 +09:00
#[cfg(test)]
2025-09-25 08:04:15 +09:00
fn validate_all(self) -> Result<bool, Error> {
Ok(self.validate_inner()
&& self.validate_string_convertion()?
&& self.validate_integer_conversion()?
)
}
2025-09-13 12:29:59 +09:00
}
#[cfg(test)]
mod tests {
use std::{fmt::Display, fmt::Debug, str::FromStr};
2025-09-25 08:04:15 +09:00
#[cfg(feature="prost")]
use crate::prost::TrypodIdMessage;
use super::*;
#[cfg(feature="prost")]
2025-09-25 08:04:15 +09:00
pub fn assert_valid_message<T, M>(id: &T) where
T: TripodId + Debug + Display + FromStr<Err=Error> + PartialEq + TryFrom<M, Error = Error> + Copy,
M: TrypodIdMessage<TrypodId = T> + From<T>
{
let message = M::from(*id);
assert!(message.is_valid());
assert_eq!(*id, T::try_from(message).unwrap());
}
2025-09-13 12:29:59 +09:00
2025-09-25 08:04:15 +09:00
pub fn assert_id_eq_int<T, I> (id: T, int: I ) where
T: TripodId<Integer = I> + Debug + PartialEq + TryFrom<I, Error = Error> + Copy,
I: From<T> + PartialEq + Debug + Copy
{
2025-09-25 08:04:15 +09:00
assert_eq!(id, T::try_from(int).unwrap());
assert_eq!(I::from(id), int);
}
2025-09-25 08:04:15 +09:00
pub fn assert_id_eq_str<T> (id: T, code: &str ) where
T: TripodId + Debug + Display + FromStr<Err=Error> + PartialEq + Copy,
{
assert_eq!(id, T::from_str(code).unwrap());
}
}