graphix-package-sys 0.9.0

A dataflow language for UIs and network programming, sys package
Documentation
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#![doc(
    html_logo_url = "https://graphix-lang.github.io/graphix/graphix-icon.svg",
    html_favicon_url = "https://graphix-lang.github.io/graphix/graphix-icon.svg"
)]
use arcstr::ArcStr;
use compact_str::CompactString;
use graphix_compiler::{
    errf, expr::ExprId, typ::FnType, Apply, BuiltIn, Event, ExecCtx, Node, Rt, Scope,
    UserEvent,
};
use graphix_package_core::{
    CachedArgs, CachedArgsAsync, CachedVals, EvalCached, EvalCachedAsync, ProgramArgs,
};
use graphix_rt::GXRt;
use netidx_value::{abstract_type::AbstractWrapper, Abstract, ValArray, Value};
use poolshark::local::LPooled;
use std::{
    cell::RefCell,
    cmp::Ordering,
    hash::{Hash, Hasher},
    path::{Path, PathBuf},
    pin::Pin,
    sync::{Arc, LazyLock},
    task::{Context, Poll},
};
use tempfile::TempDir;
use tokio::{
    io::{AsyncRead, AsyncWrite, ReadBuf},
    sync::Mutex,
};

pub(crate) mod dir;
pub(crate) mod dirs_mod;
pub(crate) mod fs;
pub(crate) mod io;
pub(crate) mod metadata;
pub(crate) mod net;
pub(crate) mod tcp;
pub(crate) mod time;
pub(crate) mod tls;
pub(crate) mod watch;

// ── StreamKind ─────────────────────────────────────────────────

pub enum StreamKind {
    File(tokio::fs::File),
    Tcp(tokio::net::TcpStream),
    Tls(tokio_rustls::TlsStream<tokio::net::TcpStream>),
    Stdin(tokio::io::Stdin),
    Stdout(tokio::io::Stdout),
    Stderr(tokio::io::Stderr),
}

impl std::fmt::Debug for StreamKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            StreamKind::File(_) => f.debug_tuple("File").finish(),
            StreamKind::Tcp(s) => f.debug_tuple("Tcp").field(s).finish(),
            StreamKind::Tls(_) => f.debug_tuple("Tls").finish(),
            StreamKind::Stdin(_) => f.debug_tuple("Stdin").finish(),
            StreamKind::Stdout(_) => f.debug_tuple("Stdout").finish(),
            StreamKind::Stderr(_) => f.debug_tuple("Stderr").finish(),
        }
    }
}

impl StreamKind {
    pub(crate) fn tcp_ref(&self) -> Option<&tokio::net::TcpStream> {
        match self {
            StreamKind::Tcp(s) => Some(s),
            StreamKind::Tls(s) => {
                let (tcp, _) = s.get_ref();
                Some(tcp)
            }
            _ => None,
        }
    }
}

impl AsyncRead for StreamKind {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<std::io::Result<()>> {
        match self.get_mut() {
            StreamKind::File(s) => Pin::new(s).poll_read(cx, buf),
            StreamKind::Tcp(s) => Pin::new(s).poll_read(cx, buf),
            StreamKind::Tls(s) => Pin::new(s).poll_read(cx, buf),
            StreamKind::Stdin(s) => Pin::new(s).poll_read(cx, buf),
            StreamKind::Stdout(_) | StreamKind::Stderr(_) => Poll::Ready(Err(
                std::io::Error::new(std::io::ErrorKind::Unsupported, "cannot read from stdout/stderr"),
            )),
        }
    }
}

impl AsyncWrite for StreamKind {
    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<std::io::Result<usize>> {
        match self.get_mut() {
            StreamKind::File(s) => Pin::new(s).poll_write(cx, buf),
            StreamKind::Tcp(s) => Pin::new(s).poll_write(cx, buf),
            StreamKind::Tls(s) => Pin::new(s).poll_write(cx, buf),
            StreamKind::Stdout(s) => Pin::new(s).poll_write(cx, buf),
            StreamKind::Stderr(s) => Pin::new(s).poll_write(cx, buf),
            StreamKind::Stdin(_) => Poll::Ready(Err(
                std::io::Error::new(std::io::ErrorKind::Unsupported, "cannot write to stdin"),
            )),
        }
    }

