Skip to main content

fa_bridge_sdk/
context.rs

1use std::io::Write;
2
3pub struct ActionContext {
4    pub domain: String,
5    pub action: String,
6    pub mode: RunMode,
7}
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum RunMode {
11    Oneshot,
12    Persistent,
13}
14
15impl ActionContext {
16    pub fn progress(&self, percent: u32, status: &str) -> anyhow::Result<()> {
17        if self.mode == RunMode::Persistent {
18            let msg = format!(
19                "progress\x1F{}\x1F{}\x1F{}\n",
20                self.domain,
21                self.action,
22                serde_json::to_string(&serde_json::json!({"进度": percent, "状态": status}))?
23            );
24            std::io::stdout().write_all(msg.as_bytes())?;
25            std::io::stdout().flush()?;
26        }
27        Ok(())
28    }
29}