Skip to content

Commit

Permalink
Fixing clippy iotedged (#3696)
Browse files Browse the repository at this point in the history
* Fix clipppy

* Fixing clippy

* Fixing review comments

* fixing forgotten map_or_else

* fix clippy test

Co-authored-by: Ubuntu <iotedgeuser@hug3l3edge.0x1kslxyg1putjewhwmoznt1le.yx.internal.cloudapp.net>
Co-authored-by: Ubuntu <azureuser@HuG1.2bw1qssgs53udfdpqpfa4uhcsd.yx.internal.cloudapp.net>
  • Loading branch information
3 people committed Oct 12, 2020
1 parent f5e8861 commit 7831fc5
Show file tree
Hide file tree
Showing 18 changed files with 205 additions and 188 deletions.
29 changes: 14 additions & 15 deletions edgelet/dps/src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,16 @@ where
operation_id: &str,
token_source: Option<DpsTokenSource<K>>,
) -> Box<dyn Future<Item = Option<DeviceRegistrationResult>, Error = Error> + Send> {
let c = if let Some(ts) = token_source {
client
.read()
.expect("RwLock read failure")
.clone()
.with_token_source(ts)
} else {
client.read().expect("RwLock read failure").clone()
};
let c = token_source.map_or_else(
|| client.read().expect("RwLock read failure").clone(),
|ts| {
client
.read()
.expect("RwLock read failure")
.clone()
.with_token_source(ts)
},
);
let request = c.request::<(), RegistrationOperationStatus>(
Method::GET,
&format!(
Expand Down Expand Up @@ -516,15 +517,13 @@ where
};
match token_key {
Ok(tk) => {
let ts = if let Some(k) = tk {
Some(DpsTokenSource::new(
let ts = tk.map(|k| {
DpsTokenSource::new(
scope_id.to_string(),
registration_id.clone(),
k,
))
} else {
None
};
)
});
Either::A(Self::get_device_registration_result(
client_with_token_status,
scope_id_status,
Expand Down
57 changes: 30 additions & 27 deletions edgelet/edgelet-core/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ where
}

fn read_remaining(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let (result, r) = if let Some(ref remaining) = self.remaining {
let (result, r) = self.remaining.as_ref().map_or((Ok(0), None), |remaining| {
let amt = cmp::min(remaining.len(), buf.len());
let (a, b) = remaining.split_at(amt);
buf[..amt].copy_from_slice(a);
Expand All @@ -112,9 +112,8 @@ where
} else {
(Ok(amt), Some(Bytes::from(b)))
}
} else {
(Ok(0), None)
};
});

self.remaining = r;
result
}
Expand All @@ -129,29 +128,33 @@ where
match self.inner.poll() {
Ok(Async::Ready(Some(ref t))) => {
let read = self.read_remaining(buf)?;
let (result, new_remaining) = if let Some(ref mut remaining) = self.remaining {
// There's still some data waiting to be written into the read buffer.
// Append to the remaining buffer
// Return the amount read from remaining
let mut r = BytesMut::with_capacity(remaining.len() + t.as_ref().len());
r.put_slice(remaining);
r.put_slice(t.as_ref());
(Ok(read), Some(r.freeze()))
} else {
// The remaining buffer was cleared.
// Attempt to read everything from the poll and add to the remaining buffer
// if needed.
let data = Bytes::from(t.as_ref());
let amt = cmp::min(data.len(), buf.len() - read);
let (a, b) = data.split_at(amt);
buf[read..read + amt].copy_from_slice(a);

if b.is_empty() {
(Ok(amt + read), None)
} else {
(Ok(amt + read), Some(Bytes::from(b)))
}
};
let (result, new_remaining) = self.remaining.as_mut().map_or_else(
|| {
// The remaining buffer was cleared.
// Attempt to read everything from the poll and add to the remaining buffer
// if needed.
let data = Bytes::from(t.as_ref());
let amt = cmp::min(data.len(), buf.len() - read);
let (a, b) = data.split_at(amt);
buf[read..read + amt].copy_from_slice(a);

if b.is_empty() {
(Ok(amt + read), None)
} else {
(Ok(amt + read), Some(Bytes::from(b)))
}
},
|remaining| {
// There's still some data waiting to be written into the read buffer.
// Append to the remaining buffer
// Return the amount read from remaining
let mut r = BytesMut::with_capacity(remaining.len() + t.as_ref().len());
r.put_slice(remaining);
r.put_slice(t.as_ref());
(Ok(read), Some(r.freeze()))
},
);

self.remaining = new_remaining;
result
}
Expand Down
1 change: 1 addition & 0 deletions edgelet/edgelet-docker/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ impl ModuleRuntime for DockerModuleRuntime {
let image_with_tag = image_with_tag.clone();
move |lock| {
let digest_from_notary = lock.get(&image_with_tag);
#[allow(clippy::option_if_let_else)]
if let Some(digest_from_notary) = digest_from_notary {
future::Either::A(future::ok((digest_from_notary.clone(), lock)))
}
Expand Down
15 changes: 8 additions & 7 deletions edgelet/edgelet-hsm/tests/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ impl<'a> TestHSMEnvSetup<'a> {
pub fn new(m: &'a Mutex<()>, home_dir: Option<&str>) -> Self {
let guard = m.lock().unwrap();

let (temp_dir, path) = if let Some(d) = home_dir {
(None, PathBuf::from(d))
} else {
let td = TempDir::new().unwrap();
let p = td.path().to_path_buf();
(Some(td), p)
};
let (temp_dir, path) = home_dir.map_or_else(
|| {
let td = TempDir::new().unwrap();
let p = td.path().to_path_buf();
(Some(td), p)
},
|d| (None, PathBuf::from(d)),
);
env::set_var(HOMEDIR_KEY, path.as_os_str());
println!("IOTEDGE_HOMEDIR set to {:#?}", &path);
TestHSMEnvSetup {
Expand Down
5 changes: 1 addition & 4 deletions edgelet/edgelet-http/src/pid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ impl cmp::PartialEq for Pid {
fn eq(&self, other: &Pid) -> bool {
match *self {
Pid::None => false,
Pid::Any => match *other {
Pid::None => false,
_ => true,
},
Pid::Any => !matches!(*other, Pid::None),
Pid::Value(pid1) => match *other {
Pid::None => false,
Pid::Any => true,
Expand Down
10 changes: 2 additions & 8 deletions edgelet/edgelet-http/src/util/hyperwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,12 @@ impl Client {

#[cfg(test)]
pub fn is_null(&self) -> bool {
match *self {
Client::Null => true,
_ => false,
}
matches!(*self, Client::Null)
}

#[cfg(test)]
pub fn has_proxy(&self) -> bool {
match *self {
Client::Proxy(_) => true,
_ => false,
}
matches!(*self, Client::Proxy(_))
}
}

Expand Down
23 changes: 12 additions & 11 deletions edgelet/edgelet-iothub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,16 @@ where
fn update(&mut self, id: IdentitySpec) -> Self::UpdateFuture {
let module_id = id.module_id().to_string();

let result = if let Some(generation_id) = id.generation_id() {
match self.get_key_pair(&module_id, generation_id) {
let result = id.generation_id().map_or_else(
|| {
Either::B(future::err(Error::from(
ErrorKind::UpdateIdentityWithReason(
id.module_id().to_string(),
IdentityOperationReason::MissingGenerationId,
),
)))
},
|generation_id| match self.get_key_pair(&module_id, generation_id) {
Ok((primary_key, secondary_key)) => {
let auth = AuthMechanism::default()
.with_type(HubAuthType::Sas)
Expand All @@ -328,15 +336,8 @@ where
}

Err(err) => Either::B(future::err(err)),
}
} else {
Either::B(future::err(Error::from(
ErrorKind::UpdateIdentityWithReason(
module_id,
IdentityOperationReason::MissingGenerationId,
),
)))
};
},
);

Box::new(result)
}
Expand Down
134 changes: 75 additions & 59 deletions edgelet/edgelet-kube/src/module/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,41 +142,46 @@ where
)
.map_err(|err| Error::from(err.context(ErrorKind::KubeClient)))
.and_then(move |service_accounts| {
if let Some(current) =
service_accounts.items.into_iter().find(|service_account| {
service_accounts
.items
.into_iter()
.find(|service_account| {
service_account.metadata.as_ref().map_or(false, |meta| {
meta.name.as_ref().map_or(false, |n| *n == name)
})
})
{
if current == new_service_account {
Either::A(Either::A(future::ok(())))
} else {
let fut = client_copy
.lock()
.expect("Unexpected lock error")
.borrow_mut()
.replace_service_account(
&namespace_copy,
&name,
&new_service_account,
)
.map_err(|err| Error::from(err.context(ErrorKind::KubeClient)))
.map(|_| ());

Either::A(Either::B(fut))
}
} else {
let fut = client_copy
.lock()
.expect("Unexpected lock error")
.borrow_mut()
.create_service_account(&namespace_copy, &new_service_account)
.map_err(|err| Error::from(err.context(ErrorKind::KubeClient)))
.map(|_| ());

Either::B(fut)
}
.map_or_else(
|| {
let fut = client_copy
.lock()
.expect("Unexpected lock error")
.borrow_mut()
.create_service_account(&namespace_copy, &new_service_account)
.map_err(|err| Error::from(err.context(ErrorKind::KubeClient)))
.map(|_| ());
Either::B(fut)
},
|current| {
if current == new_service_account {
Either::A(Either::A(future::ok(())))
} else {
let fut = client_copy
.lock()
.expect("Unexpected lock error")
.borrow_mut()
.replace_service_account(
&namespace_copy,
&name,
&new_service_account,
)
.map_err(|err| {
Error::from(err.context(ErrorKind::KubeClient))
})
.map(|_| ());
Either::A(Either::B(fut))
}
},
)
})
})
.into_future()
Expand Down Expand Up @@ -256,35 +261,46 @@ where
)
.map_err(|err| Error::from(err.context(ErrorKind::KubeClient)))
.and_then(move |deployments| {
if let Some(current) = deployments.items.into_iter().find(|deployment| {
deployment.metadata.as_ref().map_or(false, |meta| {
meta.name.as_ref().map_or(false, |n| *n == name)
deployments
.items
.into_iter()
.find(|deployment| {
deployment.metadata.as_ref().map_or(false, |meta| {
meta.name.as_ref().map_or(false, |n| *n == name)
})
})
}) {
if current == new_deployment {
Either::A(Either::A(future::ok(())))
} else {
let fut = client_copy
.lock()
.expect("Unexpected lock error")
.borrow_mut()
.replace_deployment(namespace_copy.as_str(), &name, &new_deployment)
.map_err(|err| Error::from(err.context(ErrorKind::KubeClient)))
.map(|_| ());

Either::A(Either::B(fut))
}
} else {
let fut = client_copy
.lock()
.expect("Unexpected lock error")
.borrow_mut()
.create_deployment(namespace_copy.as_str(), &new_deployment)
.map_err(|err| Error::from(err.context(ErrorKind::KubeClient)))
.map(|_| ());

Either::B(fut)
}
.map_or_else(
|| {
let fut = client_copy
.lock()
.expect("Unexpected lock error")
.borrow_mut()
.create_deployment(namespace_copy.as_str(), &new_deployment)
.map_err(|err| Error::from(err.context(ErrorKind::KubeClient)))
.map(|_| ());
Either::B(fut)
},
|current| {
if current == new_deployment {
Either::A(Either::A(future::ok(())))
} else {
let fut = client_copy
.lock()
.expect("Unexpected lock error")
.borrow_mut()
.replace_deployment(
namespace_copy.as_str(),
&name,
&new_deployment,
)
.map_err(|err| {
Error::from(err.context(ErrorKind::KubeClient))
})
.map(|_| ());
Either::A(Either::B(fut))
}
},
)
})
})
.into_future()
Expand Down

0 comments on commit 7831fc5

Please sign in to comment.