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            .no_shrink()  // Pure random strings, no meaningful shrinking pattern
34            .boxed()
35    }
36}
37
38impl Serializable for DependencyName {
39    fn write_into<W: ByteWriter>(&self, target: &mut W) {
40        self.0.write_into(target);
41    }
42}
43
44impl Deserializable for DependencyName {
45    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
46        let name = String::read_from(source)?;
47        Ok(Self(name))
48    }
49}
50
51/// A package dependency
52#[derive(Debug, Clone, PartialEq, Eq)]
53#[cfg_attr(feature = "arbitrary", derive(proptest_derive::Arbitrary))]
54#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
55#[cfg_attr(
56    all(feature = "arbitrary", test),
57    miden_test_serde_macros::serde_test(winter_serde(true))
58)]
59pub struct Dependency {
60    /// The name of the dependency.
61    /// Serves as a human-readable identifier for the dependency and a search hint for the resolver
62    pub name: DependencyName,
63    /// The digest of the dependency.
64    /// Serves as an ultimate source of truth for identifying the dependency.
65    #[cfg_attr(feature = "arbitrary", proptest(value = "Word::default()"))]
66    pub digest: Word,
67}
68
69impl Serializable for Dependency {
70    fn write_into<W: ByteWriter>(&self, target: &mut W) {
71        self.name.0.write_into(target);
72        self.digest.write_into(target);
73    }
74}
75
76impl Deserializable for Dependency {
77    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
78        let name = DependencyName(String::read_from(source)?);
79        let digest = Word::read_from(source)?;
80        Ok(Self { name, digest })
81    }
82}