Skip to content

Commit

Permalink
Add support for loading services through reflection
Browse files Browse the repository at this point in the history
Closes #9
  • Loading branch information
andrewhickman committed Jun 24, 2023
1 parent a8e8f9f commit 8a5c538
Show file tree
Hide file tree
Showing 21 changed files with 589 additions and 157 deletions.
167 changes: 106 additions & 61 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ windows = { version = "0.48.0", features = ["Win32_System_LibraryLoader", "Win32
time = { version = "0.3.22", default-features = false, features = ["parsing", "serde", "serde-well-known"] }
shell-words = "1.1.0"
http-serde = "1.1.2"
tonic-reflection = "0.9.2"

[build-dependencies]
anyhow = "1.0.71"
Expand Down
5 changes: 3 additions & 2 deletions src/app/body/method/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use tonic::metadata::MetadataMap;

use crate::{
app::{
body::{fmt_connect_err, method::MethodTabState, RequestState},
command, fmt_err,
body::{method::MethodTabState, RequestState},
command,
},
error::{fmt_connect_err, fmt_err},
grpc,
json::JsonText,
widget::update_queue::{self, UpdateQueue},
Expand Down
49 changes: 7 additions & 42 deletions src/app/body/method/stream/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ use druid::{
};
use prost_reflect::{DescriptorPool, DynamicMessage, Value};
use serde::{Deserialize, Serialize};
use tonic::{metadata::MetadataMap, Code, Status};
use tonic::{metadata::MetadataMap, Status};

use crate::{
app::body::fmt_connect_err,
grpc, lens,
theme::INVALID,
widget::{code_area, error_label, Empty},
};
use crate::{
app::metadata,
error::fmt_grpc_err,
grpc,
json::{self, JsonText},
lens,
theme::INVALID,
widget::{code_area, empty, error_label},
};

#[derive(Debug, Clone, Data, Serialize, Deserialize)]
Expand Down Expand Up @@ -52,7 +51,7 @@ pub(in crate::app) fn build() -> impl Widget<State> {
code_area(false)
.env_scope(|env: &mut Env, _: &JsonText| env.set(INVALID, true))
},
|| Empty,
empty,
)
.lens(ErrorDetail::details),
)
Expand Down Expand Up @@ -191,37 +190,3 @@ fn error_details(pool: &DescriptorPool, err: &anyhow::Error) -> Option<DynamicMe

Some(payload)
}

fn fmt_grpc_err(err: &anyhow::Error) -> ArcStr {
if let Some(status) = err.downcast_ref::<Status>() {
if status.message().is_empty() {
fmt_code(status.code()).into()
} else {
format!("{}: {}", fmt_code(status.code()), status.message()).into()
}
} else {
fmt_connect_err(err)
}
}

fn fmt_code(code: Code) -> &'static str {
match code {
Code::Ok => "OK",
Code::Cancelled => "CANCELLED",
Code::Unknown => "UNKNOWN",
Code::InvalidArgument => "INVALID_ARGUMENT",
Code::DeadlineExceeded => "DEADLINE_EXCEEDED",
Code::NotFound => "NOT_FOUND",
Code::AlreadyExists => "ALREADY_EXISTS",
Code::PermissionDenied => "PERMISSION_DENIED",
Code::ResourceExhausted => "RESOURCE_EXHAUSTED",
Code::FailedPrecondition => "FAILED_PRECONDITION",
Code::Aborted => "ABORTED",
Code::OutOfRange => "OUT_OF_RANGE",
Code::Unimplemented => "UNIMPLEMENTED",
Code::Internal => "INTERNAL",
Code::Unavailable => "UNAVAILABLE",
Code::DataLoss => "DATA_LOSS",
Code::Unauthenticated => "UNAUTHENTICATED",
}
}
29 changes: 19 additions & 10 deletions src/app/body/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod reflection;

pub(in crate::app) use self::{compile::CompileOptions, method::StreamState};

use std::{collections::BTreeMap, io, mem, ops::Bound, sync::Arc};
use std::{collections::BTreeMap, mem, ops::Bound, sync::Arc};

use druid::{lens::Field, widget::ViewSwitcher, ArcStr, Data, Lens, Widget, WidgetExt as _};
use iter_set::Inclusion;
Expand All @@ -17,7 +17,7 @@ use self::{
reflection::ReflectionTabState,
};
use crate::{
app::{command, fmt_err, metadata, sidebar::service::ServiceOptions},
app::{command, metadata, sidebar::service::ServiceOptions},
json::JsonText,
widget::{tabs, TabId, TabLabelState, TabsData, TabsDataChange},
};
Expand Down Expand Up @@ -89,6 +89,19 @@ impl State {
Arc::make_mut(&mut self.tabs).insert(id, TabState::new_compile(compile_options));
}

pub fn select_or_create_reflection_tab(&mut self) {
for (&id, tab) in self.tabs.iter() {
if matches!(tab, TabState::Reflection(_)) {
self.selected = Some(id);
return;
}
}

let id = TabId::next();
self.selected = Some(id);
Arc::make_mut(&mut self.tabs).insert(id, TabState::empty_reflection());
}

pub fn select_or_create_method_tab(
&mut self,
method: &MethodDescriptor,
Expand Down Expand Up @@ -368,6 +381,10 @@ impl TabState {
TabState::Reflection(ReflectionTabState::new(options))
}

pub fn empty_reflection() -> TabState {
TabState::Reflection(ReflectionTabState::default())
}

pub fn label(&self) -> ArcStr {
match self {
TabState::Method(method) => method.method().name().into(),
Expand Down Expand Up @@ -545,11 +562,3 @@ impl TabsData for State {
&& !cmd.is(command::FINISH)
}
}

pub fn fmt_connect_err(err: &anyhow::Error) -> ArcStr {
if let Some(err) = err.root_cause().downcast_ref::<io::Error>() {
format!("failed to connect: {}", err).into()
} else {
fmt_err(err)
}
}
2 changes: 1 addition & 1 deletion src/app/body/options/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use druid::{
use once_cell::sync::Lazy;

use crate::{
app::fmt_err,
auth::AuthorizationHook,
error::fmt_err,
lens,
theme::{self, BODY_PADDING},
widget::{
Expand Down
3 changes: 2 additions & 1 deletion src/app/body/options/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use druid::{

use crate::{
app::{
body::{fmt_connect_err, options::OptionsTabState, RequestState},
body::{options::OptionsTabState, RequestState},
command,
},
error::fmt_connect_err,
grpc,
widget::update_queue::{self, UpdateQueue},
};
Expand Down
Loading

0 comments on commit 8a5c538

Please sign in to comment.