style: code style

This commit is contained in:
2020-12-27 14:51:24 +08:00
parent 8d34b85a9c
commit cc10c8effd
4 changed files with 436 additions and 84 deletions

View File

@@ -1,4 +1,4 @@
use crate::{PluginId,WhichPlugin};
use crate::{PluginId, WhichPlugin};
use std::fmt;
use serde::{
@@ -11,8 +11,8 @@ use abi_stable::{StableAbi, std_types::*};
// This is intentionally not `#[derive(StableAbi)]`,
// since it can be extended in minor versions of the interface.
// I has to be serialized to pass it through ffi.
#[derive(Debug,Clone,PartialEq,Eq,Serialize,Deserialize)]
pub enum BasicCommand{
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum BasicCommand {
GetCommands,
}
@@ -21,8 +21,8 @@ pub enum BasicCommand{
// This is intentionally not `#[derive(StableAbi)]`,
// since it can be extended in minor versions of the interface.
// I has to be serialized to pass it through ffi.
#[derive(Debug,Clone,PartialEq,Eq,Serialize,Deserialize)]
pub enum BasicRetVal{
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum BasicRetVal {
GetCommands(RVec<CommandDescription>),
}
@@ -30,17 +30,17 @@ pub enum BasicRetVal{
// This is intentionally not `#[derive(StableAbi)]`,
// since it can be extended in minor versions of the interface.
// I has to be serialized to pass it through ffi.
#[derive(Debug,Clone,PartialEq,Eq,Serialize,Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CommandUnion<T>{
pub enum CommandUnion<T> {
ForPlugin(T),
Basic(BasicCommand),
}
#[derive(Debug,Clone,PartialEq,Eq,Serialize,Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ReturnValUnion<T>{
pub enum ReturnValUnion<T> {
ForPlugin(T),
Basic(BasicRetVal),
}
@@ -51,15 +51,15 @@ pub enum ReturnValUnion<T>{
/// A partially deserialize command,that only deserialized its variant.
#[derive(Debug,Clone)]
pub struct WhichVariant{
#[derive(Debug, Clone)]
pub struct WhichVariant {
pub variant:RString,
}
struct WhichVariantVisitor;
impl<'de> Visitor<'de> for WhichVariantVisitor{
impl<'de> Visitor<'de> for WhichVariantVisitor {
type Value = WhichVariant;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
@@ -108,33 +108,33 @@ impl<'de> Deserialize<'de> for WhichVariant{
/// Denotes this as a command type.
pub trait CommandTrait:Serialize{
pub trait CommandTrait:Serialize {
type Returns:DeserializeOwned;
}
impl CommandTrait for BasicCommand{
type Returns=BasicRetVal;
impl CommandTrait for BasicCommand {
type Returns = BasicRetVal;
}
/// Describes a command.
#[repr(C)]
#[derive(Debug,Clone,PartialEq,Eq,Serialize,Deserialize,StableAbi)]
pub struct CommandDescription{
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, StableAbi)]
pub struct CommandDescription {
/// A description of what this command does.
pub name:RCow<'static,str>,
pub name:RCow<'static, str>,
/// A description of what this command does,
/// optionally with a description of the command format.
pub description:RCow<'static,str>,
pub description:RCow<'static, str>,
}
impl CommandDescription{
impl CommandDescription {
pub fn from_literals(
name:&'static str,
description:&'static str,
)->Self{
CommandDescription{
) -> Self {
CommandDescription {
name:name.into(),
description:description.into(),
}
@@ -146,8 +146,8 @@ impl CommandDescription{
#[repr(C)]
#[derive(Debug,Clone,PartialEq,Eq,StableAbi)]
pub struct AsyncCommand{
#[derive(Debug, Clone, PartialEq, Eq, StableAbi)]
pub struct AsyncCommand {
pub from:PluginId,
pub which_plugin:WhichPlugin,
pub command:RString,

View File

@@ -1,11 +1,11 @@
use crate::{commands::CommandDescription,WhichPlugin,WhichCommandRet};
use crate::{commands::CommandDescription, WhichPlugin, WhichCommandRet};
use abi_stable::{StableAbi, std_types::{RBoxError,RBox,RString,RVec}};
use std::{error::Error as ErrorTrait, fmt::{self,Display}};
use abi_stable::{StableAbi, std_types::{RBoxError, RBox, RString, RVec}};
use std::{error::Error as ErrorTrait, fmt::{self, Display}};
use core_extensions::strings::StringExt;
#[repr(u8)]
#[derive(Debug,StableAbi)]
#[derive(Debug, StableAbi)]
pub enum Error{
/// An error produced by `serde_json::to_string`.
Serialize(RBoxError,WhichCommandRet),
@@ -27,8 +27,8 @@ pub enum Error{
/// Represents a command or return value that wasn't supported.
#[repr(C)]
#[derive(Debug,StableAbi)]
pub struct Unsupported{
#[derive(Debug, StableAbi)]
pub struct Unsupported {
/// The name of the plugin for which the command/return value wasn't supported.
pub plugin_name:RString,
/// The command/return value that wasn't supported.
@@ -40,33 +40,33 @@ pub struct Unsupported{
}
impl Error{
pub fn unsupported_command(what:Unsupported)->Self{
impl Error {
pub fn unsupported_command(what:Unsupported) -> Self {
Error::UnsupportedCommand(RBox::new(what))
}
pub fn unsupported_return_value(what:Unsupported)->Self{
pub fn unsupported_return_value(what:Unsupported) -> Self {
Error::UnsupportedReturnValue(RBox::new(what))
}
}
impl Display for Error{
fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result {
impl Display for Error {
fn fmt(&self, f:&mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Serialize(e,which)=>{
let which=match which {
WhichCommandRet::Command=>"command",
WhichCommandRet::Return=>"return value",
Error::Serialize(e, which) => {
let which = match which {
WhichCommandRet::Command => "command",
WhichCommandRet::Return => "return value",
};
writeln!(f,"Error happened while serializing the {}:\n{}\n",which,e)
}
Error::Deserialize(e,which)=>{
let which=match which {
WhichCommandRet::Command=>"command",
WhichCommandRet::Return=>"return value",
Error::Deserialize(e, which) => {
let which = match which {
WhichCommandRet::Command => "command",
WhichCommandRet::Return => "return value",
};
writeln!(f,"Error happened while deserializing {}:\n{}\n",which,e)
writeln!(f,"Error happened while deserializing {}:\n{}\n", which, e)
}
Error::UnsupportedCommand(v)=>{
Error::UnsupportedCommand(v) => {
writeln!(
f,
"Plugin '{}' ooes not support this command:\n\
@@ -77,7 +77,6 @@ impl Display for Error{
v.plugin_name,
v.command_name,
v.error,
)?;
for supported in &v.supported_commands {
@@ -94,7 +93,7 @@ impl Display for Error{
Ok(())
}
Error::UnsupportedReturnValue(v)=>
Error::UnsupportedReturnValue(v) =>
writeln!(
f,
"Unrecognized return value from '{}',named:\n\
@@ -105,14 +104,14 @@ impl Display for Error{
v.command_name,
v.error,
),
Error::InvalidPlugin(wc)=>
Error::InvalidPlugin(wc) =>
writeln!(
f,
"Attempted to access a nonexistent plugin with the WhichPlugin:\n\t{:?}\n",
wc
),
Error::Custom(e)=>Display::fmt(e,f),
Error::Many(list)=>{
Error::Custom(e) => Display::fmt(e, f),
Error::Many(list) => {
for e in list {
writeln!(f,"{}",e)?;
}

View File

@@ -19,23 +19,21 @@ use abi_stable::{
external_types::{
crossbeam_channel::RSender,
},
std_types::{RBox, RCow, RVec, RStr, RString,RResult, ROption, ROk,RSome},
std_types::{RBox, RCow, RVec, RStr, RString, RResult, ROption, ROk, RSome},
};
use serde::{Serialize,Deserialize};
use serde::{Serialize, Deserialize};
mod commands;
mod error;
mod which_plugin;
mod vec_from_map;
pub mod utils;
pub mod utils;
pub use self::{
commands::{
BasicCommand,BasicRetVal,CommandDescription,CommandTrait,WhichVariant,AsyncCommand,
BasicCommand, BasicRetVal, CommandDescription, CommandTrait, WhichVariant, AsyncCommand,
},
error::{Error,Unsupported},
error::{Error, Unsupported},
which_plugin::WhichPlugin,
vec_from_map::VecFromMap,
};
@@ -48,8 +46,8 @@ pub use self::{
The identifier for a plugin.
*/
#[repr(C)]
#[derive(Debug,Clone,PartialEq,Eq,StableAbi,Serialize,Deserialize)]
pub struct PluginId{
#[derive(Debug, Clone, PartialEq, Eq, StableAbi, Serialize, Deserialize)]
pub struct PluginId {
pub named:RCow<'static,str>,
/// The number of the instance of this Plugin.
pub instance:u64,
@@ -58,8 +56,8 @@ pub struct PluginId{
/// Describes whether a boxed error is a command or a return value.
#[repr(u8)]
#[derive(Debug,Clone,PartialEq,Eq,StableAbi,Serialize,Deserialize)]
pub enum WhichCommandRet{
#[derive(Debug, Clone, PartialEq, Eq, StableAbi, Serialize, Deserialize)]
pub enum WhichCommandRet {
Command,
Return,
}
@@ -67,21 +65,21 @@ pub enum WhichCommandRet{
/// The response from having called `ApplicationMut::send_command_to_plugin` ealier.
#[repr(C)]
#[derive(Debug,Clone,PartialEq,Eq,StableAbi)]
pub struct PluginResponse<'a>{
#[derive(Debug, Clone, PartialEq, Eq, StableAbi)]
pub struct PluginResponse<'a> {
/// The id of the plugin that is responding.
pub plugin_id:PluginId,
/// The response from the plugin
pub response:RCow<'a,str>,
pub response:RCow<'a, str>,
}
impl<'a> PluginResponse<'a>{
pub fn owned_response(plugin_id:PluginId,response:RString)->Self{
Self{plugin_id,response:response.into()}
impl<'a> PluginResponse<'a> {
pub fn owned_response(plugin_id: PluginId, response: RString) -> Self {
Self{plugin_id, response: response.into()}
}
pub fn borrowed_response(plugin_id:PluginId,response:RStr<'a>)->Self{
Self{plugin_id,response:response.into()}
pub fn borrowed_response(plugin_id: PluginId, response: RStr<'a>) -> Self {
Self{plugin_id, response: response.into()}
}
}
@@ -90,7 +88,7 @@ impl<'a> PluginResponse<'a>{
///////////////////////////////////////////////////////////////////////////////
pub type PluginType=Plugin_TO<'static,RBox<()>>;
pub type PluginType=Plugin_TO<'static, RBox<()>>;
/**
@@ -107,7 +105,7 @@ pub trait Plugin {
&mut self,
command: RStr<'_>,
app:ApplicationMut<'_>,
)->RResult<RString,Error>;
) -> RResult<RString, Error>;
/// Handles a response from another Plugin,
/// from having called `ApplicationMut::send_command_to_plugin` ealier.
@@ -115,15 +113,15 @@ pub trait Plugin {
&mut self,
response:PluginResponse<'a>,
_app:ApplicationMut<'_>,
)->RResult<ROption<PluginResponse<'a>>,Error>{
) -> RResult<ROption<PluginResponse<'a>>, Error> {
ROk(RSome(response))
}
/// Gets the PluginId that was passed to this plugin in its constructor.
fn plugin_id(&self)->&PluginId;
fn plugin_id(&self) -> &PluginId;
/// Gets a description of all commands from this Plugin.
fn list_commands(&self)->RVec<CommandDescription>;
fn list_commands(&self) -> RVec<CommandDescription>;
/*
Closes the plugin,
@@ -144,7 +142,7 @@ bumps its "major" version,
at which point it would be moved to the last method at the time.
*/
#[sabi(last_prefix_field)]
fn close(self,app:ApplicationMut<'_>);
fn close(self, app:ApplicationMut<'_>);
}
@@ -174,7 +172,7 @@ at which point it would be moved to the last field at the time.
*/
#[sabi(last_prefix_field)]
pub new: extern "C" fn(RSender<AsyncCommand>,PluginId) -> RResult<PluginType,Error>,
pub new: extern "C" fn(RSender<AsyncCommand>, PluginId) -> RResult<PluginType, Error>,
}
@@ -191,7 +189,7 @@ impl RootModule for PluginMod_Ref {
/// A mutable reference to the application implementation.
pub type ApplicationMut<'a>=Application_TO<'a,&'a mut ()>;
pub type ApplicationMut<'a> = Application_TO<'a, &'a mut ()>;
#[sabi_trait]
@@ -204,10 +202,10 @@ pub trait Application{
/// Returns an `Error::InvalidPlugin` if `which_plugin` is invalid.
fn send_command_to_plugin(
&mut self,
from:&PluginId,
which_plugin:WhichPlugin,
command:RString,
)->RResult<(),Error>;
from: &PluginId,
which_plugin: WhichPlugin,
command: RString,
) -> RResult<(), Error>;
/**
Gets the `PluginId`s of the plugins specified by `which_plugin`.
@@ -224,11 +222,11 @@ at which point it would be moved to the last method at the time.
*/
#[sabi(last_prefix_field)]
fn get_plugin_id(&self,which_plugin:WhichPlugin)->RResult<RVec<PluginId>,Error>;
fn get_plugin_id(&self, which_plugin:WhichPlugin) -> RResult<RVec<PluginId>, Error>;
/// Gets the sender end of a channel to send commands to the application/other plugins.
fn sender(&self)->RSender<AsyncCommand>;
fn sender(&self) -> RSender<AsyncCommand>;
/// Gets the PluginId of all loaded plugins
fn loaded_plugins(&self)->RVec<PluginId>;
fn loaded_plugins(&self) -> RVec<PluginId>;
}