From 727861b38f067b5c162cae2158854e4aec587544 Mon Sep 17 00:00:00 2001 From: Jonas Maier <> Date: Thu, 5 Mar 2026 13:43:40 +0100 Subject: file sinks --- src/run/builtin.rs | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'src/run/builtin.rs') diff --git a/src/run/builtin.rs b/src/run/builtin.rs index feba7f9..cdeeffe 100644 --- a/src/run/builtin.rs +++ b/src/run/builtin.rs @@ -1,3 +1,5 @@ +use std::{fs::OpenOptions, path::PathBuf}; + use super::Builtin; use crate::*; @@ -43,8 +45,40 @@ impl Builtin for Re { } } +pub struct Sink { + name: &'static str, + append: bool, +} + +impl Builtin for Sink { + fn name(&self) -> &str { + self.name + } + + fn io( + &self, + args: &[BString], + stdin: &mut dyn Read, + _stdout: &mut dyn Write, + ) -> std::io::Result<()> { + let Some(path) = args.get(0) else { + // TODO exit code + return Ok(()); + }; + let path = PathBuf::from(OsStr::from_bytes(path)); + let mut file = OpenOptions::new() + .write(true) + .create(true) + .append(self.append) + .open(path)?; + std::io::copy(stdin, &mut file)?; + Ok(()) + } +} + +pub const fn sink(name: &'static str, append: bool) -> Sink { + Sink { name, append } +} // TODO // from" => todo!("read from file"), -// to" | b"into" => todo!("write into file"), -// append" => todo!("append to file"), -- cgit v1.2.3