diff options
| author | sigoden <sigoden@gmail.com> | 2024-04-29 08:33:17 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-29 08:33:17 +0800 |
| commit | 37a0cd08a92f07ef24e39bdab7c8bced5b59c146 (patch) | |
| tree | 1082ed49fa584a7f8e07763146ab0e73a475b383 /src/client/reply_handler.rs | |
| parent | 865be2bf75bb62b6aeee059f684400b4b9938a15 (diff) | |
| download | aichat-37a0cd08a92f07ef24e39bdab7c8bced5b59c146.tar.gz | |
refactor: rename some structs (#457)
Diffstat (limited to 'src/client/reply_handler.rs')
| -rw-r--r-- | src/client/reply_handler.rs | 65 |
1 files changed, 0 insertions, 65 deletions
diff --git a/src/client/reply_handler.rs b/src/client/reply_handler.rs deleted file mode 100644 index e024685..0000000 --- a/src/client/reply_handler.rs +++ /dev/null @@ -1,65 +0,0 @@ -use crate::utils::AbortSignal; - -use anyhow::{Context, Result}; -use tokio::sync::mpsc::UnboundedSender; - -pub struct ReplyHandler { - sender: UnboundedSender<ReplyEvent>, - buffer: String, - abort: AbortSignal, -} - -impl ReplyHandler { - pub fn new(sender: UnboundedSender<ReplyEvent>, abort: AbortSignal) -> Self { - Self { - sender, - abort, - buffer: String::new(), - } - } - - pub fn text(&mut self, text: &str) -> Result<()> { - // debug!("ReplyText: {}", text); - if text.is_empty() { - return Ok(()); - } - self.buffer.push_str(text); - let ret = self - .sender - .send(ReplyEvent::Text(text.to_string())) - .with_context(|| "Failed to send ReplyEvent:Text"); - self.safe_ret(ret)?; - Ok(()) - } - - pub fn done(&mut self) -> Result<()> { - // debug!("ReplyDone"); - let ret = self - .sender - .send(ReplyEvent::Done) - .with_context(|| "Failed to send ReplyEvent::Done"); - self.safe_ret(ret)?; - Ok(()) - } - - pub fn get_buffer(&self) -> &str { - &self.buffer - } - - pub fn get_abort(&self) -> AbortSignal { - self.abort.clone() - } - - fn safe_ret(&self, ret: Result<()>) -> Result<()> { - if ret.is_err() && self.abort.aborted() { - return Ok(()); - } - ret - } -} - -#[derive(Debug)] -pub enum ReplyEvent { - Text(String), - Done, -} |
