2024-05-12 21:07:21 +02:00
|
|
|
use hyperlog_core::log::ItemState;
|
2024-04-29 21:48:01 +02:00
|
|
|
use serde::Serialize;
|
2024-05-12 22:24:37 +02:00
|
|
|
use tonic::transport::Channel;
|
2024-04-29 21:48:01 +02:00
|
|
|
|
2024-05-11 23:23:00 +02:00
|
|
|
use crate::{events::Events, shared_engine::SharedEngine, storage::Storage};
|
2024-04-29 21:48:01 +02:00
|
|
|
|
2024-05-12 22:24:37 +02:00
|
|
|
mod local;
|
|
|
|
mod remote;
|
|
|
|
|
2024-04-29 21:48:01 +02:00
|
|
|
#[derive(Serialize, PartialEq, Eq, Debug, Clone)]
|
|
|
|
pub enum Command {
|
|
|
|
CreateRoot {
|
|
|
|
root: String,
|
|
|
|
},
|
|
|
|
CreateSection {
|
|
|
|
root: String,
|
|
|
|
path: Vec<String>,
|
|
|
|
},
|
|
|
|
CreateItem {
|
|
|
|
root: String,
|
|
|
|
path: Vec<String>,
|
|
|
|
title: String,
|
|
|
|
description: String,
|
|
|
|
state: ItemState,
|
|
|
|
},
|
2024-05-10 12:03:38 +02:00
|
|
|
UpdateItem {
|
|
|
|
root: String,
|
|
|
|
path: Vec<String>,
|
|
|
|
title: String,
|
|
|
|
description: String,
|
|
|
|
state: ItemState,
|
|
|
|
},
|
2024-05-09 17:02:06 +02:00
|
|
|
ToggleItem {
|
|
|
|
root: String,
|
|
|
|
path: Vec<String>,
|
|
|
|
},
|
2024-04-29 21:48:01 +02:00
|
|
|
Move {
|
|
|
|
root: String,
|
|
|
|
src: Vec<String>,
|
|
|
|
dest: Vec<String>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-05-12 21:07:21 +02:00
|
|
|
#[derive(Clone)]
|
|
|
|
enum CommanderVariant {
|
|
|
|
Local(local::Commander),
|
2024-05-12 22:24:37 +02:00
|
|
|
Remote(remote::Commander),
|
2024-05-12 21:07:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2024-04-29 21:48:01 +02:00
|
|
|
pub struct Commander {
|
2024-05-12 21:07:21 +02:00
|
|
|
variant: CommanderVariant,
|
2024-04-29 21:48:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Commander {
|
2024-05-12 21:07:21 +02:00
|
|
|
pub fn local(engine: SharedEngine, storage: Storage, events: Events) -> anyhow::Result<Self> {
|
2024-04-29 23:34:04 +02:00
|
|
|
Ok(Self {
|
2024-05-12 21:07:21 +02:00
|
|
|
variant: CommanderVariant::Local(local::Commander::new(engine, storage, events)?),
|
2024-04-29 23:34:04 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-12 22:24:37 +02:00
|
|
|
pub fn remote(channel: Channel) -> anyhow::Result<Self> {
|
|
|
|
Ok(Self {
|
|
|
|
variant: CommanderVariant::Remote(remote::Commander::new(channel)?),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-12 21:07:21 +02:00
|
|
|
pub async fn execute(&self, cmd: Command) -> anyhow::Result<()> {
|
|
|
|
match &self.variant {
|
|
|
|
CommanderVariant::Local(commander) => commander.execute(cmd),
|
2024-05-12 22:24:37 +02:00
|
|
|
CommanderVariant::Remote(commander) => commander.execute(cmd).await,
|
2024-05-12 21:07:21 +02:00
|
|
|
}
|
2024-04-29 21:48:01 +02:00
|
|
|
}
|
|
|
|
}
|