Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 9fb998b3 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

Add the Config module (cleaner code)

parent 28652c4b
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
#![allow(dead_code)]
use ini::{Error::Io as IniErrIo, Error::Parse as IniErrParse, Ini};
use url::Url;
pub struct Config {
pub display_name: String, // The display name to set for the bot
pub homeserver_url: Url, // The url of the home server, like https://matrix.iiens.net
pub user_name: String, // The true user name
pub user_password: String, // The true user name
}
pub fn from_file(file_name: &String) -> Result<Config, String> {
return match Ini::load_from_file(file_name) {
Err(IniErrIo(e)) => Err(e.to_string()),
Err(IniErrParse(e)) => Err(e.to_string()),
Ok(conf) => {
let hs_url_str = conf.get_from_or(Some("user"), "homeserver", "https://matrix.org");
return match Url::parse(hs_url_str) {
Err(e) => Err(e.to_string()),
Ok(hs_url) => Ok(Config {
user_name: conf
.get_from_or(Some("user"), "id", "@some-id:matrix.org")
.to_string(),
user_password: conf
.get_from_or(Some("user"), "password", "no-password")
.to_string(),
display_name: conf
.get_from_or(Some("user"), "display_name", "MGB-Hitagi")
.to_string(),
homeserver_url: hs_url,
}),
};
}
};
}
pub fn write_default(file_name: &String) -> Result<(), String> {
let mut conf = Ini::new();
conf.with_section(Some("user"))
.set("id", "@some_id:matrix.org")
.set("password", "no-password")
.set("display_name", "MGB-Hitagi");
return match conf.write_to_file_policy(file_name, ini::EscapePolicy::Everything) {
Ok(()) => Ok(()),
Err(e) => Err(e.to_string()),
};
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter