Skip to main content

squib_api/schemas/
entropy.rs

1//! `/entropy` PUT body — virtio-rng configuration.
2
3use serde::{Deserialize, Serialize};
4
5/// Raw `/entropy` PUT body off the wire.
6#[derive(Debug, Clone, Default, Deserialize)]
7#[serde(deny_unknown_fields)]
8pub struct RawEntropyConfig {
9    /// Optional rate limiter passthrough.
10    #[serde(default)]
11    pub rate_limiter: Option<serde_json::Value>,
12}
13
14/// Validated `/entropy` PUT body. The device has only one tunable today: a rate
15/// limiter, which is passthrough-validated by the device layer.
16#[derive(Debug, Clone, Default, Serialize)]
17#[non_exhaustive]
18pub struct EntropyConfig {
19    /// Optional rate limiter passthrough.
20    pub rate_limiter: Option<serde_json::Value>,
21}
22
23impl TryFrom<RawEntropyConfig> for EntropyConfig {
24    type Error = String;
25
26    fn try_from(raw: RawEntropyConfig) -> Result<Self, Self::Error> {
27        Ok(Self {
28            rate_limiter: raw.rate_limiter,
29        })
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36
37    #[test]
38    fn test_should_accept_empty_entropy_config() {
39        let cfg = EntropyConfig::try_from(RawEntropyConfig::default()).unwrap();
40        assert!(cfg.rate_limiter.is_none());
41    }
42
43    #[test]
44    fn test_should_reject_unknown_fields() {
45        let json = r#"{"unexpected":1}"#;
46        assert!(serde_json::from_str::<RawEntropyConfig>(json).is_err());
47    }
48}