1use std::{fmt, sync::Arc};
10
11pub use nsh_macros::JsOps;
12
13use super::host::{HostCx, HostInput, HostList, HostValue};
14use crate::ui::TsTypes;
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum OpsError {
19 Truncated,
21 NotAnOperation,
23 Unknown(i32),
25 Misplaced(i32),
28}
29
30impl fmt::Display for OpsError {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 match self {
33 OpsError::Truncated => f.write_str("truncated list of operations"),
34 OpsError::NotAnOperation => f.write_str("expected an operation"),
35 OpsError::Unknown(code) => write!(f, "unknown operation {code}"),
36 OpsError::Misplaced(code) => write!(f, "operation {code} does not belong here"),
37 }
38 }
39}
40
41impl std::error::Error for OpsError {}
42
43pub struct OpReader<'a, L> {
45 list: &'a L,
46 pos: usize,
47 end: usize,
48}
49
50impl<'a, L: HostList<Item: HostInput>> OpReader<'a, L> {
51 pub fn new(list: &'a L, start: usize, end: usize) -> Self {
53 Self {
54 list,
55 pos: start,
56 end,
57 }
58 }
59
60 pub fn all(list: &'a L) -> Self {
62 Self::new(list, 0, list.len())
63 }
64
65 #[inline]
67 pub fn more(&self) -> bool {
68 self.pos < self.end
69 }
70
71 #[inline]
73 pub fn arg(&mut self) -> Result<L::Item, OpsError> {
74 if self.pos >= self.end {
75 return Err(OpsError::Truncated);
76 }
77 let item = self.list.item(self.pos).ok_or(OpsError::Truncated)?;
78 self.pos += 1;
79 Ok(item)
80 }
81
82 #[inline]
84 pub fn code(&mut self) -> Result<i32, OpsError> {
85 self.arg()?
86 .as_number()
87 .map(|n| n as i32)
88 .ok_or(OpsError::NotAnOperation)
89 }
90}
91
92pub trait OpArg<Cx>: Sized {
96 const ARITY: usize = 1;
98 fn read<L: HostList<Item: HostInput>>(
101 r: &mut OpReader<'_, L>,
102 cx: &mut Cx,
103 ) -> Result<Option<Self>, OpsError>;
104 fn ts(types: &mut TsTypes) -> String;
106}
107
108#[derive(Debug, Clone)]
110pub struct OpParam {
111 pub name: &'static str,
113 pub ts: String,
115 pub default: Option<&'static str>,
117}
118
119#[derive(Debug, Clone)]
121pub struct OpMethod {
122 pub name: &'static str,
124 pub code: i32,
126 pub params: Vec<OpParam>,
128 pub doc: &'static str,
130}
131
132#[derive(Debug, Clone)]
135pub struct OpProperty {
136 pub name: &'static str,
138 pub code: i32,
140 pub ts: String,
142 pub initial: &'static str,
144 pub doc: &'static str,
146}
147
148pub trait JsOps: Sized {
150 type Cx;
152 fn decode<L: HostList<Item: HostInput>>(
156 code: i32,
157 r: &mut OpReader<'_, L>,
158 cx: &mut Self::Cx,
159 ) -> Result<Option<Option<Self>>, OpsError>;
160 fn methods(types: &mut TsTypes, out: &mut Vec<OpMethod>);
163 fn properties(types: &mut TsTypes, out: &mut Vec<OpProperty>);
165}
166
167impl<Cx> OpArg<Cx> for f32 {
169 fn read<L: HostList<Item: HostInput>>(
170 r: &mut OpReader<'_, L>,
171 _: &mut Cx,
172 ) -> Result<Option<Self>, OpsError> {
173 Ok(Some(r.arg()?.as_number().map_or(0.0, |n| n as f32)))
174 }
175
176 fn ts(_: &mut TsTypes) -> String {
177 "number".into()
178 }
179}
180
181impl<Cx> OpArg<Cx> for Option<f32> {
183 fn read<L: HostList<Item: HostInput>>(
184 r: &mut OpReader<'_, L>,
185 _: &mut Cx,
186 ) -> Result<Option<Self>, OpsError> {
187 Ok(Some(r.arg()?.as_number().map(|n| n as f32)))
188 }
189
190 fn ts(_: &mut TsTypes) -> String {
191 "number".into()
192 }
193}
194
195impl<Cx> OpArg<Cx> for bool {
197 fn read<L: HostList<Item: HostInput>>(
198 r: &mut OpReader<'_, L>,
199 _: &mut Cx,
200 ) -> Result<Option<Self>, OpsError> {
201 Ok(Some(r.arg()?.as_bool().unwrap_or(false)))
202 }
203
204 fn ts(_: &mut TsTypes) -> String {
205 "boolean".into()
206 }
207}
208
209impl<Cx> OpArg<Cx> for Arc<str> {
211 fn read<L: HostList<Item: HostInput>>(
212 r: &mut OpReader<'_, L>,
213 _: &mut Cx,
214 ) -> Result<Option<Self>, OpsError> {
215 Ok(Some(r.arg()?.as_string().unwrap_or_default().into()))
216 }
217
218 fn ts(_: &mut TsTypes) -> String {
219 "string".into()
220 }
221}
222
223impl<Cx, T: OpArg<Cx> + Copy + Default, const N: usize> OpArg<Cx> for [T; N] {
225 const ARITY: usize = N * T::ARITY;
226
227 fn read<L: HostList<Item: HostInput>>(
228 r: &mut OpReader<'_, L>,
229 cx: &mut Cx,
230 ) -> Result<Option<Self>, OpsError> {
231 let mut out = [T::default(); N];
232 let mut kept = true;
233 for v in &mut out {
234 match T::read(r, cx)? {
235 Some(read) => *v = read,
236 None => kept = false,
237 }
238 }
239 Ok(kept.then_some(out))
240 }
241
242 fn ts(types: &mut TsTypes) -> String {
243 T::ts(types)
244 }
245}
246
247pub(crate) fn host_value<T: HostValue, L: HostList<Item: HostInput>>(
250 r: &mut OpReader<'_, L>,
251) -> Result<Option<T>, OpsError> {
252 let item = r.arg()?;
253 Ok(T::decode(&item, &mut HostCx::default()))
254}