Ran cargo fix and fmt.

This commit is contained in:
Werner Kroneman 2023-12-10 17:41:58 +01:00
parent 5a33bf2280
commit 74e2cfd6b7
5 changed files with 53 additions and 45 deletions

View File

@ -14,13 +14,11 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::widgets::login_screen::LoginAttempt; use crate::passwords::Password;
use dioxus::prelude::*;
use jid::BareJid; use jid::BareJid;
use keyring::Entry; use keyring::Entry;
use log::error; use log::error;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use crate::passwords::Password;
/// The configuration struct containing all the configuration options. /// The configuration struct containing all the configuration options.
#[derive(Default, Debug, Serialize, Deserialize)] #[derive(Default, Debug, Serialize, Deserialize)]
@ -38,25 +36,26 @@ pub struct Configuration {
/// # Arguments /// # Arguments
/// * `attempt` - The login attempt to store. /// * `attempt` - The login attempt to store.
/// * `config` - The current app configuration; UseState should prevent race conditions in writing. /// * `config` - The current app configuration; UseState should prevent race conditions in writing.
pub fn store_login_details(jid: BareJid, pub fn store_login_details(
default_nick: String, jid: BareJid,
password: &Password, default_nick: String,
c: &mut Configuration) { password: &Password,
c: &mut Configuration,
) {
// Open the config state and store the username and default nick. // Open the config state and store the username and default nick.
// Open the keyring and store the password. // Open the keyring and store the password.
let entry = Entry::new("dergchat", &jid.to_string()).expect("Failed to create keyring entry."); let entry = Entry::new("dergchat", &jid.to_string()).expect("Failed to create keyring entry.");
entry entry
.set_password(&password.0) .set_password(&password.0)
.expect("Failed to set password in keyring."); .expect("Failed to set password in keyring.");
// Store the username and default nick in the config. // Store the username and default nick in the config.
c.stored_username = Some(jid); c.stored_username = Some(jid);
c.stored_default_nick = Some(default_nick); c.stored_default_nick = Some(default_nick);
if let Err(e) = confy::store("dergchat", None, c) { if let Err(e) = confy::store("dergchat", None, c) {
error!("Failed to store the config file: {}", e); error!("Failed to store the config file: {}", e);
} }
} }
/// Load the configuration from the config file. /// Load the configuration from the config file.

View File

@ -17,10 +17,10 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
mod configuration; mod configuration;
mod passwords;
mod types; mod types;
mod widgets; mod widgets;
mod xmpp_interface; mod xmpp_interface;
mod passwords;
use dioxus::prelude::*; use dioxus::prelude::*;
use dioxus_desktop::Config; use dioxus_desktop::Config;

View File

@ -14,9 +14,9 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
use std::fmt::Debug;
use jid::BareJid; use jid::BareJid;
use keyring::Entry; use keyring::Entry;
use std::fmt::Debug;
pub struct Password(pub String); pub struct Password(pub String);
@ -30,11 +30,18 @@ impl Debug for Password {
/// Retrieve the password for the given username from the keyring, if it exists. /// Retrieve the password for the given username from the keyring, if it exists.
pub fn try_retrieve_password_from_keyring(username: &BareJid) -> Option<Password> { pub fn try_retrieve_password_from_keyring(username: &BareJid) -> Option<Password> {
Entry::new("dergchat", &username.to_string()).expect("Failed to create keyring entry.").get_password().ok().map(|p| Password(p)) Entry::new("dergchat", &username.to_string())
.expect("Failed to create keyring entry.")
.get_password()
.ok()
.map(|p| Password(p))
} }
/// Store the password for the given username in the keyring. /// Store the password for the given username in the keyring.
pub fn store_keyring_password(username: &BareJid, password: &Password) { pub fn store_keyring_password(username: &BareJid, password: &Password) {
let entry = Entry::new("dergchat", &username.to_string()).expect("Failed to create keyring entry."); let entry =
entry.set_password(&*password.0).expect("Failed to set password in keyring."); Entry::new("dergchat", &username.to_string()).expect("Failed to create keyring entry.");
} entry
.set_password(&*password.0)
.expect("Failed to set password in keyring.");
}

View File

@ -14,9 +14,9 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::passwords::Password;
use jid::BareJid; use jid::BareJid;
use std::fmt::Debug; use std::fmt::Debug;
use crate::passwords::Password;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct Room { pub struct Room {
@ -34,4 +34,4 @@ pub struct LoginCredentials {
pub username: BareJid, pub username: BareJid,
pub default_nick: String, pub default_nick: String,
pub password: Password, pub password: Password,
} }

View File

@ -14,6 +14,7 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::passwords::Password;
use crate::types::LoginCredentials; use crate::types::LoginCredentials;
use crate::widgets::login_screen::LoginAttempt; use crate::widgets::login_screen::LoginAttempt;
use crate::widgets::login_screen::{LoginScreen, LoginStatus}; use crate::widgets::login_screen::{LoginScreen, LoginStatus};
@ -22,7 +23,6 @@ use crate::widgets::room_view::RoomView;
use crate::widgets::sidebar::SideBar; use crate::widgets::sidebar::SideBar;
use crate::xmpp_interface::NetworkCommand; use crate::xmpp_interface::NetworkCommand;
use dioxus::core::{Element, Scope}; use dioxus::core::{Element, Scope};
use crate::passwords::Password;
use dioxus::prelude::*; use dioxus::prelude::*;
use futures_util::StreamExt; use futures_util::StreamExt;
@ -30,13 +30,13 @@ use jid::BareJid;
use log::{error, info}; use log::{error, info};
use std::collections::HashMap; use std::collections::HashMap;
use crate::configuration::{Configuration, load_config}; use crate::configuration::{load_config, Configuration};
use crate::xmpp_interface; use crate::xmpp_interface;
use keyring::Entry;
use crate::configuration::store_login_details;
use crate::passwords::try_retrieve_password_from_keyring;
use std::str::FromStr; use std::str::FromStr;
use std::string::String; use std::string::String;
use crate::passwords::try_retrieve_password_from_keyring;
use crate::configuration::store_login_details;
pub mod login_screen; pub mod login_screen;
pub mod no_room_open; pub mod no_room_open;
@ -65,7 +65,6 @@ fn try_retrieve_credentials(config: &Configuration) -> Option<LoginCredentials>
} }
pub fn App(cx: Scope) -> Element { pub fn App(cx: Scope) -> Element {
let messages = use_ref(cx, || HashMap::new()); let messages = use_ref(cx, || HashMap::new());
let current_room = use_state(cx, || None::<BareJid>); let current_room = use_state(cx, || None::<BareJid>);
let connection_status = use_state(cx, || LoginStatus::LoggedOut); let connection_status = use_state(cx, || LoginStatus::LoggedOut);
@ -76,19 +75,22 @@ pub fn App(cx: Scope) -> Element {
let config = use_ref(cx, load_config); let config = use_ref(cx, load_config);
use_on_create(cx, || { use_on_create(cx, || {
let config = config.to_owned(); let config = config.to_owned();
let _connection_status = connection_status.to_owned(); let _connection_status = connection_status.to_owned();
let coroutine = coroutine.to_owned(); let coroutine = coroutine.to_owned();
async move { async move {
if let Some(credentials) = try_retrieve_credentials(&*config.read()) { if let Some(credentials) = try_retrieve_credentials(&*config.read()) {
info!("Will try to log in as {} with default nick {}", credentials.username, credentials.default_nick); info!(
coroutine.send(NetworkCommand::TryLogin { credentials }); "Will try to log in as {} with default nick {}",
} else { credentials.username, credentials.default_nick
info!("No stored credentials found; will not try to automatically log in."); );
} coroutine.send(NetworkCommand::TryLogin { credentials });
} else {
info!("No stored credentials found; will not try to automatically log in.");
} }
}); }
});
render! { render! {