Add Interrupt struct

This commit is contained in:
Ian Manske 2024-07-05 18:47:26 -07:00
parent de2b752771
commit 8cb842eef8
3 changed files with 44 additions and 0 deletions

View File

@ -1187,6 +1187,13 @@ pub enum ShellError {
span: Option<Span>,
},
/// Operation interrupted
#[error("Operation interrupted")]
Interrupted {
#[label("This operation was interrupted")]
span: Span,
},
/// Operation interrupted by user
#[error("Operation interrupted by user")]
InterruptedByUser {

View File

@ -0,0 +1,35 @@
use crate::{ShellError, Span};
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
#[derive(Debug, Clone)]
pub struct Interrupt {
interrupt: Option<Arc<AtomicBool>>,
}
impl Interrupt {
pub fn new(ctrlc: Arc<AtomicBool>) -> Self {
Self {
interrupt: Some(ctrlc),
}
}
pub const fn empty() -> Self {
Self { interrupt: None }
}
#[inline]
pub fn poll(&self, span: Span) -> Result<(), ShellError> {
if self
.interrupt
.as_deref()
.is_some_and(|b| b.load(Ordering::Relaxed))
{
Err(ShellError::Interrupted { span })
} else {
Ok(())
}
}
}

View File

@ -1,10 +1,12 @@
pub mod byte_stream;
mod interrupt;
pub mod list_stream;
mod metadata;
mod out_dest;
mod pipeline_data;
pub use byte_stream::*;
pub use interrupt::*;
pub use list_stream::*;
pub use metadata::*;
pub use out_dest::*;