protocol: use and_then, ok_or_else; option_map_or_none and or_fun_call
This commit is contained in:
parent
07fed441f3
commit
77394f0536
|
@ -95,14 +95,14 @@ pub struct Modifier {
|
|||
impl Modifier {
|
||||
pub fn from_value(v: &serde_json::Value) -> Self {
|
||||
let mut m = Modifier {
|
||||
bold: v.get("bold").map_or(Option::None, |v| v.as_bool()),
|
||||
italic: v.get("italic").map_or(Option::None, |v| v.as_bool()),
|
||||
underlined: v.get("underlined").map_or(Option::None, |v| v.as_bool()),
|
||||
strikethrough: v.get("strikethrough").map_or(Option::None, |v| v.as_bool()),
|
||||
obfuscated: v.get("obfuscated").map_or(Option::None, |v| v.as_bool()),
|
||||
bold: v.get("bold").and_then(|v| v.as_bool()),
|
||||
italic: v.get("italic").and_then(|v| v.as_bool()),
|
||||
underlined: v.get("underlined").and_then(|v| v.as_bool()),
|
||||
strikethrough: v.get("strikethrough").and_then(|v| v.as_bool()),
|
||||
obfuscated: v.get("obfuscated").and_then(|v| v.as_bool()),
|
||||
color: v
|
||||
.get("color")
|
||||
.map_or(Option::None, |v| v.as_str())
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|v| Color::from_string(&v.to_owned())),
|
||||
extra: Option::None,
|
||||
};
|
||||
|
|
|
@ -1208,8 +1208,8 @@ impl Conn {
|
|||
|
||||
let invalid_status = || Error::Err("Invalid status".to_owned());
|
||||
|
||||
let version = val.get("version").ok_or(invalid_status())?;
|
||||
let players = val.get("players").ok_or(invalid_status())?;
|
||||
let version = val.get("version").ok_or_else(invalid_status)?;
|
||||
let players = val.get("players").ok_or_else(invalid_status)?;
|
||||
|
||||
// For modded servers, get the list of Forge mods installed
|
||||
let mut forge_mods: std::vec::Vec<crate::protocol::forge::ForgeMod> = vec![];
|
||||
|
@ -1264,26 +1264,26 @@ impl Conn {
|
|||
name: version
|
||||
.get("name")
|
||||
.and_then(Value::as_str)
|
||||
.ok_or(invalid_status())?
|
||||
.ok_or_else(invalid_status)?
|
||||
.to_owned(),
|
||||
protocol: version
|
||||
.get("protocol")
|
||||
.and_then(Value::as_i64)
|
||||
.ok_or(invalid_status())? as i32,
|
||||
.ok_or_else(invalid_status)? as i32,
|
||||
},
|
||||
players: StatusPlayers {
|
||||
max: players
|
||||
.get("max")
|
||||
.and_then(Value::as_i64)
|
||||
.ok_or(invalid_status())? as i32,
|
||||
.ok_or_else(invalid_status)? as i32,
|
||||
online: players
|
||||
.get("online")
|
||||
.and_then(Value::as_i64)
|
||||
.ok_or(invalid_status())? as i32,
|
||||
.ok_or_else(invalid_status)? as i32,
|
||||
sample: Vec::new(), /* TODO */
|
||||
},
|
||||
description: format::Component::from_value(
|
||||
val.get("description").ok_or(invalid_status())?,
|
||||
val.get("description").ok_or_else(invalid_status)?,
|
||||
),
|
||||
favicon: val
|
||||
.get("favicon")
|
||||
|
|
Loading…
Reference in New Issue