hide some generics that don't need to be generic

This commit is contained in:
Tangent Wantwight 2020-05-24 23:14:25 -04:00
parent 03e4512839
commit eb7ea021b0
3 changed files with 8 additions and 7 deletions

View file

@ -5,7 +5,7 @@ use anyhow::{Context, Error, Result};
use future::ok; use future::ok;
use futures::prelude::*; use futures::prelude::*;
use net::{ClientMessage, ServerMessage, server::greet}; use net::{ClientMessage, ServerMessage, server::greet};
use serde_json::{from_str, to_string, Value}; use serde_json::{from_str, to_string};
use std::net::ToSocketAddrs; use std::net::ToSocketAddrs;
use stream::FuturesUnordered; use stream::FuturesUnordered;
use structopt::StructOpt; use structopt::StructOpt;
@ -53,7 +53,7 @@ async fn main() -> Result<()> {
async fn handle_socket(websocket: WebSocket) -> Result<()> { async fn handle_socket(websocket: WebSocket) -> Result<()> {
let (sink, mut source) = websocket.split(); let (sink, mut source) = websocket.split();
let mut sink = sink.with(|msg: ServerMessage<Vec<Value>, Value>| { let mut sink = sink.with(|msg: ServerMessage| {
let json = to_string(&msg).expect("JSON encoding shouldn't fail"); let json = to_string(&msg).expect("JSON encoding shouldn't fail");
ok::<Message, Error>(Message::text(json)) ok::<Message, Error>(Message::text(json))
}); });
@ -64,7 +64,7 @@ async fn handle_socket(websocket: WebSocket) -> Result<()> {
match source.next().await { match source.next().await {
Some(Ok(msg)) => match msg.to_str() { Some(Ok(msg)) => match msg.to_str() {
Ok(json) => { Ok(json) => {
let msg = from_str::<ClientMessage<Value, Value>>(json).context("Parsing JSON")?; let msg = from_str::<ClientMessage>(json).context("Parsing JSON")?;
debug!("Client message: {:?}", &msg); debug!("Client message: {:?}", &msg);
} }
Err(()) => debug!("Non-text message"), Err(()) => debug!("Non-text message"),

View file

@ -1,10 +1,11 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value;
pub mod server; pub mod server;
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
#[serde(tag = "t")] #[serde(tag = "t")]
pub enum ClientMessage<I, S> { pub enum ClientMessage<I = Value, S = Value> {
#[serde(rename = "s")] #[serde(rename = "s")]
SetState { s: S }, SetState { s: S },
#[serde(rename = "i")] #[serde(rename = "i")]
@ -21,7 +22,7 @@ pub struct Meta {
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]
#[serde(tag = "t")] #[serde(tag = "t")]
pub enum ServerMessage<I, S> { pub enum ServerMessage<I = Vec<Value>, S = Value> {
#[serde(rename = "m")] #[serde(rename = "m")]
Meta { Meta {
#[serde(flatten)] #[serde(flatten)]

View file

@ -1,11 +1,11 @@
use anyhow::{Context, Error, Result}; use anyhow::{Context, Error, Result};
use super::*; use super::*;
use futures::prelude::*; use futures::prelude::*;
use serde_json::{Value, json}; use serde_json::json;
use stream::iter; use stream::iter;
pub async fn greet<T>(sink: &mut T) -> Result<()> pub async fn greet<T>(sink: &mut T) -> Result<()>
where T: Sink<ServerMessage<Vec<Value>, Value>, Error = Error> + Unpin where T: Sink<ServerMessage, Error = Error> + Unpin
{ {
let mut greeting = iter(vec![ let mut greeting = iter(vec![
ServerMessage::Meta { ServerMessage::Meta {