-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from zorp-corp/jon/scry-tools
add scryutil, timer driver
- Loading branch information
Showing
5 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ pub mod http; | |
pub mod markdown; | ||
pub mod npc; | ||
pub mod one_punch; | ||
pub mod timer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?; | ||
} | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |