remove hex dep
Some checks failed
buildbot/cargo fmt (1.72) Build done.
buildbot/cargo doc (1.72) Build done.
buildbot/cargo clippy (1.72) Build done.
buildbot/cargo test (1.65) Build done.
buildbot/cargo clippy (1.65) Build done.

This commit is contained in:
AF 2023-09-03 18:42:25 +00:00
parent a43f1dbf27
commit dc229363af
4 changed files with 91 additions and 21 deletions

View File

@ -10,7 +10,6 @@ members = ["radn-derive"]
[workspace.dependencies]
futures = "0.3.26"
hex = "0.4.3"
sha2 = "0.10.6"
rand = "0.8.5"
quote = "1"
@ -18,7 +17,6 @@ syn = "2"
[dependencies]
futures.workspace = true
hex.workspace = true
radn-derive.path = "radn-derive"
[dev-dependencies]

View File

@ -22,9 +22,26 @@ use crate::func::*;
use crate::mode::*;
use crate::rcore::*;
struct Hex<'a> {
point: &'a Hash,
}
impl Display for Hex<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for byte in self.point {
write!(f, "{byte:02x}")?
}
Ok(())
}
}
pub(crate) fn hex(point: &Hash) -> impl '_ + Display {
Hex { point }
}
impl Display for Address {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}@{}", hex::encode(self.point), self.index)
write!(f, "{}@{}", hex(&self.point), self.index)
}
}

View File

@ -69,8 +69,8 @@ impl<'a> Display for CastError<'a> {
f,
"address mismatch at index {}: {}!={}",
index,
hex::encode(expected),
hex::encode(received),
hex(expected),
hex(received),
),
}
}

View File

@ -20,36 +20,91 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> SingularResolution<'a, Ctx>
}
}
/// ```rust
/// # use radn::rcore::Context;
/// # use radn::rcore::Factory;
/// # use radn::rcore::FactoryExt;
/// # use radn::rcore::Mentionable;
/// # use radn::rstd::ResolverExt;
/// # use radn::rstd::SerializableExt;
/// # use radn::rstd::singular::SingularResolver;
/// # fn test<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>>(a: &A) {
/// let resolver = SingularResolver::from_mentionable(a).into_rc();
/// let bytes = a.bytes();
/// let factory = a.factory();
/// factory.parse_slice(&bytes, &resolver);
/// # }
/// ```
pub struct SingularResolver<'a, Ctx: Context<'a>> {
points: Vec<Arc<dyn SingularResolution<'a, Ctx>>>,
}
#[derive(Debug)]
pub enum SingularityError {
OutOfBounds {
index: usize,
len: usize,
},
Mismatch {
index: usize,
expected: Hash,
point: Hash,
},
}
impl Display for SingularityError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::OutOfBounds { index, len } => {
write!(f, "singularity out-of-bounds: {index}/{len}")
}
Self::Mismatch {
index,
expected,
point,
} => write!(
f,
"address mismatch at index {index}: {}!={}",
hex(expected),
hex(point),
),
}
}
}
impl<'a, Ctx: Context<'a>> SingularResolver<'a, Ctx> {
pub fn from_mentionable<A: Mentionable<'a, Ctx>>(a: &A) -> Self {
let mut points = Vec::new();
a.points_typed(&mut points);
Self { points }
}
fn resolve_robust(
self: Arc<Self>,
address: Address,
) -> Result<HashResolution<'a, Ctx>, SingularityError> {
let point =
self.points
.get(address.index)
.ok_or_else(|| SingularityError::OutOfBounds {
index: address.index,
len: self.points.len(),
})?;
if point.s_hash() != address.point {
Err(SingularityError::Mismatch {
index: address.index,
expected: address.point,
point: point.s_hash(),
})
} else {
Ok(point.clone().singular())
}
}
}
impl<'a, Ctx: Context<'a>> Resolver<'a, Ctx> for SingularResolver<'a, Ctx> {
fn resolve(self: Arc<Self>, address: Address) -> HashResolution<'a, Ctx> {
let point = self.points.get(address.index).unwrap_or_else(|| {
panic!(
"singularity out-of-bounds: {}/{}",
address.index,
self.points.len()
)
});
if point.s_hash() != address.point {
panic!(
"address mismatch at index {}: {}!={}",
address.index,
hex::encode(address.point),
hex::encode(point.s_hash()),
)
}
point.clone().singular()
self.resolve_robust(address).unwrap()
}
}