feat: move core to tui and begin grpc work
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2024-05-11 23:23:00 +02:00
parent 86cba91b16
commit 4a0fcd1bbb
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394
25 changed files with 309 additions and 112 deletions

8
Cargo.lock generated
View File

@ -1068,17 +1068,13 @@ version = "0.1.0"
dependencies = [
"anyhow",
"axum 0.7.5",
"bus",
"clap",
"dirs",
"dotenv",
"serde",
"serde_json",
"similar-asserts",
"sqlx",
"tempfile",
"tokio",
"tower-http",
"tracing",
"tracing-subscriber",
"uuid",
@ -1118,15 +1114,19 @@ name = "hyperlog-tui"
version = "0.1.0"
dependencies = [
"anyhow",
"bus",
"crossterm",
"directories",
"dirs",
"human-panic",
"hyperlog-core",
"itertools",
"ratatui",
"ropey",
"serde",
"serde_json",
"similar-asserts",
"tempfile",
"tokio",
"tracing",
"tracing-subscriber",

View File

@ -13,18 +13,8 @@ dotenv.workspace = true
axum.workspace = true
serde = { version = "1.0.201", features = ["derive"] }
sqlx = { version = "0.7.4", features = [
"runtime-tokio",
"tls-rustls",
"postgres",
"uuid",
"time",
] }
uuid = { version = "1.8.0", features = ["v4"] }
tower-http = { version = "0.5.2", features = ["cors", "trace"] }
serde_json = "1.0.117"
bus = "2.4.1"
dirs = "5.0.1"
[dev-dependencies]
similar-asserts = "1.5.0"

View File

@ -1,11 +1 @@
#![feature(map_try_insert)]
pub mod commander;
pub mod querier;
pub mod engine;
pub mod events;
pub mod log;
pub mod shared_engine;
pub mod state;
pub mod storage;

View File

@ -3,14 +3,45 @@ syntax = "proto3";
package hyperlog;
service Graph {
rpc SayHello (HelloRequest) returns (HelloReply);
rpc Get(GetRequest) returns (GetReply);
}
message HelloRequest {
string name = 1;
message UserGraphItem {
map<string, GraphItem> items = 1;
}
message SectionGraphItem {
map<string, GraphItem> items = 1;
}
message HelloReply {
string message = 1;
enum ItemState {
UNSPECIFIED = 0;
NOT_DONE = 1;
DONE = 2;
}
message ItemGraphItem {
string title = 1;
string description = 2;
ItemState state = 3;
}
message GraphItem {
string path = 1;
oneof contents {
UserGraphItem user = 2;
SectionGraphItem section = 3;
ItemGraphItem item = 4;
}
}
message GetRequest {
string root = 1;
repeated string paths = 2;
}
message GetReply {
repeated GraphItem items = 1;
}

View File

@ -0,0 +1,97 @@
use hyperlog_core::log::{GraphItem, ItemState};
pub enum Command {
CreateRoot {
root: String,
},
CreateSection {
root: String,
path: Vec<String>,
},
CreateItem {
root: String,
path: Vec<String>,
title: String,
description: String,
state: ItemState,
},
UpdateItem {
root: String,
path: Vec<String>,
title: String,
description: String,
state: ItemState,
},
ToggleItem {
root: String,
path: Vec<String>,
},
Move {
root: String,
src: Vec<String>,
dest: Vec<String>,
},
}
pub struct Commander {}
impl Commander {
pub fn execute(&self, cmd: Command) -> anyhow::Result<()> {
match cmd {
Command::CreateRoot { root } => todo!(),
Command::CreateSection { root, path } => todo!(),
Command::CreateItem {
root,
path,
title,
description,
state,
} => todo!(),
Command::UpdateItem {
root,
path,
title,
description,
state,
} => todo!(),
Command::ToggleItem { root, path } => todo!(),
Command::Move { root, src, dest } => todo!(),
}
Ok(())
}
pub async fn create_root(&self, root: &str) -> anyhow::Result<()> {
todo!()
}
pub async fn create(&self, root: &str, path: &[&str], item: GraphItem) -> anyhow::Result<()> {
todo!()
}
pub async fn get(&self, root: &str, path: &[&str]) -> Option<GraphItem> {
todo!()
}
pub async fn section_move(
&self,
root: &str,
src_path: &[&str],
dest_path: &[&str],
) -> anyhow::Result<()> {
todo!()
}
pub async fn delete(&self, root: &str, path: &[&str]) -> anyhow::Result<()> {
todo!()
}
pub async fn update_item(
&self,
root: &str,
path: &[&str],
item: &GraphItem,
) -> anyhow::Result<()> {
todo!()
}
}

View File

@ -0,0 +1,82 @@
use std::{collections::HashMap, net::SocketAddr};
use hyperlog_protos::hyperlog::{
graph_server::{Graph, GraphServer},
*,
};
use tonic::{transport, Response};
use crate::{
querier::{Querier, QuerierExt},
state::SharedState,
};
pub struct Server {
querier: Querier,
}
impl Server {
pub fn new(querier: Querier) -> Self {
Self { querier }
}
}
#[tonic::async_trait]
impl Graph for Server {
async fn get(
&self,
request: tonic::Request<GetRequest>,
) -> std::result::Result<tonic::Response<GetReply>, tonic::Status> {
let msg = request.get_ref();
tracing::trace!("get: req({:?})", msg);
Ok(Response::new(GetReply {
items: vec![
GraphItem {
path: "some.path".into(),
contents: Some(graph_item::Contents::Item(ItemGraphItem {
title: "some-title".into(),
description: "some-description".into(),
state: ItemState::NotDone as i32,
})),
},
GraphItem {
path: "some.path.section".into(),
contents: Some(graph_item::Contents::Section(SectionGraphItem {
items: HashMap::new(),
})),
},
GraphItem {
path: "some.path".into(),
contents: Some(graph_item::Contents::User(UserGraphItem {
items: HashMap::new(),
})),
},
],
}))
}
}
pub trait ServerExt {
fn grpc_server(&self) -> Server;
}
impl ServerExt for SharedState {
fn grpc_server(&self) -> Server {
Server::new(self.querier())
}
}
pub async fn serve(state: &SharedState, host: SocketAddr) -> anyhow::Result<()> {
tracing::info!("listening on {}", host);
let graph_server = state.grpc_server();
transport::Server::builder()
.add_service(GraphServer::new(graph_server))
.serve(host)
.await?;
Ok(())
}

View File

@ -2,49 +2,13 @@ use std::{net::SocketAddr, sync::Arc};
use crate::state::{SharedState, State};
mod external_grpc;
mod external_http;
mod internal_http;
mod external_grpc {
use std::net::SocketAddr;
use hyperlog_protos::hyperlog::{
graph_server::{Graph, GraphServer},
HelloReply, HelloRequest,
};
use tonic::{transport, Response};
mod commands;
mod querier;
use crate::state::SharedState;
#[derive(Default)]
struct Server {}
#[tonic::async_trait]
impl Graph for Server {
async fn say_hello(
&self,
request: tonic::Request<HelloRequest>,
) -> std::result::Result<tonic::Response<HelloReply>, tonic::Status> {
tracing::info!("received hello request");
Ok(Response::new(HelloReply {
message: "hello".into(),
}))
}
}
pub async fn serve(state: &SharedState, host: SocketAddr) -> anyhow::Result<()> {
tracing::info!("listening on {}", host);
let graph_server = Server::default();
transport::Server::builder()
.add_service(GraphServer::new(graph_server))
.serve(host)
.await?;
Ok(())
}
}
mod state;
#[derive(Clone)]

View File

@ -0,0 +1,33 @@
use hyperlog_core::log::GraphItem;
use crate::state::SharedState;
pub struct Querier {}
impl Querier {
pub fn new() -> Self {
Self {}
}
pub fn get_available_roots(&self) -> Option<Vec<String>> {
todo!()
}
pub fn get(
&self,
root: &str,
path: impl IntoIterator<Item = impl Into<String>>,
) -> Option<GraphItem> {
todo!()
}
}
pub trait QuerierExt {
fn querier(&self) -> Querier;
}
impl QuerierExt for SharedState {
fn querier(&self) -> Querier {
Querier::new()
}
}

View File

@ -11,6 +11,7 @@ anyhow.workspace = true
tokio.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
serde.workspace = true
serde_json.workspace = true
itertools.workspace = true
@ -19,6 +20,9 @@ crossterm = { version = "0.27.0", features = ["event-stream"] }
directories = "5.0.1"
human-panic = "2.0.0"
ropey = "1.6.1"
bus = "2.4.1"
dirs = "5.0.1"
[dev-dependencies]
similar-asserts = "1.5.0"
tempfile = "3.10.1"

View File

@ -5,7 +5,7 @@ use ratatui::{
};
use crate::{
command_parser::CommandParser, commands::IntoCommand,
command_parser::CommandParser, commander, commands::IntoCommand,
components::graph_explorer::GraphExplorer, state::SharedState, Msg,
};
@ -26,7 +26,7 @@ pub enum Dialog {
}
impl Dialog {
pub fn get_command(&self) -> Option<hyperlog_core::commander::Command> {
pub fn get_command(&self) -> Option<commander::Command> {
match self {
Dialog::CreateItem { state } => state.get_command(),
Dialog::EditItem { state } => state.get_command(),

View File

@ -1,7 +1,7 @@
use itertools::Itertools;
use ratatui::{prelude::*, widgets::*};
use crate::models::Msg;
use crate::{commander, models::Msg};
use super::{InputBuffer, InputField};
@ -61,7 +61,7 @@ impl CreateItemState {
Ok(())
}
pub fn get_command(&self) -> Option<hyperlog_core::commander::Command> {
pub fn get_command(&self) -> Option<commander::Command> {
let title = self.title.string();
let description = self.description.string();
@ -69,7 +69,7 @@ impl CreateItemState {
let mut path = self.path.clone();
path.push(title.replace([' ', '.'], "-"));
Some(hyperlog_core::commander::Command::CreateItem {
Some(commander::Command::CreateItem {
root: self.root.clone(),
path,
title: title.trim().into(),

View File

@ -2,7 +2,7 @@ use hyperlog_core::log::GraphItem;
use itertools::Itertools;
use ratatui::{prelude::*, widgets::*};
use crate::models::Msg;
use crate::{commander, models::Msg};
use super::{InputBuffer, InputField};
@ -82,14 +82,14 @@ impl EditItemState {
Ok(())
}
pub fn get_command(&self) -> Option<hyperlog_core::commander::Command> {
pub fn get_command(&self) -> Option<commander::Command> {
let title = self.title.string();
let description = self.description.string();
if !title.is_empty() {
let path = self.path.clone();
Some(hyperlog_core::commander::Command::UpdateItem {
Some(commander::Command::UpdateItem {
root: self.root.clone(),
path,
title: title.trim().into(),

View File

@ -1,13 +1,9 @@
use std::collections::BTreeMap;
use hyperlog_core::log::{GraphItem, ItemState};
use serde::Serialize;
use crate::{
events::Events,
log::{GraphItem, ItemState},
shared_engine::SharedEngine,
storage::Storage,
};
use crate::{events::Events, shared_engine::SharedEngine, storage::Storage};
#[derive(Serialize, PartialEq, Eq, Debug, Clone)]
pub enum Command {

View File

@ -3,7 +3,7 @@ use hyperlog_core::log::GraphItem;
use ratatui::{prelude::*, widgets::*};
use crate::{
command_parser::Commands, components::movement_graph::GraphItemType, models::Msg,
command_parser::Commands, commander, components::movement_graph::GraphItemType, models::Msg,
state::SharedState,
};
@ -196,12 +196,12 @@ impl<'a> GraphExplorer<'a> {
let mut path = self.get_current_path();
path.push(name.replace(" ", "-").replace(".", "-"));
self.state.commander.execute(
hyperlog_core::commander::Command::CreateSection {
self.state
.commander
.execute(commander::Command::CreateSection {
root: self.inner.root.clone(),
path,
},
)?;
})?;
}
}
Commands::Edit => {
@ -247,7 +247,7 @@ impl<'a> GraphExplorer<'a> {
self.state
.commander
.execute(hyperlog_core::commander::Command::ToggleItem {
.execute(commander::Command::ToggleItem {
root: self.inner.root.to_string(),
path: self.get_current_path(),
})?;

View File

@ -1,8 +1,7 @@
use std::{collections::BTreeMap, fmt::Display};
use anyhow::{anyhow, Context};
use crate::log::{Graph, GraphItem, ItemState};
use hyperlog_core::log::{Graph, GraphItem, ItemState};
#[derive(Default)]
pub struct Engine {
@ -218,10 +217,9 @@ impl Display for Engine {
mod test {
use std::collections::BTreeMap;
use hyperlog_core::log::{GraphItem, ItemState};
use similar_asserts::assert_eq;
use crate::log::{GraphItem, ItemState};
use super::Engine;
#[test]
@ -249,7 +247,7 @@ mod test {
.create(
"kjuulh",
&["some-section"],
crate::log::GraphItem::Section(BTreeMap::default()),
GraphItem::Section(BTreeMap::default()),
)
.unwrap();
@ -275,7 +273,7 @@ mod test {
.create(
"kjuulh",
&["some-section"],
crate::log::GraphItem::Section(BTreeMap::default()),
GraphItem::Section(BTreeMap::default()),
)
.unwrap();
@ -283,7 +281,7 @@ mod test {
.create(
"kjuulh",
&["some-section", "some-sub-section"],
crate::log::GraphItem::Section(BTreeMap::default()),
GraphItem::Section(BTreeMap::default()),
)
.unwrap();

View File

@ -1,3 +1,4 @@
#![feature(map_try_insert)]
#![feature(fn_traits)]
use std::{io::Stdout, time::Duration};
@ -6,8 +7,8 @@ use anyhow::{Context, Result};
use app::{render_app, App};
use commands::IntoCommand;
use components::graph_explorer::GraphExplorer;
use core_state::State;
use crossterm::event::{self, Event, KeyCode};
use hyperlog_core::state::State;
use models::{EditMsg, Msg};
use ratatui::{backend::CrosstermBackend, Terminal};
@ -19,7 +20,16 @@ pub(crate) mod app;
pub(crate) mod command_parser;
pub(crate) mod commands;
pub(crate) mod components;
pub(crate) mod state;
pub mod commander;
pub mod core_state;
pub mod shared_engine;
pub mod state;
mod engine;
mod events;
mod querier;
mod storage;
mod logging;
mod terminal;

View File

@ -1,4 +1,6 @@
use crate::{log::GraphItem, shared_engine::SharedEngine};
use hyperlog_core::log::GraphItem;
use crate::shared_engine::SharedEngine;
pub struct Querier {
engine: SharedEngine,

View File

@ -1,6 +1,8 @@
use std::sync::{Arc, RwLock};
use crate::{engine::Engine, log::GraphItem};
use hyperlog_core::log::GraphItem;
use crate::engine::Engine;
#[derive(Clone)]
pub struct SharedEngine {

View File

@ -1,6 +1,6 @@
use std::{ops::Deref, sync::Arc};
use hyperlog_core::state::State;
use crate::core_state::State;
#[derive(Clone)]
pub struct SharedState {

View File

@ -149,10 +149,9 @@ impl Storage {
mod test {
use std::collections::BTreeMap;
use hyperlog_core::log::GraphItem;
use similar_asserts::assert_eq;
use crate::log::GraphItem;
use super::*;
#[test]

View File

@ -1,7 +1,7 @@
use std::net::SocketAddr;
use clap::{Parser, Subcommand};
use hyperlog_core::{commander, state};
use hyperlog_tui::{commander, core_state::State, state};
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
@ -90,7 +90,7 @@ pub async fn execute() -> anyhow::Result<()> {
.await?;
}
Some(Commands::Exec { commands }) => {
let state = state::State::new()?;
let state = State::new()?;
match commands {
ExecCommands::CreateRoot { root } => state
.commander
@ -109,7 +109,7 @@ pub async fn execute() -> anyhow::Result<()> {
}
}
Some(Commands::Query { commands }) => {
let state = state::State::new()?;
let state = State::new()?;
match commands {
QueryCommands::Get { root, path } => {
let res = state.querier.get(
@ -126,23 +126,23 @@ pub async fn execute() -> anyhow::Result<()> {
}
}
Some(Commands::CreateRoot { name }) => {
let state = state::State::new()?;
let state = State::new()?;
state
.commander
.execute(commander::Command::CreateRoot { root: name })?;
println!("Root was successfully created, now run:\n\n$ hyperlog");
}
Some(Commands::Info {}) => {
let state = state::State::new()?;
let state = State::new()?;
println!("graph stored at: {}", state.storage.info()?)
}
Some(Commands::ClearLock {}) => {
let state = state::State::new()?;
let state = State::new()?;
state.storage.clear_lock_file();
println!("cleared lock file");
}
None => {
let state = state::State::new()?;
let state = State::new()?;
hyperlog_tui::execute(state).await?;
}
}

View File

@ -12,4 +12,4 @@ tear_down() {
trap tear_down SIGINT
RUST_LOG=info,hyperlog=trace cargo run -F include_server -- serve
RUST_LOG=info,hyperlog=trace cargo watch -x 'run -F include_server -- serve'

View File

@ -1,4 +1,3 @@
version: "3"
services:
crdb:
restart: 'always'
@ -11,5 +10,5 @@ services:
retries: 5
start_period: '20s'
ports:
- 8080:8080
- 28080:8080
- '26257:26257'