    fn poll_flush(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<std::io::Result<()>> {
        match self.get_mut() {
            StreamKind::File(s) => Pin::new(s).poll_flush(cx),
            StreamKind::Tcp(s) => Pin::new(s).poll_flush(cx),
            StreamKind::Tls(s) => Pin::new(s).poll_flush(cx),
            StreamKind::Stdout(s) => Pin::new(s).poll_flush(cx),
            StreamKind::Stderr(s) => Pin::new(s).poll_flush(cx),
            StreamKind::Stdin(_) => Poll::Ready(Ok(())),
        }
    }

    fn poll_shutdown(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<std::io::Result<()>> {
        match self.get_mut() {
            StreamKind::File(s) => Pin::new(s).poll_shutdown(cx),
            StreamKind::Tcp(s) => Pin::new(s).poll_shutdown(cx),
            StreamKind::Tls(s) => Pin::new(s).poll_shutdown(cx),
            StreamKind::Stdout(s) => Pin::new(s).poll_shutdown(cx),
            StreamKind::Stderr(s) => Pin::new(s).poll_shutdown(cx),
            StreamKind::Stdin(_) => Poll::Ready(Ok(())),
        }
    }
}

// ── StreamValue ────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub struct StreamValue {
    pub inner: Arc<Mutex<Option<StreamKind>>>,
}

impl PartialEq for StreamValue {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.inner, &other.inner)
    }
}

impl Eq for StreamValue {}

impl PartialOrd for StreamValue {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for StreamValue {
    fn cmp(&self, other: &Self) -> Ordering {
        Arc::as_ptr(&self.inner).cmp(&Arc::as_ptr(&other.inner))
    }
}

impl Hash for StreamValue {
    fn hash<H: Hasher>(&self, state: &mut H) {
        Arc::as_ptr(&self.inner).hash(state)
    }
}

graphix_package_core::impl_no_pack!(StreamValue);

pub static STREAM_WRAPPER: LazyLock<AbstractWrapper<StreamValue>> = LazyLock::new(|| {
    let id = uuid::Uuid::from_bytes([
        0xb7, 0xc8, 0xd9, 0xea, 0xfb, 0x0c, 0x4d, 0x1e, 0x2f, 0x30, 0x41, 0x52, 0x63,
        0x74, 0x85, 0x96,
    ]);
    Abstract::register::<StreamValue>(id).expect("failed to register StreamValue")
});

pub(crate) fn wrap_file(file: tokio::fs::File) -> Value {
    STREAM_WRAPPER
        .wrap(StreamValue { inner: Arc::new(Mutex::new(Some(StreamKind::File(file)))) })
}

pub(crate) fn wrap_tcp(stream: tokio::net::TcpStream) -> Value {
    STREAM_WRAPPER
        .wrap(StreamValue { inner: Arc::new(Mutex::new(Some(StreamKind::Tcp(stream)))) })
}

pub fn get_stream(
    cached: &CachedVals,
    idx: usize,
) -> Option<Arc<Mutex<Option<StreamKind>>>> {
    match cached.0.get(idx)?.as_ref()? {
        Value::Abstract(a) => {
            let sv = a.downcast_ref::<StreamValue>()?;
            Some(sv.inner.clone())
        }
        _ => None,
    }
}

pub fn get_stream_value(cached: &CachedVals, idx: usize) -> Option<StreamValue> {
    match cached.0.get(idx)?.as_ref()? {
        Value::Abstract(a) => a.downcast_ref::<StreamValue>().cloned(),
        _ => None,
    }
}

// ── TempDir ────────────────────────────────────────────────────

#[derive(Debug)]
struct TempDirValue {
    path: ArcStr,
    _dir: TempDir,
}

impl PartialEq for TempDirValue {
    fn eq(&self, other: &Self) -> bool {
        self.path == other.path
    }
}

impl Eq for TempDirValue {}

impl PartialOrd for TempDirValue {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for TempDirValue {
    fn cmp(&self, other: &Self) -> Ordering {
        self.path.cmp(&other.path)
    }
}

impl Hash for TempDirValue {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.path.hash(state)
    }
}

graphix_package_core::impl_no_pack!(TempDirValue);

static TEMPDIR_WRAPPER: LazyLock<AbstractWrapper<TempDirValue>> = LazyLock::new(|| {
    let id = uuid::Uuid::from_bytes([
        0xa1, 0xb2, 0xc3, 0xd4, 0xe5, 0xf6, 0x47, 0x89, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
        0x34, 0x56, 0x78,
    ]);
    Abstract::register::<TempDirValue>(id).expect("failed to register TempDirValue")
});

#[derive(Debug)]
enum Name {
    Prefix(ArcStr),
    Suffix(ArcStr),
}

#[derive(Debug)]
pub(crate) struct TempDirArgs {
    dir: Option<ArcStr>,
    name: Option<Name>,
}

#[derive(Debug, Default)]
pub(crate) struct GxTempDirEv;

impl EvalCachedAsync for GxTempDirEv {
    const NAME: &str = "sys_tempdir";
    const NEEDS_CALLSITE: bool = false;
    type Args = TempDirArgs;

