protocol: use and_then, ok_or_else; option_map_or_none and or_fun_call

This commit is contained in:
ice_iix 2020-06-28 18:33:32 -07:00
parent 07fed441f3
commit 77394f0536
2 changed files with 13 additions and 13 deletions

View File

@ -95,14 +95,14 @@ pub struct Modifier {
impl Modifier { impl Modifier {
pub fn from_value(v: &serde_json::Value) -> Self { pub fn from_value(v: &serde_json::Value) -> Self {
let mut m = Modifier { let mut m = Modifier {
bold: v.get("bold").map_or(Option::None, |v| v.as_bool()), bold: v.get("bold").and_then(|v| v.as_bool()),
italic: v.get("italic").map_or(Option::None, |v| v.as_bool()), italic: v.get("italic").and_then(|v| v.as_bool()),
underlined: v.get("underlined").map_or(Option::None, |v| v.as_bool()), underlined: v.get("underlined").and_then(|v| v.as_bool()),
strikethrough: v.get("strikethrough").map_or(Option::None, |v| v.as_bool()), strikethrough: v.get("strikethrough").and_then(|v| v.as_bool()),
obfuscated: v.get("obfuscated").map_or(Option::None, |v| v.as_bool()), obfuscated: v.get("obfuscated").and_then(|v| v.as_bool()),
color: v color: v
.get("color") .get("color")
.map_or(Option::None, |v| v.as_str()) .and_then(|v| v.as_str())
.map(|v| Color::from_string(&v.to_owned())), .map(|v| Color::from_string(&v.to_owned())),
extra: Option::None, extra: Option::None,
}; };

View File

@ -1208,8 +1208,8 @@ impl Conn {
let invalid_status = || Error::Err("Invalid status".to_owned()); let invalid_status = || Error::Err("Invalid status".to_owned());
let version = val.get("version").ok_or(invalid_status())?; let version = val.get("version").ok_or_else(invalid_status)?;
let players = val.get("players").ok_or(invalid_status())?; let players = val.get("players").ok_or_else(invalid_status)?;
// For modded servers, get the list of Forge mods installed // For modded servers, get the list of Forge mods installed
let mut forge_mods: std::vec::Vec<crate::protocol::forge::ForgeMod> = vec![]; let mut forge_mods: std::vec::Vec<crate::protocol::forge::ForgeMod> = vec![];
@ -1264,26 +1264,26 @@ impl Conn {
name: version name: version
.get("name") .get("name")
.and_then(Value::as_str) .and_then(Value::as_str)
.ok_or(invalid_status())? .ok_or_else(invalid_status)?
.to_owned(), .to_owned(),
protocol: version protocol: version
.get("protocol") .get("protocol")
.and_then(Value::as_i64) .and_then(Value::as_i64)
.ok_or(invalid_status())? as i32, .ok_or_else(invalid_status)? as i32,
}, },
players: StatusPlayers { players: StatusPlayers {
max: players max: players
.get("max") .get("max")
.and_then(Value::as_i64) .and_then(Value::as_i64)
.ok_or(invalid_status())? as i32, .ok_or_else(invalid_status)? as i32,
online: players online: players
.get("online") .get("online")
.and_then(Value::as_i64) .and_then(Value::as_i64)
.ok_or(invalid_status())? as i32, .ok_or_else(invalid_status)? as i32,
sample: Vec::new(), /* TODO */ sample: Vec::new(), /* TODO */
}, },
description: format::Component::from_value( description: format::Component::from_value(
val.get("description").ok_or(invalid_status())?, val.get("description").ok_or_else(invalid_status)?,
), ),
favicon: val favicon: val
.get("favicon") .get("favicon")