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

56 lines
1.4 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-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
pub trait TripodId {
2025-09-13 08:39:06 +09:00
type SizeType;
const NIL: Self;
const MAX: Self;
const SIZE: Self::SizeType;
2025-09-13 12:29:59 +09:00
#[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<SizeType = I> + Debug + Display + FromStr<Err=Error> + PartialEq + TryFrom<I, Error = Error> + Copy,
I: From<T> {
use crate::SingleMessage;
let message = SingleMessage::from(*id);
assert_eq!(message.is_valid());
let result = Single::try_from(message).unwrap();
assert_eq!(id, result);
}
2025-09-13 12:29:59 +09:00
fn assert_id<T, I> (id: T) where
T: TripodId<SizeType = I> + Debug + Display + FromStr<Err=Error> + PartialEq + TryFrom<I, Error = Error> + Copy,
I: From<T>
{
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());
}
}