Skip to content

Commit

Permalink
Merge pull request #13 from j4r0u53k/code-sanitization
Browse files Browse the repository at this point in the history
Code sanitization
  • Loading branch information
fvacek authored Mar 14, 2024
2 parents 565f192 + 4d77ef7 commit 48a1c35
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/bin/shvcall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ async fn make_call(url: &Url, opts: &Opts) -> Result {
match parse_line(line) {
Ok((path, method, param)) => {
let rqid =
send_request(&mut *frame_writer, &path, &method, &param)
send_request(&mut *frame_writer, path, method, param)
.await?;
loop {
let resp = frame_reader.receive_message().await?;
Expand Down
1 change: 1 addition & 0 deletions src/chainpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::collections::BTreeMap;
use crate::reader::{Reader, ByteReader, ReadError, ReadErrorReason};
use crate::rpcvalue::{Map, IMap};

#[allow(clippy::upper_case_acronyms)]
#[warn(non_camel_case_types)]
#[allow(dead_code)]
pub(crate) enum PackingSchema {
Expand Down
2 changes: 2 additions & 0 deletions src/metamethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ impl From<u8> for Flag {
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub enum Access { Browse = 0, Read, Write, Command, Config, Service, SuperService, Developer, Superuser }
impl Access {
// It makes sense to return Option rather than Result as the `FromStr` trait does.
#[allow(clippy::should_implement_trait)]
pub fn from_str(value: &str) -> Option<Self> {
match value {
"bws" => Some(Access::Browse),
Expand Down
2 changes: 1 addition & 1 deletion src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl SubscriptionPattern {
Self::new(paths, methods)
}
pub fn from_subscription(subscription: &Subscription) -> crate::Result<Self> {
Ok(Self::new(&subscription.paths, &subscription.methods)?)
Self::new(&subscription.paths, &subscription.methods)
}
pub fn to_rpcvalue(&self) -> RpcValue {
self.as_subscription().to_rpcvalue()
Expand Down
31 changes: 6 additions & 25 deletions src/rpcmessage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ impl RpcMessage {
self
}
pub fn error(&self) -> Option<RpcError> {
if let Some(rv) = self.key(Key::Error as i32) {
return RpcError::from_rpcvalue(rv)
}
None
self.key(Key::Error as i32).and_then(RpcError::from_rpcvalue)
}
pub fn set_error(&mut self, err: RpcError) -> &mut Self {
self.set_key(Key::Error, Some(err.to_rpcvalue()));
Expand Down Expand Up @@ -119,8 +116,7 @@ impl RpcMessage {
}
fn key(&self, key: i32) -> Option<&RpcValue> {
if let Value::IMap(m) = self.0.value() {
let v = m.get(&key);
return v;
return m.get(&key);
}
None
}
Expand Down Expand Up @@ -201,20 +197,13 @@ pub trait RpcMessageMetaTags {
self.tag(Tag::RequestId as i32).map(|rv| rv.as_i64())
}
fn try_request_id(&self) -> crate::Result<RqId> {
match self.request_id() {
None => Err("Request id not exists.".into()),
Some(id) => Ok(id),
}
self.request_id().ok_or_else(|| "Request id not exists.".into())
}
fn set_request_id(&mut self, id: RqId) -> &mut Self::Target {
self.set_tag(Tag::RequestId as i32, Some(RpcValue::from(id)))
}
fn shv_path(&self) -> Option<&str> {
let t = self.tag(Tag::ShvPath as i32);
match t {
None => None,
Some(rv) => Some(rv.as_str()),
}
self.tag(Tag::ShvPath as i32).map(RpcValue::as_str)
}
//fn shv_path_or_empty(&self) -> &str {
// self.shv_path().unwrap_or("")
Expand All @@ -223,21 +212,13 @@ pub trait RpcMessageMetaTags {
self.set_tag(Tag::ShvPath as i32, Some(RpcValue::from(shv_path)))
}
fn method(&self) -> Option<&str> {
let t = self.tag(Tag::Method as i32);
match t {
None => None,
Some(rv) => Some(rv.as_str()),
}
self.tag(Tag::Method as i32).map(RpcValue::as_str)
}
fn set_method(&mut self, method: &str) -> &mut Self::Target {
self.set_tag(Tag::Method as i32, Some(RpcValue::from(method)))
}
fn access(&self) -> Option<&str> {
let t = self.tag(Tag::Access as i32);
match t {
None => None,
Some(rv) => Some(rv.as_str()),
}
self.tag(Tag::Access as i32).map(RpcValue::as_str)
}
fn set_access(&mut self, grant: &str) -> &mut Self::Target {
self.set_tag(Tag::Access as i32, Some(RpcValue::from(grant)))
Expand Down
27 changes: 6 additions & 21 deletions src/rpcvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,21 +214,12 @@ impl RpcValue {
self.meta.is_some()
}
pub fn meta(&self) -> &MetaMap {
match &self.meta {
Some(mm) => {
mm
}
None => {
let mm = EMPTY_METAMAP.get_or_init(MetaMap::new);
mm
}
}
self.meta.as_ref().map_or_else(
|| EMPTY_METAMAP.get_or_init(MetaMap::new),
<Box<MetaMap>>::as_ref)
}
pub fn meta_mut(&mut self) -> Option<&mut MetaMap> {
match &mut self.meta {
Some(mm) => Some(mm.as_mut()),
_ => None,
}
self.meta.as_mut().map(<Box<MetaMap>>::as_mut)
}
pub fn clear_meta(&mut self) {
self.meta = None;
Expand Down Expand Up @@ -371,10 +362,7 @@ impl RpcValue {
pub fn to_cpon(&self) -> String { self.to_cpon_indented("").unwrap_or("".to_string()) }
pub fn to_cpon_indented(&self, indent: &str) -> crate::Result<String> {
let buff = self.to_cpon_bytes_indented(indent.as_bytes())?;
match String::from_utf8(buff) {
Ok(s) => Ok(s),
Err(e) => Err(e.into()),
}
String::from_utf8(buff).map_err(|e| e.into())
}
pub fn to_cpon_bytes_indented(&self, indent: &[u8]) -> crate::Result<Vec<u8>> {
let mut buff: Vec<u8> = Vec::new();
Expand All @@ -389,10 +377,7 @@ impl RpcValue {
let mut buff: Vec<u8> = Vec::new();
let mut wr = ChainPackWriter::new(&mut buff);
let r = wr.write(self);
match r {
Ok(_) => buff,
Err(_) => Vec::new(),
}
r.map_or_else(|_| Vec::new(), |_| buff)
}

pub fn from_cpon(s: &str) -> ReadResult {
Expand Down

0 comments on commit 48a1c35

Please sign in to comment.