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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use zisk_common::io::ZiskStdin as ZiskStdinInner;
use crate::{Result, SdkError};
use serde::{de::DeserializeOwned, Serialize};
use std::path::Path;
/// Standard input for a guest program execution or proof.
#[derive(Clone)]
pub struct ZiskStdin(ZiskStdinInner);
impl ZiskStdin {
/// Creates a new empty memory-based stdin.
pub fn new() -> Self {
Self(ZiskStdinInner::new())
}
/// Creates stdin from a byte vector.
pub fn from_bytes(bytes: Vec<u8>) -> Self {
let stdin = Self(ZiskStdinInner::new());
stdin.write_slice(&bytes);
stdin
}
/// Creates stdin from a file path.
///
/// # Errors
/// Returns an error if the file does not exist, is not accessible, or the path contains
/// invalid UTF-8.
pub fn from_file(path: impl AsRef<Path>) -> Result<Self> {
Ok(Self(ZiskStdinInner::from_file(path).map_err(SdkError::backend)?))
}
/// Creates stdin from a URI string.
///
/// - `None` → empty stdin
/// - `file://path` → read from file
/// - `inline://[[1,2],[3]]` → inline input, a JSON array of u64 arrays
/// - No scheme → treated as a file path
pub fn from_uri<S: Into<String>>(uri: Option<S>) -> Result<Self> {
Ok(Self(ZiskStdinInner::from_uri(uri).map_err(SdkError::backend)?))
}
/// Reads all data from the stdin buffer.
pub fn read_data(&self) -> Vec<u8> {
self.0.read_data()
}
/// Reads and deserializes the next value from the stdin buffer.
pub fn read<T: DeserializeOwned>(&self) -> Result<T> {
self.0.read().map_err(SdkError::backend)
}
/// Reads the next `n` bytes from the stdin buffer.
pub fn read_bytes(&self) -> Vec<u8> {
self.0.read_bytes()
}
/// Appends a serialized value to the stdin buffer.
pub fn write<T: Serialize>(&self, data: &T) {
self.0.write(data);
}
/// Appends raw bytes to the stdin buffer.
pub fn write_slice(&self, data: &[u8]) {
self.0.write_slice(data);
}
/// Saves the stdin buffer contents to a file.
pub fn save(&self, path: impl AsRef<Path>) -> Result<()> {
self.0.save(path.as_ref()).map_err(SdkError::backend)
}
/// Reset internal read position so the next read starts from the beginning.
pub fn reset(&self) {
self.0.reset();
}
/// Rewind the read cursor to the beginning.
pub fn rewind(&self) {
self.0.rewind();
}
/// Clear the entire input buffer.
pub fn clear(&self) {
self.0.clear();
}
/// Consumes this wrapper and returns the underlying common `ZiskStdin`.
pub(crate) fn into_inner(self) -> ZiskStdinInner {
self.0
}
}
impl Default for ZiskStdin {
fn default() -> Self {
Self::new()
}
}