Skip to content

Commit

Permalink
implement some clippy changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilken committed Sep 15, 2018
1 parent 36f598c commit 4f54fdc
Show file tree
Hide file tree
Showing 25 changed files with 128 additions and 130 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hap"
version = "0.0.2"
version = "0.0.3"
authors = ["Elias Wilken <eliasw@me.com>"]
description = "Rust implementation of the Apple HomeKit Accessory Protocol (HAP)"
repository = "https://github.com/ewilken/hap-rs"
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl Readable<bool> for VirtualOutlet {
impl Updatable<bool> for VirtualOutlet {
fn on_update(&mut self, old_val: &bool, new_val: &bool, _: HapType) {
println!("On updated from {} to {}.", old_val, new_val);
if new_val != old_val { self.on = new_val.clone(); }
if new_val != old_val { self.on = *new_val; }
}
}

Expand Down Expand Up @@ -237,16 +237,16 @@ impl Updatable<u8> for VirtualDoor {
HapType::CurrentPosition => {
println!("Current position updated from {} to {}.", old_val, new_val);
if new_val != old_val {
self.inner.borrow_mut().current_position = new_val.clone();
self.inner.borrow_mut().current_position = *new_val;
}
},
HapType::TargetPosition => {
println!("Target position updated from {} to {}.", old_val, new_val);
if new_val != old_val {
{
let mut inner = self.inner.borrow_mut();
inner.target_position = new_val.clone();
inner.current_position = new_val.clone();
inner.target_position = *new_val;
inner.current_position = *new_val;
}
self.current_position.set_value(*new_val).unwrap();
}
Expand Down
10 changes: 5 additions & 5 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,14 @@ pub enum HapType {
impl HapType {
/// Converts a `HapType` to its corresponding shortened UUID string.
pub fn to_string(&self) -> String {
pub fn to_string(self) -> String {
match self {
&HapType::Unknown => \"unknown\".into(),
HapType::Unknown => \"unknown\".into(),
{{#each Characteristics as |c|}}\
\t\t\t&HapType::{{trim c.Name}} => \"{{uuid c.UUID}}\".into(),
\t\t\tHapType::{{trim c.Name}} => \"{{uuid c.UUID}}\".into(),
{{/each}}\
{{#each Services as |s|}}\
\t\t\t&HapType::{{trim s.Name}} => \"{{uuid s.UUID}}\".into(),
\t\t\tHapType::{{trim s.Name}} => \"{{uuid s.UUID}}\".into(),
{{/each}}\
\t\t}
}
Expand Down Expand Up @@ -533,7 +533,7 @@ pub fn new(information: Information) -> Result<{{trim service.Name}}, Error> {
{{snake_case service.Name}}.set_primary(true);
Ok({{trim service.Name}}::new({{trim service.Name}}Inner {
accessory_information: information.to_service()?,
{{snake_case service.Name}}: {{snake_case service.Name}},
{{snake_case service.Name}},
..Default::default()
}))
}
Expand Down
8 changes: 4 additions & 4 deletions examples/bridged_accessories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Readable<bool> for VirtualOutlet {
impl Updatable<bool> for VirtualOutlet {
fn on_update(&mut self, old_val: &bool, new_val: &bool, _: HapType) {
println!("Outlet: On updated from {} to {}.", old_val, new_val);
if new_val != old_val { self.inner.borrow_mut().on = new_val.clone(); }
if new_val != old_val { self.inner.borrow_mut().on = *new_val; }
}
}

Expand Down Expand Up @@ -78,16 +78,16 @@ impl Updatable<u8> for VirtualDoor {
HapType::CurrentPosition => {
println!("Door: Current position updated from {} to {}.", old_val, new_val);
if new_val != old_val {
self.inner.borrow_mut().current_position = new_val.clone();
self.inner.borrow_mut().current_position = *new_val;
}
},
HapType::TargetPosition => {
println!("Door: Target position updated from {} to {}.", old_val, new_val);
if new_val != old_val {
{
let mut inner = self.inner.borrow_mut();
inner.target_position = new_val.clone();
inner.current_position = new_val.clone();
inner.target_position = *new_val;
inner.current_position = *new_val;
}
self.current_position.set_value(*new_val).expect("couldn't set value");
}
Expand Down
2 changes: 1 addition & 1 deletion src/accessory/defined/ip_camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn new(information: Information) -> Result<IpCamera, Error> {
camera_rtp_stream_management.set_primary(true);
Ok(IpCamera::new(IpCameraInner {
accessory_information: information.to_service()?,
camera_rtp_stream_management: camera_rtp_stream_management,
camera_rtp_stream_management,
microphone: microphone::new(),
..Default::default()
}))
Expand Down
2 changes: 1 addition & 1 deletion src/accessory/defined/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn new(information: Information) -> Result<Lock, Error> {
lock_mechanism.set_primary(true);
Ok(Lock::new(LockInner {
accessory_information: information.to_service()?,
lock_mechanism: lock_mechanism,
lock_mechanism,
lock_management: lock_management::new(),
..Default::default()
}))
Expand Down
2 changes: 1 addition & 1 deletion src/accessory/defined/video_doorbell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn new(information: Information) -> Result<VideoDoorbell, Error> {
camera_rtp_stream_management.set_primary(true);
Ok(VideoDoorbell::new(VideoDoorbellInner {
accessory_information: information.to_service()?,
camera_rtp_stream_management: camera_rtp_stream_management,
camera_rtp_stream_management,
speaker: speaker::new(),
microphone: microphone::new(),
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/characteristic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl<T: Default + Clone + Serialize> Characteristic<T> where for<'de> T: Deseria
let inner = self.inner.try_borrow()?;
if inner.event_notifications == Some(true) {
if let Some(ref event_emitter) = inner.event_emitter {
event_emitter.try_borrow()?.emit(Event::CharacteristicValueChanged {
event_emitter.try_borrow()?.emit(&Event::CharacteristicValueChanged {
aid: inner.accessory_id,
iid: inner.id,
value: json!(&val),
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl Config {

pub(crate) fn save_to(&self, storage: &Storage) -> Result<(), Error> {
storage.set_bytes("device_id", self.device_id.to_hex_string().as_bytes().to_vec())?;
storage.set_u64("version", self.version.clone())?;
storage.set_u64("version", self.version)?;
if let Some(config_hash) = self.config_hash {
storage.set_u64("config_hash", config_hash)?;
}
Expand Down
10 changes: 5 additions & 5 deletions src/db/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Database {
/// Returns the stored `Device`.
pub fn get_device(&self) -> Result<Device, Error> {
let device_bytes = self.get_bytes("device")?;
let device = Device::from_bytes(device_bytes)?;
let device = Device::from_bytes(&device_bytes)?;
Ok(device)
}

Expand All @@ -60,7 +60,7 @@ impl Database {
/// Returns the stored `Pairing` for a given `Uuid`.
pub fn get_pairing(&self, id: Uuid) -> Result<Pairing, Error> {
let pairing_bytes = self.get_bytes(&id.simple().to_string())?;
let pairing = Pairing::from_bytes(pairing_bytes)?;
let pairing = Pairing::from_bytes(&pairing_bytes)?;
Ok(pairing)
}

Expand All @@ -82,9 +82,9 @@ impl Database {
pub fn list_pairings(&self) -> Result<Vec<Pairing>, Error> {
let mut pairings = Vec::new();
for key in self.storage.keys_with_suffix("entity")? {
if key != String::from("device") {
if &key != "device" {
let pairing_bytes = self.get_bytes(&key)?;
let pairing = Pairing::from_bytes(pairing_bytes)?;
let pairing = Pairing::from_bytes(&pairing_bytes)?;
pairings.push(pairing);
}
}
Expand All @@ -95,7 +95,7 @@ impl Database {
pub fn count_pairings(&self) -> Result<usize, Error> {
let mut count = 0;
for key in self.storage.keys_with_suffix("entity")? {
if key != String::from("device") {
if &key != "device" {
count += 1;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/db/file_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Storage for FileStorage {

fn set_bytes(&self, key: &str, value: Vec<u8>) -> Result<(), Error> {
let mut writer = self.get_writer(key)?;
writer.write(&value)?;
writer.write_all(&value)?;
Ok(())
}

Expand Down Expand Up @@ -114,7 +114,7 @@ impl Storage for FileStorage {

fn set_uuid(&self, key: &str, value: Uuid) -> Result<(), Error> {
let mut writer = self.get_writer(key)?;
writer.write(value.hyphenated().to_string().as_bytes())?;
writer.write_all(value.hyphenated().to_string().as_bytes())?;
Ok(())
}

Expand Down
5 changes: 3 additions & 2 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub enum Event {
CharacteristicValueChanged { aid: u64, iid: u64, value: Value }
}

#[derive(Default)]
pub struct Emitter {
listeners: Vec<Box<Fn(&Event)>>,
}
Expand All @@ -21,8 +22,8 @@ impl Emitter {
self.listeners.push(listener);
}

pub fn emit(&self, event: Event) {
for listener in self.listeners.iter() {
pub fn emit(&self, event: &Event) {
for listener in &self.listeners {
listener(&event);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use Error;

pub type Pin = String;

pub fn new(input: &String) -> Result<Pin, Error> {
pub fn new(input: &str) -> Result<Pin, Error> {
let invalid_pins: [String; 12] = [
"12345678".into(),
"87654321".into(),
Expand All @@ -17,7 +17,7 @@ pub fn new(input: &String) -> Result<Pin, Error> {
"88888888".into(),
"99999999".into(),
];
for invalid_pin in invalid_pins.iter() {
for invalid_pin in &invalid_pins {
if input == invalid_pin {
return Err(Error::new_io("invalid pin - too easy"));
}
Expand Down
18 changes: 10 additions & 8 deletions src/protocol/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ impl Device {
/// Attempts to load a `Device` from a database and creates a new one with a random key pair if
/// none is found for the given ID.
pub fn load_or_new(id: String, pin: Pin, database: &Database) -> Result<Device, Error> {
if let Some(device) = database.get_device().ok() {
return Ok(device)
match database.get_device() {
Ok(device) => Ok(device),
Err(_) => {
let device = Device::new_random(id, pin);
database.set_device(&device)?;
Ok(device)
},
}
let device = Device::new_random(id, pin);
database.set_device(&device)?;
Ok(device)
}

/// Loads a `Device` from a database.
Expand All @@ -63,9 +65,9 @@ impl Device {
Ok(value)
}

/// Deserializes a `Device` from a `Vec<u8>`.
pub fn from_bytes(bytes: Vec<u8>) -> Result<Device, Error> {
let value = serde_json::from_slice(&bytes)?;
/// Deserializes a `Device` from a `&[u8]`.
pub fn from_bytes(bytes: &[u8]) -> Result<Device, Error> {
let value = serde_json::from_slice(bytes)?;
Ok(value)
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/protocol/pairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ impl Pairing {
Ok(value)
}

/// Deserializes a `Pairing` from a `Vec<u8>`.
pub fn from_bytes(bytes: Vec<u8>) -> Result<Pairing, Error> {
/// Deserializes a `Pairing` from a `&[u8]`.
pub fn from_bytes(bytes: &[u8]) -> Result<Pairing, Error> {
let value = serde_json::from_slice(&bytes)?;
Ok(value)
}
Expand All @@ -66,9 +66,9 @@ impl Permissions {

/// Converts a `Permissions` variant to the corresponding Byte value.
pub fn as_u8(&self) -> u8 {
match self {
&Permissions::User => 0x00,
&Permissions::Admin => 0x01,
match *self {
Permissions::User => 0x00,
Permissions::Admin => 0x01,
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/protocol/tlv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use error;
/// TLVs.
pub fn encode(hm: HashMap<u8, Vec<u8>>) -> Vec<u8> {
let mut vec: Vec<u8> = Vec::new();
for (k, v) in hm.iter() {
for (k, v) in &hm {
let length = v.len();
if length <= 255 {
vec.push(k.clone());
Expand Down Expand Up @@ -56,7 +56,7 @@ pub fn decode(tlv: Vec<u8>) -> HashMap<u8, Vec<u8>> {
let t = tlv[p];
let l = tlv[p + 1];
if l < 255 {
if t != pt && buf.len() > 0 {
if t != pt && !buf.is_empty() {
hm.insert(t, buf.clone());
buf.clear();
}
Expand All @@ -69,7 +69,7 @@ pub fn decode(tlv: Vec<u8>) -> HashMap<u8, Vec<u8>> {
pt = t;
p = p + 2 + l as usize;
}
if buf.len() > 0 {
if !buf.is_empty() {
hm.insert(pt, buf.clone());
buf.clear();
}
Expand Down
8 changes: 4 additions & 4 deletions src/transport/http/handlers/characteristics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ impl JsonHandler for GetCharacteristics {
}
let (f_meta, f_perms, f_type, f_ev) = check_flags(&queries);
let q_id = queries.get("id").ok_or(Error::HttpStatus(StatusCode::BadRequest))?;
let ids = q_id.split(",").collect::<Vec<&str>>();
let ids = q_id.split(',').collect::<Vec<&str>>();
for id in ids {
let id_pair = id.split(".").collect::<Vec<&str>>();
let id_pair = id.split('.').collect::<Vec<&str>>();
if id_pair.len() != 2 {
return Err(Error::HttpStatus(StatusCode::BadRequest));
}
Expand Down Expand Up @@ -160,8 +160,8 @@ impl JsonHandler for UpdateCharacteristics {
Err(_) => {
some_err = true;
WriteResponseObject {
iid: iid,
aid: aid,
iid,
aid,
status: Status::ServiceCommunicationFailure as i32,
}
},
Expand Down
Loading

0 comments on commit 4f54fdc

Please sign in to comment.