/ cab / runtime / value / path / blob.rs
blob.rs
 1  use std::sync::Arc;
 2  
 3  use async_trait::async_trait;
 4  use bytes::Bytes;
 5  use cyn::{
 6     Result,
 7     bail,
 8  };
 9  
10  use super::{
11     Root,
12     Subpath,
13  };
14  use crate::Value;
15  
16  #[must_use]
17  pub fn blob(config: Value) -> impl Root {
18     Blob { config }
19  }
20  
21  struct Blob {
22     config: Value,
23  }
24  
25  #[async_trait]
26  impl Root for Blob {
27     fn type_(&self) -> &'static str {
28        "blob"
29     }
30  
31     fn config(&self) -> Option<&Value> {
32        Some(&self.config)
33     }
34  
35     async fn read(self: Arc<Self>, subpath: &Subpath) -> Result<Bytes> {
36        if !subpath.is_empty() {
37           bail!("blob only contains a single leaf");
38        }
39  
40        let Value::String(ref string) = self.config else {
41           unreachable!()
42        };
43  
44        Ok(Bytes::copy_from_slice(string.as_bytes()))
45     }
46  }