1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use crate::{hash::HashAlgId, signing::SignatureAlgId};

/// Request configuration.
#[derive(Debug, Clone)]
pub struct RequestConfig {
    signature_alg: SignatureAlgId,
    hash_alg: HashAlgId,
}

impl Default for RequestConfig {
    fn default() -> Self {
        Self::builder().build().unwrap()
    }
}

impl RequestConfig {
    /// Creates a new builder.
    pub fn builder() -> RequestConfigBuilder {
        RequestConfigBuilder::default()
    }

    /// Returns the signature algorithm.
    pub fn signature_alg(&self) -> &SignatureAlgId {
        &self.signature_alg
    }

    /// Returns the hash algorithm.
    pub fn hash_alg(&self) -> &HashAlgId {
        &self.hash_alg
    }
}

/// Builder for [`RequestConfig`].
#[derive(Debug)]
pub struct RequestConfigBuilder {
    signature_alg: SignatureAlgId,
    hash_alg: HashAlgId,
}

impl Default for RequestConfigBuilder {
    fn default() -> Self {
        Self {
            signature_alg: SignatureAlgId::SECP256K1,
            hash_alg: HashAlgId::BLAKE3,
        }
    }
}

impl RequestConfigBuilder {
    /// Sets the signature algorithm.
    pub fn signature_alg(&mut self, signature_alg: SignatureAlgId) -> &mut Self {
        self.signature_alg = signature_alg;
        self
    }

    /// Sets the hash algorithm.
    pub fn hash_alg(&mut self, hash_alg: HashAlgId) -> &mut Self {
        self.hash_alg = hash_alg;
        self
    }

    /// Builds the config.
    pub fn build(self) -> Result<RequestConfig, RequestConfigBuilderError> {
        Ok(RequestConfig {
            signature_alg: self.signature_alg,
            hash_alg: self.hash_alg,
        })
    }
}

/// Error for [`RequestConfigBuilder`].
#[derive(Debug, thiserror::Error)]
#[error("request configuration builder error: {message}")]
pub struct RequestConfigBuilderError {
    message: String,
}