From 134efdab2541eb505e6f6fedc2fb48f94e1ab3be Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Wed, 27 Jul 2022 22:58:01 +0300 Subject: feat(predlozhnik): implement fever-dream like case selection don't look too closely at this, it's kind of insane Change-Id: I789ddd9dd5a4cf28b3007e38ef1c345e639a5fc1 Reviewed-on: https://cl.tvl.fyi/c/depot/+/5983 Tested-by: BuildkiteCI Reviewed-by: tazjin --- users/tazjin/predlozhnik/src/main.rs | 101 +++++++++++++++++++++++++++++++++-- 1 file changed, 97 insertions(+), 4 deletions(-) (limited to 'users/tazjin') diff --git a/users/tazjin/predlozhnik/src/main.rs b/users/tazjin/predlozhnik/src/main.rs index f2e46da48f..65c11f07db 100644 --- a/users/tazjin/predlozhnik/src/main.rs +++ b/users/tazjin/predlozhnik/src/main.rs @@ -1,3 +1,4 @@ +use yew::html::Scope; use yew::prelude::*; use lazy_static::lazy_static; @@ -97,6 +98,16 @@ lazy_static! { m }; + static ref ПАДЕЖИ: BTreeSet<Падеж> = BTreeSet::from(Падеж::ВСЕ); + static ref ПРЕДЛОГИ: BTreeSet<&'static str> = { + let mut s: BTreeSet<&'static str> = BTreeSet::new(); + + for п in ПО_ПРЕДЛОГУ.keys() { + s.insert(п); + } + + s + }; } fn example_output() -> String { @@ -156,13 +167,68 @@ fn ограничить(м: &Модель) -> Вывод { }, (None, None) => Вывод { - доступные_падежи: BTreeSet::new(), - доступные_предлоги: BTreeSet::new(), + доступные_падежи: ПАДЕЖИ.clone(), + доступные_предлоги: ПРЕДЛОГИ.clone(), объяснение: None, }, } } +fn покажи_предлог( + link: &Scope<Модель>, + м: &Модель, + вв: &Вывод, + п: &'static str, +) -> Html { + let выбран = м.предлог == Some(п); + let доступен = вв.доступные_предлоги.contains(п); + + let mut класс = "btn btn-ghost ".to_string(); + класс += match (выбран, доступен) { + (true, _) => "btn-error", + (false, true) => "btn-primary", + (false, false) => "btn-default", + }; + + html! { + + } +} + +fn покажи_падеж( + link: &Scope<Модель>, м: &Модель, вв: &Вывод, п: Падеж +) -> Html { + let выбран = м.падеж == Some(п); + let доступен = вв.доступные_падежи.contains(&п); + + let mut класс = "btn btn-ghost ".to_string(); + класс += match (выбран, доступен) { + (true, _) => "btn-error", + (false, true) => "btn-primary", + (false, false) => "btn-default", + }; + + html! { + + } +} + impl Component for Модель { type Message = Сообщение; type Properties = (); @@ -180,9 +246,36 @@ impl Component for Модель { true } - fn view(&self, _ctx: &Context) -> Html { + fn view(&self, ctx: &Context) -> Html { + let вв = ограничить(self); + let link = ctx.link(); + + let кнапки_предлогов = ПРЕДЛОГИ + .iter() + .map(|п| покажи_предлог(link, self, &вв, п)) + .collect::(); + + let кнапки_падежов = ПАДЕЖИ + .iter() + .map(|п| покажи_падеж(link, self, &вв, *п)) + .collect::(); + html! { -
{example_output()}
+ <> + +
+

{"Предлоги:"}

+ {кнапки_предлогов} +
+
+ +
+

{"Падежи:"}

+ {кнапки_падежов} +
+
+ } } } -- cgit 1.4.1