use anyhow::Context;
use bc_envelope::prelude::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NonHardenedChildIndex(u32);
impl From<u32> for NonHardenedChildIndex {
fn from(value: u32) -> Self {
Self(value)
}
}
impl From<NonHardenedChildIndex> for u32 {
fn from(value: NonHardenedChildIndex) -> Self {
value.0
}
}
impl From<usize> for NonHardenedChildIndex {
fn from(value: usize) -> Self {
Self(value as u32)
}
}
impl From<NonHardenedChildIndex> for CBOR {
fn from(value: NonHardenedChildIndex) -> Self {
CBOR::from(value.0)
}
}
impl From<&NonHardenedChildIndex> for CBOR {
fn from(value: &NonHardenedChildIndex) -> Self {
CBOR::from(value.0)
}
}
impl TryFrom<CBOR> for NonHardenedChildIndex {
type Error = dcbor::Error;
fn try_from(value: CBOR) -> dcbor::Result<Self> {
let position: u32 = value.try_into()?;
Ok(NonHardenedChildIndex(position))
}
}
impl From<NonHardenedChildIndex> for Envelope {
fn from(value: NonHardenedChildIndex) -> Self {
Envelope::new(CBOR::from(value))
}
}
impl TryFrom<Envelope> for NonHardenedChildIndex {
type Error = anyhow::Error;
fn try_from(envelope: Envelope) -> Result<Self, Self::Error> {
envelope.extract_subject().context("NonHardenedChildIndex")
}
}
#[cfg(test)]
mod tests {
use crate::{test_cbor_roundtrip, test_envelope_roundtrip};
use super::NonHardenedChildIndex;
impl crate::RandomInstance for NonHardenedChildIndex {
fn random() -> Self {
Self(u32::random())
}
}
test_cbor_roundtrip!(NonHardenedChildIndex);
test_envelope_roundtrip!(NonHardenedChildIndex);
}