uselesskey_core/sink/
mod.rs1use std::fmt;
2use std::path::Path;
3
4use crate::Error;
5use uselesskey_core_sink::TempArtifact as RawTempArtifact;
6
7pub struct TempArtifact {
8 inner: RawTempArtifact,
9}
10
11impl fmt::Debug for TempArtifact {
12 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13 f.debug_struct("TempArtifact")
14 .field("path", &self.inner.path())
15 .finish_non_exhaustive()
16 }
17}
18
19impl TempArtifact {
20 pub fn new_bytes(prefix: &str, suffix: &str, bytes: &[u8]) -> Result<Self, Error> {
21 let inner = RawTempArtifact::new_bytes(prefix, suffix, bytes)?;
22 Ok(Self { inner })
23 }
24
25 pub fn new_string(prefix: &str, suffix: &str, s: &str) -> Result<Self, Error> {
26 let inner = RawTempArtifact::new_string(prefix, suffix, s)?;
27 Ok(Self { inner })
28 }
29
30 pub fn path(&self) -> &Path {
31 self.inner.path()
32 }
33
34 pub fn read_to_bytes(&self) -> Result<Vec<u8>, Error> {
35 self.inner.read_to_bytes().map_err(Error::from)
36 }
37
38 pub fn read_to_string(&self) -> Result<String, Error> {
39 self.inner.read_to_string().map_err(Error::from)
40 }
41}
42
43#[cfg(test)]
44mod tests {
45 use super::TempArtifact;
46
47 #[test]
48 fn temp_artifact_string_round_trips_and_debug_mentions_path() {
49 let artifact = TempArtifact::new_string("uselesskey-", ".unit.txt", "hello-world")
50 .expect("create TempArtifact");
51
52 let dbg = format!("{artifact:?}");
53 assert!(dbg.contains("TempArtifact"));
54 assert!(dbg.contains(".unit.txt"));
55
56 let text = artifact.read_to_string().expect("read_to_string");
57 assert_eq!(text, "hello-world");
58 }
59
60 #[test]
61 fn temp_artifact_bytes_round_trip() {
62 let bytes = vec![0x01, 0x02, 0x03, 0xFF];
63 let artifact = TempArtifact::new_bytes("uselesskey-", ".unit.bin", &bytes)
64 .expect("create TempArtifact");
65
66 let read = artifact.read_to_bytes().expect("read_to_bytes");
67 assert_eq!(read, bytes);
68 }
69}