Skip to content

Commit

Permalink
Merge pull request #120 from zorp-corp/jon/scry-tools
Browse files Browse the repository at this point in the history
add scryutil, timer driver
  • Loading branch information
drbeefsupreme authored Jan 16, 2025
2 parents 3904861 + b4ece6a commit 4a432cc
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions crown/src/drivers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub mod http;
pub mod markdown;
pub mod npc;
pub mod one_punch;
pub mod timer;
24 changes: 24 additions & 0 deletions crown/src/drivers/timer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::nockapp::driver::*;
use crate::noun::slab::NounSlab;
use crate::utils::make_tas;
use std::time::Duration;
use sword::noun::{D, T};
use tokio::time;

pub fn make_timer_driver(interval_secs: u64) -> IODriverFn {
make_driver(move |handle| async move {
let mut timer_interval = time::interval(Duration::from_secs(interval_secs));

loop {
tokio::select! {
_ = timer_interval.tick() => {
let mut timer_slab = NounSlab::new();
let timer_tas = make_tas(&mut timer_slab, "timer").as_noun();
let timer_noun = T(&mut timer_slab, &[timer_tas, D(0)]);
timer_slab.set_root(timer_noun);
handle.poke(timer_slab).await?;
}
}
}
})
}
1 change: 1 addition & 0 deletions crown/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub use drivers::http::http as http_driver;
pub use drivers::markdown::markdown as markdown_driver;
pub use drivers::npc::{npc_client as npc_client_driver, npc_listener as npc_listener_driver};
pub use drivers::one_punch::one_punch_man as one_punch_driver;
pub use drivers::timer::make_timer_driver as timer_driver;

use std::path::PathBuf;

Expand Down
1 change: 1 addition & 0 deletions crown/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod bytes;
pub mod error;
pub mod scry;
pub mod slogger;

use byteorder::{LittleEndian, ReadBytesExt};
Expand Down
50 changes: 50 additions & 0 deletions crown/src/utils/scry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use either::{Left, Right};
use sword::noun::Noun;

pub enum ScryResult {
BadPath, // ~
Nothing, // [~ ~]
Some(Noun), // [~ ~ foo]
Invalid, // anything that isn't one of the above
}

impl From<&Noun> for ScryResult {
fn from(noun: &Noun) -> ScryResult {
match noun.as_either_atom_cell() {
Left(atom) => {
let Ok(direct) = atom.as_direct() else {
return ScryResult::Invalid;
};
if direct.data() == 0 {
return ScryResult::BadPath;
}
}
Right(cell) => {
let Ok(head) = cell.head().as_direct() else {
return ScryResult::Invalid;
};
if head.data() == 0 {
match cell.tail().as_either_atom_cell() {
Left(atom) => {
let Ok(direct) = atom.as_direct() else {
return ScryResult::Invalid;
};
if direct.data() == 0 {
return ScryResult::Nothing;
}
}
Right(tail) => {
let Ok(tail_head) = tail.head().as_direct() else {
return ScryResult::Invalid;
};
if tail_head.data() == 0 {
return ScryResult::Some(tail.tail());
}
}
}
}
}
}
ScryResult::Invalid
}
}

0 comments on commit 4a432cc

Please sign in to comment.