Skip to content

Commit

Permalink
Convert LocaleError into Error
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Mar 15, 2024
1 parent 36ee8f9 commit 635ebcb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
8 changes: 5 additions & 3 deletions rust/agama-server/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use axum::{
};
use serde_json::json;

use crate::questions::QuestionsError;
use crate::{l10n::web::LocaleError, questions::QuestionsError};

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand All @@ -16,8 +16,10 @@ pub enum Error {
Anyhow(String),
#[error("Agama service error: {0}")]
Service(#[from] ServiceError),
#[error("Answers handling error: {0}")]
Answers(QuestionsError),
#[error("Questions service error: {0}")]
Questions(QuestionsError),
#[error("Software service error: {0}")]
Locale(#[from] LocaleError),
}

// This would be nice, but using it for a return type
Expand Down
38 changes: 13 additions & 25 deletions rust/agama-server/src/l10n/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,25 @@ use crate::{
use agama_locale_data::{InvalidKeymap, LocaleId};
use axum::{
extract::State,
http::StatusCode,
response::{IntoResponse, Response},
routing::{get, put},
Json, Router,
};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::{
process::Command,
sync::{Arc, RwLock},
};
use thiserror::Error;

#[derive(Error, Debug)]
#[derive(thiserror::Error, Debug)]
pub enum LocaleError {
#[error("Unknown locale code: {0}")]
UnknownLocale(String),
#[error("Unknown timezone: {0}")]
UnknownTimezone(String),
#[error("Invalid keymap: {0}")]
InvalidKeymap(#[from] InvalidKeymap),
#[error("Cannot translate: {0}")]
OtherError(#[from] Error),
#[error("Cannot change the local keymap: {0}")]
CouldNotSetKeymap(#[from] std::io::Error),
}

impl IntoResponse for LocaleError {
fn into_response(self) -> Response {
let body = json!({
"error": self.to_string()
});
(StatusCode::BAD_REQUEST, Json(body)).into_response()
}
#[error("Could not apply the changes")]
Commit(#[from] std::io::Error),
}

#[derive(Clone)]
Expand Down Expand Up @@ -119,14 +104,14 @@ async fn keymaps(State(state): State<LocaleState>) -> Json<Vec<Keymap>> {
async fn set_config(
State(state): State<LocaleState>,
Json(value): Json<LocaleConfig>,
) -> Result<Json<()>, LocaleError> {
) -> Result<Json<()>, Error> {
let mut data = state.locale.write().unwrap();
let mut changes = LocaleConfig::default();

if let Some(locales) = &value.locales {
for loc in locales {
if !data.locales_db.exists(loc.as_str()) {
return Err(LocaleError::UnknownLocale(loc.to_string()));
return Err(LocaleError::UnknownLocale(loc.to_string()))?;
}
}
data.locales = locales.clone();
Expand All @@ -135,14 +120,14 @@ async fn set_config(

if let Some(timezone) = &value.timezone {
if !data.timezones_db.exists(timezone) {
return Err(LocaleError::UnknownTimezone(timezone.to_string()));
return Err(LocaleError::UnknownTimezone(timezone.to_string()))?;
}
data.timezone = timezone.to_owned();
changes.timezone = Some(data.timezone.clone());
}

if let Some(keymap_id) = &value.keymap {
data.keymap = keymap_id.parse()?;
data.keymap = keymap_id.parse().map_err(LocaleError::InvalidKeymap)?;
changes.keymap = Some(keymap_id.clone());
}

Expand All @@ -161,14 +146,17 @@ async fn set_config(
}

if let Some(ui_keymap) = &value.ui_keymap {
data.ui_keymap = ui_keymap.parse()?;
// data.ui_keymap = ui_keymap.parse().into::<Result<KeymapId, LocaleError>>()?;
data.ui_keymap = ui_keymap.parse().map_err(LocaleError::InvalidKeymap)?;
Command::new("/usr/bin/localectl")
.args(["set-x11-keymap", &ui_keymap])
.output()?;
.output()
.map_err(LocaleError::Commit)?;
Command::new("/usr/bin/setxkbmap")
.arg(&ui_keymap)
.env("DISPLAY", ":0")
.output()?;
.output()
.map_err(LocaleError::Commit)?;
}

_ = state.events.send(Event::L10nConfigChanged(changes));
Expand Down

0 comments on commit 635ebcb

Please sign in to comment.