Skip to content

Commit

Permalink
Use error variants instead of static strings
Browse files Browse the repository at this point in the history
  • Loading branch information
4e554c4c committed Sep 26, 2024
1 parent 2e7e8ce commit f6fe926
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 44 deletions.
25 changes: 13 additions & 12 deletions data/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ impl Config {
}

let path = Self::path();
if !path.try_exists()? {
return Err(Error::ConfigMissing { has_yaml_config: has_yaml_config()? });
}
let content = fs::read_to_string(path)
.await
.map_err(|e| Error::LoadConfigFile(e.to_string()))?;
Expand Down Expand Up @@ -294,8 +297,8 @@ pub fn random_nickname_with_seed<R: Rng>(rng: &mut R) -> String {
}

/// Has YAML configuration file.
pub fn has_yaml_config() -> bool {
config_dir().join("config.yaml").exists()
fn has_yaml_config() -> Result<bool, Error> {
Ok(config_dir().join("config.yaml").try_exists()?)
}

fn default_tooltip() -> bool {
Expand All @@ -318,16 +321,14 @@ pub enum Error {
StringUtf8Error(#[from] string::FromUtf8Error),
#[error(transparent)]
LoadSounds(#[from] audio::LoadError),
}

impl Error {
pub fn is_expected_on_first_load(&self) -> bool {
match self {
// If a user doesn't have a config when we start up, then we end up with a read error
Error::LoadConfigFile(_) => true,
_ => false,
}
}
#[error("Only one of password, password_file and password_command can be set.")]
DuplicatePassword(),
#[error("Only one of nick_password, nick_password_file and nick_password_command can be set.")]
DuplicateNickPassword(),
#[error("Exactly one of sasl.plain.password, sasl.plain.password_file or sasl.plain.password_command must be set.")]
DuplicateSaslPassword(),
#[error("Config does not exist")]
ConfigMissing { has_yaml_config: bool },
}

impl From<std::io::Error> for Error {
Expand Down
13 changes: 5 additions & 8 deletions data/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ use crate::config::server::Sasl;
use crate::config::Error;

pub type Handle = Sender<proto::Message>;
const DUP_PASS_MSG: &str = "Only one of password, password_file and password_command can be set.";
const DUP_NICK_PASS_MSG: &str = "Only one of nick_password, nick_password_file and nick_password_command can be set.";
const DUP_SASL_PASS_MSG: &str = "Exactly one of sasl.plain.password, sasl.plain.password_file or sasl.plain.password_command must be set.";

#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Server(String);
Expand Down Expand Up @@ -103,27 +100,27 @@ impl Map {
for (_, config) in self.0.iter_mut() {
if let Some(pass_file) = &config.password_file {
if config.password.is_some() || config.password_command.is_some() {
return Err(Error::Parse(DUP_PASS_MSG.to_string()));
return Err(Error::DuplicatePassword());
}
let pass = fs::read_to_string(pass_file).await?;
config.password = Some(pass);
}
if let Some(pass_command) = &config.password_command {
if config.password.is_some() {
return Err(Error::Parse(DUP_PASS_MSG.to_string()));
return Err(Error::DuplicatePassword());
}
config.password = Some(read_from_command(pass_command).await?);
}
if let Some(nick_pass_file) = &config.nick_password_file {
if config.nick_password.is_some() || config.nick_password_command.is_some() {
return Err(Error::Parse(DUP_NICK_PASS_MSG.to_string()));
return Err(Error::DuplicateNickPassword());
}
let nick_pass = fs::read_to_string(nick_pass_file).await?;
config.nick_password = Some(nick_pass);
}
if let Some(nick_pass_command) = &config.nick_password_command {
if config.password.is_some() {
return Err(Error::Parse(DUP_NICK_PASS_MSG.to_string()));
return Err(Error::DuplicateNickPassword());
}
config.password = Some(read_from_command(nick_pass_command).await?);
}
Expand Down Expand Up @@ -154,7 +151,7 @@ impl Map {
*password = Some(pass);
}
Sasl::Plain { .. } => {
return Err(Error::Parse(DUP_SASL_PASS_MSG.to_string()));
return Err(Error::DuplicateSaslPassword());
}
Sasl::External { .. } => {
// no passwords to read
Expand Down
46 changes: 22 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,30 +151,28 @@ impl Halloy {
command.map(Message::Dashboard),
)
}
Err(error) => {
if config::has_yaml_config() {
// If we have a YAML file, but end up in this arm
// it means the user tried to load Halloy with a YAML configuration, but it expected TOML.
(
Screen::Migration(screen::Migration::new()),
Config::default(),
Task::none(),
)
} else if error.is_expected_on_first_load() {
// Show regular welcome screen for new users.
(
Screen::Welcome(screen::Welcome::new()),
Config::default(),
Task::none(),
)
} else {
(
Screen::Help(screen::Help::new(error)),
Config::default(),
Task::none(),
)
}
},
// If we have a YAML file, but end up in this arm
// it means the user tried to load Halloy with a YAML configuration, but it expected TOML.
Err(config::Error::ConfigMissing {
has_yaml_config: true,
}) => (
Screen::Migration(screen::Migration::new()),
Config::default(),
Task::none(),
),
// Show regular welcome screen for new users.
Err(config::Error::ConfigMissing {
has_yaml_config: false,
}) => (
Screen::Welcome(screen::Welcome::new()),
Config::default(),
Task::none(),
),
Err(error) => (
Screen::Help(screen::Help::new(error)),
Config::default(),
Task::none(),
),
};

(
Expand Down

0 comments on commit f6fe926

Please sign in to comment.