    fn prepare_args(&mut self, cached: &CachedVals) -> Option<Self::Args> {
        if cached.0.iter().any(|v| v.is_none()) {
            None
        } else {
            let dir = cached.get::<Option<ArcStr>>(0).flatten();
            let name = cached
                .get::<Option<(ArcStr, ArcStr)>>(1)
                .and_then(|v| v)
                .and_then(|(tag, v)| match &*tag {
                    "Prefix" => Some(Name::Prefix(v)),
                    "Suffix" => Some(Name::Suffix(v)),
                    _ => None,
                });
            let _ = cached.get::<Value>(2)?;
            Some(TempDirArgs { dir, name })
        }
    }

    fn eval(args: Self::Args) -> impl Future<Output = Value> + Send {
        async move {
            let td = tokio::task::spawn_blocking(|| match (args.dir, args.name) {
                (None, None) => TempDir::new(),
                (None, Some(Name::Prefix(pfx))) => TempDir::with_prefix(&*pfx),
                (None, Some(Name::Suffix(sfx))) => TempDir::with_suffix(&*sfx),
                (Some(dir), None) => TempDir::new_in(&*dir),
                (Some(dir), Some(Name::Prefix(pfx))) => {
                    TempDir::with_prefix_in(&*pfx, &*dir)
                }
                (Some(dir), Some(Name::Suffix(sfx))) => {
                    TempDir::with_suffix_in(&*sfx, &*dir)
                }
            })
            .await;
            match td {
                Err(e) => errf!("IOError", "failed to spawn create temp dir {e:?}"),
                Ok(Err(e)) => errf!("IOError", "failed to create temp dir {e:?}"),
                Ok(Ok(td)) => {
                    use std::fmt::Write;
                    let mut buf = CompactString::new("");
                    write!(buf, "{}", td.path().display()).unwrap();
                    let path = ArcStr::from(buf.as_str());
                    TEMPDIR_WRAPPER.wrap(TempDirValue { path, _dir: td })
                }
            }
        }
    }
}

pub(crate) type GxTempDir = CachedArgsAsync<GxTempDirEv>;

#[derive(Debug, Default)]
pub(crate) struct TempDirPathEv;

impl<R: Rt, E: UserEvent> EvalCached<R, E> for TempDirPathEv {
    const NAME: &str = "sys_tempdir_path";
    const NEEDS_CALLSITE: bool = false;

    fn eval(&mut self, _ctx: &mut ExecCtx<R, E>, from: &CachedVals) -> Option<Value> {
        let v = from.0.first()?.as_ref()?;
        match v {
            Value::Abstract(a) => {
                let td = a.downcast_ref::<TempDirValue>()?;
                Some(Value::String(td.path.clone()))
            }
            _ => None,
        }
    }
}

pub(crate) type TempDirPath = CachedArgs<TempDirPathEv>;

pub(crate) fn convert_path(path: &Path) -> ArcStr {
    thread_local! {
        static BUF: RefCell<String> = RefCell::new(String::new());
    }
    BUF.with_borrow_mut(|buf| {
        buf.clear();
        use std::fmt::Write;
        write!(buf, "{}", path.display()).unwrap();
        ArcStr::from(buf.as_str())
    })
}

#[derive(Debug, Default)]
pub(crate) struct JoinPathEv;

impl<R: Rt, E: UserEvent> EvalCached<R, E> for JoinPathEv {
    const NAME: &str = "sys_join_path";
    const NEEDS_CALLSITE: bool = false;

