Skip to content

Rust locale#533

Merged
jreidinger merged 76 commits intomasterfrom
rust_locale
May 24, 2023
Merged

Rust locale#533
jreidinger merged 76 commits intomasterfrom
rust_locale

Conversation

@jreidinger
Copy link
Contributor

@jreidinger jreidinger commented Apr 14, 2023

Problem

Language dbus backend is quite limited in functionality and still eating and lot of memory.

Solution

try to write language component in rust to compare it.

  1. The D-Bus interface org.opensuse.Agama.Language1 is replaced by org.opensuse.Agama.Locale1, described in doc/locale_api.md

  2. Two new crates in the rust/ directory:

  • agama-dbus-server the common server for Rust-hosted services
  • agama-locale-data library interfacing with langtable, in particular python-langtable-data.rpm which is just the XML part without the Python code
$ cd service; bundle exec bin/agamactl 
ERROR: Using 'agamactl' to start all services no longer works.
NOTE: It had race conditions all along and now there's a Rust service it can't reach too.
NOTE: Use `systemctl start agama.service` instead
NOTE: which is setup by a) RPMs b) ./setup-service.sh

@coveralls
Copy link

coveralls commented Apr 14, 2023

Coverage Status

Changes unknown
when pulling ce3362a on rust_locale
into ** on master**.


#[derive(Debug, Deserialize)]
pub struct Keyboards {
pub keyboard: Vec<Keyboard>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np: If you do not plan to add anything else, you could use the newtype pattern idiom.

Copy link
Contributor

@imobachgs imobachgs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know whether we need to split the code into so many modules, but it looks fine, as I do not have enough experience. I would do reduce the amount of repeated code too.

And thanks for writing those unit tests 💯

pub mod ranked;

pub fn get_keyboards() -> keyboard::Keyboards {
const FILE_PATH: &str = "/usr/share/langtable/data/keyboards.xml.gz";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put all the constants outside of the functions so it is easier to adjust the names. Although I would expect Rust not define the const on every single call.

self.keyboard_id = keyboard.to_string();
}

fn list_timezones(&self, locale: &str) -> Vec<(String, String)> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bad input makes a thread panic and then the service is running in an unusable state. We should send error replies instead

$ ./target/debug/agama-dbus-server &
$ busctl --user call org.opensuse.Agama.Locale1 /org/opensuse/Agama/Locale1 org.opensuse.Agama.Locale1 ListTimezones s idontknow
thread 'zbus::Connection executor' panicked at 'Wrong language {language}', agama-locale-data/src/localization.rs:12:14
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Call failed: Connection timed out

@mvidner
Copy link
Contributor

mvidner commented Apr 24, 2023

@jreidinger I have pushed code that enables error reporting via the D-Bus methods

Funnily, it does not help with the error that I originally encountered: ListTimezones("nosuchlanguage") because that one is pretty deep down the call stack and I want to think about the general API there first.

#[test]
fn test_get_languages() {
let result = get_languages().unwrap();
assert_eq!(result.language.len(), 356);
Copy link
Contributor

@mvidner mvidner Apr 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess what, on my system this fails because the actual result is 354 🤣

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting why you have different data from langtable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I took an old python3-langtable-0.0.51 and symlinked the directory. But the result is valid even for proper packaging: upstream will add some data and this will break our brittle tests.

considering langtable,
de-emphasizing systemd but not throwing it away just yet
  because systemd:locale != langtable:(territory+language)
  well yes but the `+` is a nontrivial operation
@mvidner
Copy link
Contributor

mvidner commented May 11, 2023

CI Failures:

  • integration-tests: Playwright... ?? Do I need to run it locally to figure it out
  • cli_build: (that's Rust build) python-langtable-data is missing; the job runs cargo on ububtu-latest

mvidner added 5 commits May 11, 2023 14:59
this only helps for local testing, not for CI
TODO: move this test to integration tests where we can install the
python-langtable-data dependency
Its child services would fail anyway when they could not reach Locale
…ks it

that's why we would see the login screen in Playwright tests

and that needs the frontend built and installed first
@mvidner mvidner marked this pull request as ready for review May 22, 2023 05:31
@mvidner
Copy link
Contributor

mvidner commented May 22, 2023

@jreidinger I made CI green again, yay :)

Copy link
Contributor

@imobachgs imobachgs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach looks good. I have some comments, especially about the internal APIs. Nothing to block this PR, though.

let (loc_language, loc_territory) = agama_locale_data::parse_locale(locale.as_str())?;
let languages = agama_locale_data::get_languages()?;
let territories = agama_locale_data::get_territories()?;
let language = languages
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that get_languages and get_territories return their structs (territory::Territory and languages::Languages). But it would be even better if those structs implemented an API to find a language or a territory instead of iterating here. You would not even need to know that the languages::Languages has a language field.

Not to mention that this code would be much shorter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I think it makes sense. I just want to have something working quickly and then abstract usable parts to those structs. Parts that are really used.


#[dbus_interface(property)]
fn locales(&self) -> Vec<String> {
return self.locales.to_owned();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say it is more idiomatic to omit the return (and the semicolon).


#[dbus_interface(property)]
fn supported_locales(&self) -> Vec<String> {
return self.supported_locales.to_owned();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return self.supported_locales.to_owned();
self.supported_locales.to_owned()

mvidner and others added 5 commits May 24, 2023 09:48
Co-authored-by: Imobach González Sosa <igonzalezsosa@suse.com>
and other doc fixes thanks to Imo's review
or else the daemon WILL disconnect you!! ;-)
thanks to Imo and https://www.deepl.com/write

Also link to the cargo_audit PR

CI skip, docs only
mvidner added 3 commits May 24, 2023 17:11
by running `cargo test` in integration_tests job where we do have the
langtable dependency

With that, integration_tests has swallowed the entire cli_build job.
This makes python-langtable-data available if you only set up the
backend

I hope I got the split right
Copy link
Contributor

@mvidner mvidner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay, thanks, LGTM now 😄

@jreidinger jreidinger merged commit d09d1e1 into master May 24, 2023
@jreidinger jreidinger deleted the rust_locale branch May 24, 2023 17:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants