[fractal] Use .unwrap_or_default() instead of .unwrap_or() where possible
- From: Daniel Garcia Moreno <danigm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal] Use .unwrap_or_default() instead of .unwrap_or() where possible
- Date: Sun, 6 Jan 2019 17:49:56 +0000 (UTC)
commit ff7744efc07d91ffee3eecbe7f2598f9a1d9ec0b
Author: Alejandro DomÃnguez <adomu net-c com>
Date: Sun Jan 6 17:48:37 2019 +0100
Use .unwrap_or_default() instead of .unwrap_or() where possible
fractal-gtk/src/appop/member.rs | 4 ++--
fractal-gtk/src/appop/message.rs | 4 ++--
fractal-gtk/src/appop/room.rs | 6 +++---
fractal-gtk/src/i18n.rs | 2 +-
fractal-gtk/src/util.rs | 2 +-
fractal-gtk/src/widgets/avatar.rs | 4 ++--
fractal-gtk/src/widgets/inline_player.rs | 4 ++--
fractal-gtk/src/widgets/roomlist.rs | 2 +-
fractal-gtk/src/widgets/scroll_widget.rs | 4 ++--
fractal-matrix-api/src/backend/directory.rs | 22 ++++++++++++----------
fractal-matrix-api/src/backend/register.rs | 18 +++++++++---------
fractal-matrix-api/src/backend/room.rs | 16 +++++++++-------
fractal-matrix-api/src/backend/sync.rs | 9 +++++----
fractal-matrix-api/src/backend/user.rs | 10 +++++-----
fractal-matrix-api/src/model/message.rs | 22 +++++++++++-----------
fractal-matrix-api/src/util.rs | 20 ++++++++++----------
16 files changed, 77 insertions(+), 72 deletions(-)
---
diff --git a/fractal-gtk/src/appop/member.rs b/fractal-gtk/src/appop/member.rs
index f96a5af0..55a0759f 100644
--- a/fractal-gtk/src/appop/member.rs
+++ b/fractal-gtk/src/appop/member.rs
@@ -63,10 +63,10 @@ impl AppOp {
Some("join") => {
let m = Member {
avatar: Some(String::from(
- ev.content["avatar_url"].as_str().unwrap_or(""),
+ ev.content["avatar_url"].as_str().unwrap_or_default(),
)),
alias: Some(String::from(
- ev.content["displayname"].as_str().unwrap_or(""),
+ ev.content["displayname"].as_str().unwrap_or_default(),
)),
uid: sender.clone(),
};
diff --git a/fractal-gtk/src/appop/message.rs b/fractal-gtk/src/appop/message.rs
index 46008223..80083c38 100644
--- a/fractal-gtk/src/appop/message.rs
+++ b/fractal-gtk/src/appop/message.rs
@@ -321,7 +321,7 @@ impl AppOp {
let result = file_chooser.run();
if gtk::ResponseType::from(result) == gtk::ResponseType::Accept {
if let Some(fname) = file_chooser.get_filename() {
- let f = String::from(fname.to_str().unwrap_or(""));
+ let f = String::from(fname.to_str().unwrap_or_default());
APPOP!(attach_message, (f));
}
}
@@ -494,7 +494,7 @@ fn create_ui_message(
) -> MessageContent {
MessageContent {
msg: msg.clone(),
- id: msg.id.unwrap_or(String::new()),
+ id: msg.id.unwrap_or_default(),
sender: msg.sender,
sender_name: name,
mtype: t,
diff --git a/fractal-gtk/src/appop/room.rs b/fractal-gtk/src/appop/room.rs
index 8ef1348b..d57fde7d 100644
--- a/fractal-gtk/src/appop/room.rs
+++ b/fractal-gtk/src/appop/room.rs
@@ -226,7 +226,7 @@ impl AppOp {
.get_object::<gtk::ToggleButton>("private_visibility_button")
.expect("Can't find private_visibility_button in ui file.");
- let n = name.get_text().unwrap_or(String::new());
+ let n = name.get_text().unwrap_or_default();
// Since the switcher
let p = if private.get_active() {
@@ -286,7 +286,7 @@ impl AppOp {
.unsent_messages
.get(&active_room_id)
.cloned()
- .unwrap_or((String::new(), 0));
+ .unwrap_or_default();
if let Some(buffer) = self.ui.sventry.view.get_buffer() {
buffer.set_text(&msg.0);
@@ -401,7 +401,7 @@ impl AppOp {
.get_object::<gtk::Entry>("join_room_name")
.expect("Can't find join_room_name in ui file.");
- let n = name.get_text().unwrap_or(String::new()).trim().to_string();
+ let n = name.get_text().unwrap_or_default().trim().to_string();
self.backend.send(BKCommand::JoinRoom(n.clone())).unwrap();
}
diff --git a/fractal-gtk/src/i18n.rs b/fractal-gtk/src/i18n.rs
index 8a9ce52a..d4b9aff2 100644
--- a/fractal-gtk/src/i18n.rs
+++ b/fractal-gtk/src/i18n.rs
@@ -8,7 +8,7 @@ use regex::Regex;
#[allow(dead_code)]
fn freplace(input: String, args: &[&str]) -> String {
let mut parts = input.split("{}");
- let mut output = parts.next().unwrap_or("").to_string();
+ let mut output = parts.next().unwrap_or_default().to_string();
for (p, a) in parts.zip(args.iter()) {
output += &(a.to_string() + &p.to_string());
}
diff --git a/fractal-gtk/src/util.rs b/fractal-gtk/src/util.rs
index 3ca928f7..231b748f 100644
--- a/fractal-gtk/src/util.rs
+++ b/fractal-gtk/src/util.rs
@@ -63,7 +63,7 @@ pub fn get_markdown_schema() -> bool {
let settings: Settings = Settings::new("org.gnome.Fractal");
Some(settings.get_boolean("markdown-active"))
})
- .unwrap_or(false)
+ .unwrap_or_default()
}
pub fn set_markdown_schema(md: bool) {
diff --git a/fractal-gtk/src/widgets/avatar.rs b/fractal-gtk/src/widgets/avatar.rs
index bed70abf..39350102 100644
--- a/fractal-gtk/src/widgets/avatar.rs
+++ b/fractal-gtk/src/widgets/avatar.rs
@@ -32,7 +32,7 @@ impl AvatarData {
}
pub fn redraw_pixbuf(&mut self) {
- let path = cache_path(&self.uid).unwrap_or(String::new());
+ let path = cache_path(&self.uid).unwrap_or_default();
self.cache = load_pixbuf(&path, self.size);
self.widget.queue_draw();
}
@@ -77,7 +77,7 @@ impl AvatarExt for gtk::Box {
fn circle(&self, uid: String, username: Option<String>, size: i32) -> Rc<RefCell<AvatarData>> {
self.clean();
let da = self.create_da(Some(size));
- let path = cache_path(&uid).unwrap_or(String::new());
+ let path = cache_path(&uid).unwrap_or_default();
let user_avatar = load_pixbuf(&path, size);
let uname = username.clone();
/* remove IRC postfix from the username */
diff --git a/fractal-gtk/src/widgets/inline_player.rs b/fractal-gtk/src/widgets/inline_player.rs
index ab1e7491..3cb4a854 100644
--- a/fractal-gtk/src/widgets/inline_player.rs
+++ b/fractal-gtk/src/widgets/inline_player.rs
@@ -75,7 +75,7 @@ impl Deref for Position {
impl PlayerTimes {
/// Update the duration `gtk::Label` and the max range of the `gtk::SclaeBar`.
fn on_duration_changed(&self, duration: Duration) {
- let seconds = duration.seconds().map(|v| v as f64).unwrap_or(0.0);
+ let seconds = duration.seconds().map(|v| v as f64).unwrap_or_default();
self.slider.block_signal(&self.slider_update);
self.slider.set_range(0.0, seconds);
@@ -86,7 +86,7 @@ impl PlayerTimes {
/// Update the `gtk::SclaeBar` when the pipeline position is changed.
fn on_position_updated(&self, position: Position) {
- let seconds = position.seconds().map(|v| v as f64).unwrap_or(0.0);
+ let seconds = position.seconds().map(|v| v as f64).unwrap_or_default();
self.slider.block_signal(&self.slider_update);
self.slider.set_value(seconds);
diff --git a/fractal-gtk/src/widgets/roomlist.rs b/fractal-gtk/src/widgets/roomlist.rs
index ef93fdb6..d5c69141 100644
--- a/fractal-gtk/src/widgets/roomlist.rs
+++ b/fractal-gtk/src/widgets/roomlist.rs
@@ -360,7 +360,7 @@ impl RoomListGroup {
if let Some(row) = self.list.get_row_at_index(i as i32) {
match term {
&Some(ref t) if !t.is_empty() => {
- let rname = r.room.name.clone().unwrap_or(String::new()).to_lowercase();
+ let rname = r.room.name.clone().unwrap_or_default().to_lowercase();
if rname.contains(&t.to_lowercase()) {
row.show();
} else {
diff --git a/fractal-gtk/src/widgets/scroll_widget.rs b/fractal-gtk/src/widgets/scroll_widget.rs
index 60f675df..8fac41fe 100644
--- a/fractal-gtk/src/widgets/scroll_widget.rs
+++ b/fractal-gtk/src/widgets/scroll_widget.rs
@@ -110,12 +110,12 @@ impl ScrollWidget {
.view
.get_vadjustment()
.and_then(|adj| Some(adj.get_upper()))
- .unwrap_or(0.0);
+ .unwrap_or_default();
let value = widgets
.view
.get_vadjustment()
.and_then(|adj| Some(adj.get_value()))
- .unwrap_or(0.0);
+ .unwrap_or_default();
let mut scroll = ScrollWidget {
widgets,
diff --git a/fractal-matrix-api/src/backend/directory.rs b/fractal-matrix-api/src/backend/directory.rs
index bf2107c2..5872eeb6 100644
--- a/fractal-matrix-api/src/backend/directory.rs
+++ b/fractal-matrix-api/src/backend/directory.rs
@@ -33,7 +33,7 @@ pub fn protocols(bk: &Backend) -> Result<(), Error> {
protocols.push(Protocol {
id: String::new(),
- desc: String::from(s.split('/').last().unwrap_or("")),
+ desc: String::from(s.split('/').last().unwrap_or_default()),
});
if let Some(prs) = r.as_object() {
@@ -101,21 +101,23 @@ pub fn room_search(
&url,
&attrs,
move |r: JsonValue| {
- let next_branch = r["next_batch"].as_str().unwrap_or("");
+ let next_branch = r["next_batch"].as_str().unwrap_or_default();
data.lock().unwrap().rooms_since = String::from(next_branch);
let mut rooms: Vec<Room> = vec![];
for room in r["chunk"].as_array().unwrap() {
- let alias = String::from(room["canonical_alias"].as_str().unwrap_or(""));
- let id = String::from(room["room_id"].as_str().unwrap_or(""));
- let name = String::from(room["name"].as_str().unwrap_or(""));
+ let alias = String::from(room["canonical_alias"].as_str().unwrap_or_default());
+ let id = String::from(room["room_id"].as_str().unwrap_or_default());
+ let name = String::from(room["name"].as_str().unwrap_or_default());
let mut r = Room::new(id.clone(), Some(name));
r.alias = Some(alias);
- r.avatar = Some(String::from(room["avatar_url"].as_str().unwrap_or("")));
- r.topic = Some(String::from(room["topic"].as_str().unwrap_or("")));
- r.n_members = room["num_joined_members"].as_i64().unwrap_or(0) as i32;
- r.world_readable = room["world_readable"].as_bool().unwrap_or(false);
- r.guest_can_join = room["guest_can_join"].as_bool().unwrap_or(false);
+ r.avatar = Some(String::from(
+ room["avatar_url"].as_str().unwrap_or_default(),
+ ));
+ r.topic = Some(String::from(room["topic"].as_str().unwrap_or_default()));
+ r.n_members = room["num_joined_members"].as_i64().unwrap_or_default() as i32;
+ r.world_readable = room["world_readable"].as_bool().unwrap_or_default();
+ r.guest_can_join = room["guest_can_join"].as_bool().unwrap_or_default();
/* download the avatar */
if let Some(avatar) = r.avatar.clone() {
if let Ok(dest) = cache_path(&id) {
diff --git a/fractal-matrix-api/src/backend/register.rs b/fractal-matrix-api/src/backend/register.rs
index fb769628..149d5823 100644
--- a/fractal-matrix-api/src/backend/register.rs
+++ b/fractal-matrix-api/src/backend/register.rs
@@ -24,9 +24,9 @@ pub fn guest(bk: &Backend, server: &str) -> Result<(), Error> {
&url,
&attrs,
|r: JsonValue| {
- let uid = String::from(r["user_id"].as_str().unwrap_or(""));
- let tk = String::from(r["access_token"].as_str().unwrap_or(""));
- let dev = String::from(r["device_id"].as_str().unwrap_or(""));
+ let uid = String::from(r["user_id"].as_str().unwrap_or_default());
+ let tk = String::from(r["access_token"].as_str().unwrap_or_default());
+ let dev = String::from(r["device_id"].as_str().unwrap_or_default());
data.lock().unwrap().user_id = uid.clone();
data.lock().unwrap().access_token = tk.clone();
data.lock().unwrap().since = None;
@@ -79,9 +79,9 @@ pub fn login(bk: &Backend, user: &str, password: &str, server: &str) -> Result<(
&url,
&attrs,
|r: JsonValue| {
- let uid = String::from(r["user_id"].as_str().unwrap_or(""));
- let tk = String::from(r["access_token"].as_str().unwrap_or(""));
- let dev = String::from(r["device_id"].as_str().unwrap_or(""));
+ let uid = String::from(r["user_id"].as_str().unwrap_or_default());
+ let tk = String::from(r["access_token"].as_str().unwrap_or_default());
+ let dev = String::from(r["device_id"].as_str().unwrap_or_default());
if uid.is_empty() || tk.is_empty() {
tx.send(BKResponse::LoginError(Error::BackendError))
@@ -148,9 +148,9 @@ pub fn register(bk: &Backend, user: &str, password: &str, server: &str) -> Resul
&url,
&attrs,
|r: JsonValue| {
- let uid = String::from(r["user_id"].as_str().unwrap_or(""));
- let tk = String::from(r["access_token"].as_str().unwrap_or(""));
- let dev = String::from(r["device_id"].as_str().unwrap_or(""));
+ let uid = String::from(r["user_id"].as_str().unwrap_or_default());
+ let tk = String::from(r["access_token"].as_str().unwrap_or_default());
+ let dev = String::from(r["device_id"].as_str().unwrap_or_default());
data.lock().unwrap().user_id = uid.clone();
data.lock().unwrap().access_token = tk.clone();
diff --git a/fractal-matrix-api/src/backend/room.rs b/fractal-matrix-api/src/backend/room.rs
index 7099fca3..184d3667 100644
--- a/fractal-matrix-api/src/backend/room.rs
+++ b/fractal-matrix-api/src/backend/room.rs
@@ -47,7 +47,7 @@ pub fn get_room_detail(bk: &Backend, roomid: String, key: String) -> Result<(),
|r: JsonValue| {
let k = keys.split('.').last().unwrap();
- let value = String::from(r[&k].as_str().unwrap_or(""));
+ let value = String::from(r[&k].as_str().unwrap_or_default());
tx.send(BKResponse::RoomDetail(roomid, key, value)).unwrap();
},
|err| tx.send(BKResponse::RoomDetailError(err)).unwrap()
@@ -79,7 +79,9 @@ pub fn get_room_avatar(bk: &Backend, roomid: String) -> Result<(), Error> {
tx.send(BKResponse::RoomAvatar(roomid, avatar)).unwrap();
},
|err: Error| match err {
- Error::MatrixError(ref js) if js["errcode"].as_str().unwrap_or("") == "M_NOT_FOUND" => {
+ Error::MatrixError(ref js)
+ if js["errcode"].as_str().unwrap_or_default() == "M_NOT_FOUND" =>
+ {
let avatar =
util::get_room_avatar(&baseu, &tk, &userid, &roomid).unwrap_or_default();
tx.send(BKResponse::RoomAvatar(roomid, avatar)).unwrap();
@@ -193,7 +195,7 @@ fn parse_context(
let array = r["events_before"].as_array();
for msg in array.unwrap().iter().rev() {
if id.is_none() {
- id = Some(msg["event_id"].as_str().unwrap_or("").to_string());
+ id = Some(msg["event_id"].as_str().unwrap_or_default().to_string());
}
if !Message::supported_event(&&msg) {
@@ -442,7 +444,7 @@ pub fn set_room_avatar(bk: &Backend, roomid: &str, avatar: &str) -> Result<(), E
tx.send(BKResponse::SetRoomAvatarError(err)).unwrap();
}
Ok(js) => {
- let uri = js["content_uri"].as_str().unwrap_or("");
+ let uri = js["content_uri"].as_str().unwrap_or_default();
let attrs = json!({ "url": uri });
match json_q("put", &roomurl, &attrs, 0) {
Ok(_) => {
@@ -484,7 +486,7 @@ pub fn attach_file(bk: &Backend, msg: Message) -> Result<(), Error> {
tx.send(BKResponse::AttachFileError(err)).unwrap();
}
Ok(js) => {
- let uri = js["content_uri"].as_str().unwrap_or("");
+ let uri = js["content_uri"].as_str().unwrap_or_default();
m.url = Some(uri.to_string());
if let Some(t) = itx {
t.send(BKCommand::SendMsg(m.clone())).unwrap();
@@ -525,7 +527,7 @@ pub fn new_room(
&url,
&attrs,
move |r: JsonValue| {
- let id = String::from(r["room_id"].as_str().unwrap_or(""));
+ let id = String::from(r["room_id"].as_str().unwrap_or_default());
let name = n;
let r = Room::new(id, Some(name));
tx.send(BKResponse::NewRoom(r, internal_id)).unwrap();
@@ -557,7 +559,7 @@ pub fn direct_chat(bk: &Backend, user: &Member, internal_id: String) -> Result<(
&url,
&attrs,
move |r: JsonValue| {
- let id = String::from(r["room_id"].as_str().unwrap_or(""));
+ let id = String::from(r["room_id"].as_str().unwrap_or_default());
let mut r = Room::new(id.clone(), m.alias.clone());
r.direct = true;
tx.send(BKResponse::NewRoom(r, internal_id)).unwrap();
diff --git a/fractal-matrix-api/src/backend/sync.rs b/fractal-matrix-api/src/backend/sync.rs
index 35f44c14..7eda7150 100644
--- a/fractal-matrix-api/src/backend/sync.rs
+++ b/fractal-matrix-api/src/backend/sync.rs
@@ -68,7 +68,7 @@ pub fn sync(bk: &Backend, new_since: Option<String>, initial: bool) -> Result<()
thread::spawn(move || {
match json_q("get", &url, &attrs, timeout) {
Ok(r) => {
- let next_batch = String::from(r["next_batch"].as_str().unwrap_or(""));
+ let next_batch = String::from(r["next_batch"].as_str().unwrap_or_default());
if let Some(since) = since {
// New rooms
match get_rooms_from_json(&r, &userid, &baseu) {
@@ -98,14 +98,15 @@ pub fn sync(bk: &Backend, new_since: Option<String>, initial: bool) -> Result<()
for ev in events {
match ev.stype.as_ref() {
"m.room.name" => {
- let name =
- String::from(ev.content["name"].as_str().unwrap_or(""));
+ let name = String::from(
+ ev.content["name"].as_str().unwrap_or_default(),
+ );
tx.send(BKResponse::RoomName(ev.room.clone(), name))
.unwrap();
}
"m.room.topic" => {
let t = String::from(
- ev.content["topic"].as_str().unwrap_or(""),
+ ev.content["topic"].as_str().unwrap_or_default(),
);
tx.send(BKResponse::RoomTopic(ev.room.clone(), t)).unwrap();
}
diff --git a/fractal-matrix-api/src/backend/user.rs b/fractal-matrix-api/src/backend/user.rs
index 84d959a1..6905dba6 100644
--- a/fractal-matrix-api/src/backend/user.rs
+++ b/fractal-matrix-api/src/backend/user.rs
@@ -127,13 +127,13 @@ pub fn get_email_token(
&url,
&attrs,
|r: JsonValue| {
- let sid = String::from(r["sid"].as_str().unwrap_or(""));
+ let sid = String::from(r["sid"].as_str().unwrap_or_default());
tx.send(BKResponse::GetTokenEmail(sid, client_secret))
.unwrap();
},
|err| match err {
Error::MatrixError(ref js)
- if js["errcode"].as_str().unwrap_or("") == "M_THREEPID_IN_USE" =>
+ if js["errcode"].as_str().unwrap_or_default() == "M_THREEPID_IN_USE" =>
{
tx.send(BKResponse::GetTokenEmailUsed).unwrap();
}
@@ -167,13 +167,13 @@ pub fn get_phone_token(
&url,
&attrs,
|r: JsonValue| {
- let sid = String::from(r["sid"].as_str().unwrap_or(""));
+ let sid = String::from(r["sid"].as_str().unwrap_or_default());
tx.send(BKResponse::GetTokenPhone(sid, client_secret))
.unwrap();
},
|err| match err {
Error::MatrixError(ref js)
- if js["errcode"].as_str().unwrap_or("") == "M_THREEPID_IN_USE" =>
+ if js["errcode"].as_str().unwrap_or_default() == "M_THREEPID_IN_USE" =>
{
tx.send(BKResponse::GetTokenPhoneUsed).unwrap();
}
@@ -476,7 +476,7 @@ pub fn set_user_avatar(bk: &Backend, avatar: String) -> Result<(), Error> {
tx.send(BKResponse::SetUserAvatarError(err)).unwrap();
}
Ok(js) => {
- let uri = js["content_uri"].as_str().unwrap_or("");
+ let uri = js["content_uri"].as_str().unwrap_or_default();
let attrs = json!({ "avatar_url": uri });
match json_q("put", &url, &attrs, 0) {
Ok(_) => {
diff --git a/fractal-matrix-api/src/model/message.rs b/fractal-matrix-api/src/model/message.rs
index 280cb4b2..04e83588 100644
--- a/fractal-matrix-api/src/model/message.rs
+++ b/fractal-matrix-api/src/model/message.rs
@@ -110,13 +110,13 @@ impl Message {
/// * `roomid` - The message room id
/// * `msg` - The message event as Json
pub fn parse_room_message(roomid: &str, msg: &JsonValue) -> Message {
- let sender = msg["sender"].as_str().unwrap_or("");
+ let sender = msg["sender"].as_str().unwrap_or_default();
- let timestamp = msg["origin_server_ts"].as_i64().unwrap_or(0) / 1000;
+ let timestamp = msg["origin_server_ts"].as_i64().unwrap_or_default() / 1000;
let server_timestamp: DateTime<Local> = Local.timestamp(timestamp, 0);
- let id = msg["event_id"].as_str().unwrap_or("");
- let type_ = msg["type"].as_str().unwrap_or("");
+ let id = msg["event_id"].as_str().unwrap_or_default();
+ let type_ = msg["type"].as_str().unwrap_or_default();
let redacted = msg["unsigned"].get("redacted_because") != None;
@@ -149,15 +149,15 @@ impl Message {
}
fn parse_m_room_message(msg: &mut Message, c: &JsonValue) {
- let mtype = c["msgtype"].as_str().unwrap_or("");
- let body = c["body"].as_str().unwrap_or("");
+ let mtype = c["msgtype"].as_str().unwrap_or_default();
+ let body = c["body"].as_str().unwrap_or_default();
let formatted_body = c["formatted_body"].as_str().map(String::from);
let format = c["format"].as_str().map(String::from);
match mtype {
"m.image" | "m.file" | "m.video" | "m.audio" => {
- let url = String::from(c["url"].as_str().unwrap_or(""));
- let mut t = String::from(c["info"]["thumbnail_url"].as_str().unwrap_or(""));
+ let url = String::from(c["url"].as_str().unwrap_or_default());
+ let mut t = String::from(c["info"]["thumbnail_url"].as_str().unwrap_or_default());
if t.is_empty() && !url.is_empty() {
t = url.clone();
}
@@ -182,10 +182,10 @@ impl Message {
}
fn parse_m_sticker(msg: &mut Message, c: &JsonValue) {
- let body = c["body"].as_str().unwrap_or("");
+ let body = c["body"].as_str().unwrap_or_default();
- let url = String::from(c["url"].as_str().unwrap_or(""));
- let mut t = String::from(c["info"]["thumbnail_url"].as_str().unwrap_or(""));
+ let url = String::from(c["url"].as_str().unwrap_or_default());
+ let mut t = String::from(c["info"]["thumbnail_url"].as_str().unwrap_or_default());
if t.is_empty() && !url.is_empty() {
t = url.clone();
}
diff --git a/fractal-matrix-api/src/util.rs b/fractal-matrix-api/src/util.rs
index c91789e6..cc31be75 100644
--- a/fractal-matrix-api/src/util.rs
+++ b/fractal-matrix-api/src/util.rs
@@ -210,10 +210,10 @@ pub fn get_rooms_from_json(r: &JsonValue, userid: &str, baseu: &Url) -> Result<V
r.direct = direct.contains(k);
r.notifications = room["unread_notifications"]["notification_count"]
.as_i64()
- .unwrap_or(0) as i32;
+ .unwrap_or_default() as i32;
r.highlight = room["unread_notifications"]["highlight_count"]
.as_i64()
- .unwrap_or(0) as i32;
+ .unwrap_or_default() as i32;
r.prev_batch = timeline["prev_batch"].as_str().map(String::from);
@@ -376,10 +376,10 @@ pub fn get_rooms_notifies_from_json(r: &JsonValue) -> Result<Vec<(String, i32, i
let room = join.get(k).ok_or(Error::BackendError)?;
let n = room["unread_notifications"]["notification_count"]
.as_i64()
- .unwrap_or(0) as i32;
+ .unwrap_or_default() as i32;
let h = room["unread_notifications"]["highlight_count"]
.as_i64()
- .unwrap_or(0) as i32;
+ .unwrap_or_default() as i32;
out.push((k.clone(), n, h));
}
@@ -408,10 +408,10 @@ pub fn parse_sync_events(r: &JsonValue) -> Result<Vec<Event>, Error> {
//info!("ev: {:#?}", ev);
evs.push(Event {
room: k.clone(),
- sender: String::from(ev["sender"].as_str().unwrap_or("")),
+ sender: String::from(ev["sender"].as_str().unwrap_or_default()),
content: ev["content"].clone(),
- stype: String::from(ev["type"].as_str().unwrap_or("")),
- id: String::from(ev["id"].as_str().unwrap_or("")),
+ stype: String::from(ev["type"].as_str().unwrap_or_default()),
+ id: String::from(ev["id"].as_str().unwrap_or_default()),
});
}
}
@@ -702,7 +702,7 @@ pub fn get_room_avatar(base: &Url, tk: &str, userid: &str, roomid: &str) -> Resu
let mut members2 = events.iter().filter(&filter);
let m1 = match members2.next() {
- Some(m) => m["content"]["avatar_url"].as_str().unwrap_or(""),
+ Some(m) => m["content"]["avatar_url"].as_str().unwrap_or_default(),
None => "",
};
@@ -810,7 +810,7 @@ pub fn fill_room_gap(
let url = client_url(baseu, &path, ¶ms)?;
let r = json_q("get", &url, &json!(null), globals::TIMEOUT)?;
- nend = String::from(r["end"].as_str().unwrap_or(""));
+ nend = String::from(r["end"].as_str().unwrap_or_default());
let array = r["chunk"].as_array();
if array.is_none() || array.unwrap().is_empty() {
@@ -904,7 +904,7 @@ pub fn get_user_avatar_img(baseu: &Url, userid: &str, avatar: &str) -> Result<St
}
pub fn parse_room_member(msg: &JsonValue) -> Option<Member> {
- let sender = msg["sender"].as_str().unwrap_or("");
+ let sender = msg["sender"].as_str().unwrap_or_default();
let c = &msg["content"];
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]