-
Notifications
You must be signed in to change notification settings - Fork 27
Hide Value from user and use serde instead #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
da2c5c8
7e4ed28
da79d67
6e333cf
6b9dd24
5725fdd
f3a973c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
//! An active neovim session. | ||
use std::{ | ||
fmt, | ||
future::Future, | ||
sync::{ | ||
atomic::{AtomicU64, Ordering}, | ||
|
@@ -23,13 +24,19 @@ use crate::{ | |
}, | ||
uioptions::UiAttachOptions, | ||
}; | ||
use rmpv::Value; | ||
use rmpv::{ext::to_value, Value}; | ||
use serde::{self, Deserialize, Deserializer, Serialize}; | ||
|
||
/// Pack the given arguments into a `Vec<Value>`, suitable for using it for a | ||
/// [`call`](crate::neovim::Neovim::call) to neovim. | ||
#[macro_export] | ||
macro_rules! call_args { | ||
() => (Vec::new()); | ||
() => { | ||
{ | ||
let vec: Vec<$crate::Value> = Vec::new(); | ||
vec | ||
} | ||
}; | ||
($($e:expr), +,) => (call_args![$($e),*]); | ||
($($e:expr), +) => {{ | ||
let mut vec = Vec::new(); | ||
|
@@ -96,17 +103,31 @@ where | |
(req, fut) | ||
} | ||
|
||
async fn send_msg( | ||
/// Will panic if args is serialized into something that is not an array | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where does that happen? How can the caller ensure this doesn't happen? Could we statically guarantee that somehow? |
||
async fn send_msg<T: Serialize + fmt::Debug>( | ||
&self, | ||
method: &str, | ||
args: Vec<Value>, | ||
args: T, | ||
) -> Result<oneshot::Receiver<ResponseResult>, Box<EncodeError>> { | ||
let msgid = self.msgid_counter.fetch_add(1, Ordering::SeqCst); | ||
|
||
fn get_args<T: Serialize + fmt::Debug>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this inlined for a specific reason? I'd prefer if it weren't, unless I'm missing something. |
||
args: T, | ||
) -> Result<Vec<Value>, Box<EncodeError>> { | ||
debug!("Args value is {:?}", args); | ||
let args_value = to_value(args)?; | ||
debug!("Args value is {:?}", args_value); | ||
|
||
Ok(match args_value { | ||
Value::Array(arr) => arr, | ||
v => vec![v], | ||
}) | ||
} | ||
|
||
let req = RpcMessage::RpcRequest { | ||
msgid, | ||
method: method.to_owned(), | ||
params: args, | ||
params: get_args(args)?, | ||
}; | ||
|
||
let (sender, receiver) = oneshot::channel(); | ||
|
@@ -119,10 +140,10 @@ where | |
Ok(receiver) | ||
} | ||
|
||
pub async fn call( | ||
pub async fn call<T: Serialize + fmt::Debug>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you using |
||
&self, | ||
method: &str, | ||
args: Vec<Value>, | ||
args: T, | ||
) -> Result<Result<Value, Value>, Box<CallError>> { | ||
let receiver = self | ||
.send_msg(method, args) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
//! The auto generated API for [`neovim`](crate::neovim::Neovim) | ||
//! | ||
//! Auto generated 2020-08-18 09:13:24.551223 | ||
use std::fmt; | ||
|
||
use futures::io::AsyncWrite; | ||
use serde::Serialize; | ||
|
||
use crate::{ | ||
error::CallError, | ||
|
@@ -1009,13 +1012,17 @@ where | |
.map_err(|v| Box::new(CallError::WrongValueType(v))) | ||
} | ||
|
||
pub async fn call_function( | ||
pub async fn call_function<T: Serialize + fmt::Debug>( | ||
&self, | ||
fname: &str, | ||
args: Vec<Value>, | ||
args: T, | ||
) -> Result<Value, Box<CallError>> { | ||
|
||
#[derive(Debug, Serialize)] | ||
struct Args<'a, T: Serialize>(&'a str, T); | ||
|
||
self | ||
.call("nvim_call_function", call_args![fname, args]) | ||
.call("nvim_call_function", Args(fname, args)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there any blanket impl's we could take advantage of here? Maybe we can just use |
||
.await?? | ||
.try_unpack() | ||
.map_err(|v| Box::new(CallError::WrongValueType(v))) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's this one for? If we need an empty
Vec<Value>
I think type inference should usually figure that out when just usingvec![]
, right?