Skip to content

Commit

Permalink
chore: more clippy
Browse files Browse the repository at this point in the history
Signed-off-by: spdfnet <32593931+spdfnet@users.noreply.github.com>
  • Loading branch information
spdfnet committed Sep 4, 2024
1 parent 0c26c41 commit 02d06d5
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 99 deletions.
22 changes: 5 additions & 17 deletions examples/ceph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,8 @@ fn main() {
// Mon command to check the health. Same as `ceph -s`
match cluster.ceph_mon_command("prefix", "status", None) {
Ok((outbuf, outs)) => {
match outbuf {
Some(output) => println!("Ceph mon command (outbuf):\n{}", output),
None => {}
}
match outs {
Some(output) => println!("Ceph mon command (outs):\n{}", output),
None => {}
}
if let Some(output) = outbuf { println!("Ceph mon command (outbuf):\n{}", output) }
if let Some(output) = outs { println!("Ceph mon command (outs):\n{}", output) }
}
Err(e) => {
println!("{:?}", e);
Expand Down Expand Up @@ -125,7 +119,7 @@ fn main() {
}

let fsid = cluster.rados_fsid().unwrap();
println!("rados_cluster_fsid: {}", fsid.to_hyphenated().to_string());
println!("rados_cluster_fsid: {}", fsid.to_hyphenated());

let ping_monitor = cluster.ping_monitor("ceph-mon.ceph-vm1"); // Change to support your mon name
println!("Ping monitor: {:?}", ping_monitor);
Expand All @@ -137,14 +131,8 @@ fn main() {
// Mon command to check the health. Same as `ceph -s`
match cluster.ceph_mon_command("prefix", "status", None) {
Ok((outbuf, outs)) => {
match outbuf {
Some(output) => println!("Ceph mon command (outbuf):\n{}", output),
None => {}
}
match outs {
Some(output) => println!("Ceph mon command (outs):\n{}", output),
None => {}
}
if let Some(output) = outbuf { println!("Ceph mon command (outbuf):\n{}", output) }
if let Some(output) = outs { println!("Ceph mon command (outs):\n{}", output) }
}
Err(e) => {
println!("{:?}", e);
Expand Down
8 changes: 4 additions & 4 deletions src/ceph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1839,7 +1839,7 @@ impl Rados {
pub fn ceph_version(socket: &str) -> Option<String> {
let cmd = "version";

admin_socket_command(&cmd, socket).ok().and_then(|json| {
admin_socket_command(cmd, socket).ok().and_then(|json| {
json_data(&json)
.and_then(|jsondata| json_find(jsondata, &[cmd]).map(|data| json_as_string(&data)))
})
Expand Down Expand Up @@ -2016,7 +2016,7 @@ impl Rados {
&mut cmds.as_ptr(),
1,
data.as_ptr() as *mut c_char,
data.len() as usize,
data.len(),
&mut outbuf,
&mut outbuf_len,
&mut outs,
Expand All @@ -2034,13 +2034,13 @@ impl Rados {

// Copy the data from outbuf and then call rados_buffer_free instead libc::free
if outbuf_len > 0 && !outbuf.is_null() {
let slice = ::std::slice::from_raw_parts(outbuf as *const u8, outbuf_len as usize);
let slice = ::std::slice::from_raw_parts(outbuf as *const u8, outbuf_len);
out = slice.to_vec();

rados_buffer_free(outbuf);
}
if outs_len > 0 && !outs.is_null() {
let slice = ::std::slice::from_raw_parts(outs as *const u8, outs_len as usize);
let slice = ::std::slice::from_raw_parts(outs as *const u8, outs_len);
status_string = Some(String::from_utf8(slice.to_vec())?);
rados_buffer_free(outs);
}
Expand Down
6 changes: 3 additions & 3 deletions src/ceph_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl CephClient {
user_id: T1,
config_file: T2,
) -> Result<CephClient, RadosError> {
let rados_t = match connect_to_ceph(&user_id.as_ref(), &config_file.as_ref()) {
let rados_t = match connect_to_ceph(user_id.as_ref(), config_file.as_ref()) {
Ok(rados_t) => rados_t,
Err(e) => return Err(e),
};
Expand Down Expand Up @@ -170,11 +170,11 @@ impl CephClient {
/// # }
/// ```
pub fn osd_unset(&self, key: OsdOption) -> Result<(), RadosError> {
cmd::osd_unset(&self.rados_t, &key, self.simulate).map_err(|a| a)
cmd::osd_unset(&self.rados_t, &key, self.simulate)
}

pub fn osd_tree(&self) -> Result<cmd::CrushTree, RadosError> {
cmd::osd_tree(&self.rados_t).map_err(|a| a)
cmd::osd_tree(&self.rados_t)
}

/// Get cluster status
Expand Down
7 changes: 2 additions & 5 deletions src/ceph_volume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,10 @@ pub fn ceph_volume_scan(
.output()?;
}
let json = String::from_utf8_lossy(&output.stdout);
let index: usize = match json.find("{") {
Some(i) => i,
None => 0,
};
let index: usize = json.find("{").unwrap_or_default();
// Skip stderr's. The last output is Json
let json = json.split_at(index);
match json_data(&json.1) {
match json_data(json.1) {
Some(jsondata) => Ok(jsondata),
_ => Err(RadosError::new("JSON data not found.".to_string())),
}
Expand Down
5 changes: 1 addition & 4 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1376,10 +1376,7 @@ pub fn osd_safe_to_destroy(cluster_handle: &Rados, osd_id: u64) -> bool {
"prefix": "osd safe-to-destroy",
"ids": [osd_id.to_string()]
});
match cluster_handle.ceph_mon_command_without_data(&cmd) {
Err(_) => false,
Ok(_) => true,
}
cluster_handle.ceph_mon_command_without_data(&cmd).is_ok()
}

/// count ceph-mgr daemons by metadata field property
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl fmt::Display for RadosError {
match *self {
RadosError::FromUtf8Error(ref e) => f.write_str(&e.to_string()),
RadosError::NulError(ref e) => f.write_str(&e.to_string()),
RadosError::Error(ref e) => f.write_str(&e),
RadosError::Error(ref e) => f.write_str(e),
RadosError::IoError(ref e) => f.write_str(&e.to_string()),
RadosError::ApiError(ref e) => e.fmt(f),
RadosError::IntoStringError(ref e) => f.write_str(&e.to_string()),
Expand Down
37 changes: 18 additions & 19 deletions src/mon_command.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
use std::collections::HashMap;

use serde_json;

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_builds_a_mon_command() {
let command = MonCommand::new()
.with_prefix("osd set")
.with("key", "osdout");

let actual: HashMap<String, String> = serde_json::from_str(&command.as_json()).unwrap();
let expected: HashMap<String, String> =
serde_json::from_str(r#"{"prefix":"osd set","format":"json","key":"osdout"}"#).unwrap();

assert_eq!(expected, actual);
}
}

pub struct MonCommand<'a> {
map: HashMap<&'a str, &'a str>,
Expand Down Expand Up @@ -62,3 +43,21 @@ impl<'a> MonCommand<'a> {
serde_json::to_string(&self.map).unwrap()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_builds_a_mon_command() {
let command = MonCommand::new()
.with_prefix("osd set")
.with("key", "osdout");

let actual: HashMap<String, String> = serde_json::from_str(&command.as_json()).unwrap();
let expected: HashMap<String, String> =
serde_json::from_str(r#"{"prefix":"osd set","format":"json","key":"osdout"}"#).unwrap();

assert_eq!(expected, actual);
}
}
Loading

0 comments on commit 02d06d5

Please sign in to comment.