miden_mast_package/dependency/
mod.rs

1use alloc::string::String;
2
3use miden_core::utils::{
4    ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable,
5};
6#[cfg(feature = "serde")]
7use serde::{Deserialize, Serialize};
8
9use crate::Word;
10
11pub(crate) mod resolver;
12
13/// The name of a dependency
14#[derive(Debug, Clone, PartialEq, Eq, derive_more::From)]
15#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
16#[cfg_attr(feature = "serde", serde(transparent))]
17#[cfg_attr(
18    all(feature = "arbitrary", test),
19    miden_test_serde_macros::serde_test(winter_serde(true))
20)]
21pub struct DependencyName(String);
22
23#[cfg(feature = "arbitrary")]
24impl proptest::arbitrary::Arbitrary for DependencyName {
25    type Parameters = ();
26    type Strategy = proptest::prelude::BoxedStrategy<Self>;
27    fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
28        use proptest::prelude::Strategy;
29
30        let chars = proptest::char::range('a', 'z');
31        proptest::collection::vec(chars, 4..32)
32            .prop_map(|chars| Self(String::from_iter(chars)))
33            .boxed()
34    }
35}
36
37impl Serializable for DependencyName {
38    fn write_into<W: ByteWriter>(&self, target: &mut W) {
39        self.0.write_into(target);
40    }
41}
42
43impl Deserializable for DependencyName {
44    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
45        let name = String::read_from(source)?;
46        Ok(Self(name))
47    }
48}
49
50/// A package dependency
51#[derive(Debug, Clone, PartialEq, Eq)]
52#[cfg_attr(feature = "arbitrary", derive(proptest_derive::Arbitrary))]
53#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
54#[cfg_attr(
55    all(feature = "arbitrary", test),
56    miden_test_serde_macros::serde_test(winter_serde(true))
57)]
58pub struct Dependency {
59    /// The name of the dependency.
60    /// Serves as a human-readable identifier for the dependency and a search hint for the resolver
61    pub name: DependencyName,
62    /// The digest of the dependency.
63    /// Serves as an ultimate source of truth for identifying the dependency.
64    #[cfg_attr(feature = "arbitrary", proptest(value = "Word::default()"))]
65    pub digest: Word,
66}
67
68impl Serializable for Dependency {
69    fn write_into<W: ByteWriter>(&self, target: &mut W) {
70        self.name.0.write_into(target);
71        self.digest.write_into(target);
72    }
73}
74
75impl Deserializable for Dependency {
76    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
77        let name = DependencyName(String::read_from(source)?);
78        let digest = Word::read_from(source)?;
79        Ok(Self { name, digest })
80    }
81}