    fn eval(&mut self, _ctx: &mut ExecCtx<R, E>, from: &CachedVals) -> Option<Value> {
        let mut parts: LPooled<Vec<ArcStr>> = LPooled::take();
        for part in from.0.iter() {
            match part {
                None => return None,
                Some(Value::String(s)) => parts.push(s.clone()),
                Some(Value::Array(a)) => {
                    for part in a.iter() {
                        match part {
                            Value::String(s) => parts.push(s.clone()),
                            _ => return None,
                        }
                    }
                }
                _ => return None,
            }
        }
        thread_local! {
            static BUF: RefCell<PathBuf> = RefCell::new(PathBuf::new());
        }
        BUF.with_borrow_mut(|path| {
            path.clear();
            for part in parts.drain(..) {
                path.push(&*part)
            }
            Some(Value::String(convert_path(&path)))
        })
    }
}

pub(crate) type JoinPath = CachedArgs<JoinPathEv>;

// ── Args ──────────────────────────────────────────────────────

#[derive(Debug)]
pub(crate) struct Args {
    fired: bool,
}

impl<R: Rt, E: UserEvent> BuiltIn<R, E> for Args {
    const NAME: &str = "sys_args";
    const NEEDS_CALLSITE: bool = false;

    fn init<'a, 'b, 'c, 'd>(
        _ctx: &'a mut ExecCtx<R, E>,
        _typ: &'a FnType,
        _resolved: Option<&'d FnType>,
        _scope: &'b Scope,
        _from: &'c [Node<R, E>],
        _top_id: ExprId,
    ) -> anyhow::Result<Box<dyn Apply<R, E>>> {
        Ok(Box::new(Self { fired: false }))
    }
}

impl<R: Rt, E: UserEvent> Apply<R, E> for Args {
    fn update(
        &mut self,
        ctx: &mut ExecCtx<R, E>,
        _from: &mut [Node<R, E>],
        event: &mut Event<E>,
    ) -> Option<Value> {
        if event.init && !self.fired {
            self.fired = true;
            let pargs = ctx.libstate.get_or_default::<ProgramArgs>();
            let arr: ValArray =
                pargs.0.iter().map(|s| Value::String(s.clone())).collect();
            Some(Value::Array(arr))
        } else {
            None
        }
    }

    fn delete(&mut self, _ctx: &mut ExecCtx<R, E>) {}

    fn sleep(&mut self, _ctx: &mut ExecCtx<R, E>) {
        self.fired = false;
    }
}

graphix_derive::defpackage! {
    builtins => [
        Args,
        GxTempDir,
        TempDirPath,
        JoinPath,
        metadata::IsFile,
        metadata::IsDir,
        metadata::Metadata,
        watch::CreateWatcher,
        watch::WatchApply,
        watch::WatchPath,
        watch::WatchEvents,
        fs::ReadAll,
        fs::ReadAllBin,
        fs::WriteAll,
        fs::WriteAllBin,
        fs::RemoveFile,
        fs::FileOpen,
        fs::FileSeek,
        fs::FileFstat,
        fs::FileTruncate,
        dir::ReadDir,
        dir::CreateDir,
        dir::RemoveDir,
        io::IoRead,
        io::IoReadExact,
        io::IoWrite,
        io::IoWriteExact,
        io::IoFlush,
        io::IoStdin,
        io::IoStdout,
        io::IoStderr,
        tcp::TcpConnect,
        tcp::TcpListen,
        tcp::TcpAccept,
        tcp::TcpShutdown,
        tcp::TcpPeerAddr,
        tcp::TcpLocalAddr,
        tcp::TcpListenerAddr,
        tls::TlsConnect,
        tls::TlsAccept,
        net::Write,
        net::Subscribe,
        net::RpcCall,
        net::List,
        net::ListTable,
        net::Publish as net::Publish<GXRt<X>, X::UserEvent>,
        net::PublishRpc as net::PublishRpc<GXRt<X>, X::UserEvent>,
        time::AfterIdle,
        time::Timer,
        time::Now,
        dirs_mod::HomeDir,
        dirs_mod::CacheDir,
        dirs_mod::ConfigDir,
        dirs_mod::ConfigLocalDir,
        dirs_mod::DataDir,
        dirs_mod::DataLocalDir,
        dirs_mod::ExecutableDir,
        dirs_mod::PreferenceDir,
        dirs_mod::RuntimeDir,
        dirs_mod::StateDir,
        dirs_mod::AudioDir,
        dirs_mod::DesktopDir,
        dirs_mod::DocumentDir,
        dirs_mod::DownloadDir,
        dirs_mod::FontDir,
        dirs_mod::PictureDir,
        dirs_mod::PublicDir,
        dirs_mod::TemplateDir,
        dirs_mod::VideoDir,
    ],
}