remove unnessessary RefCell

This commit is contained in:
Dietrich 2021-06-07 07:27:37 +02:00 committed by Franz Dietrich
parent 93472c061e
commit 5da1d3fb16
2 changed files with 59 additions and 61 deletions

40
Cargo.lock generated
View File

@ -336,9 +336,9 @@ dependencies = [
[[package]] [[package]]
name = "addr2line" name = "addr2line"
version = "0.15.1" version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03345e98af8f3d786b6d9f656ccfa6ac316d954e92bc4841f0bba20789d5fb5a" checksum = "e7a2e47a1fbe209ee101dd6d61285226744c6c8d3c21c8dc878ba6cb9f467f3a"
dependencies = [ dependencies = [
"gimli", "gimli",
] ]
@ -579,9 +579,9 @@ dependencies = [
[[package]] [[package]]
name = "backtrace" name = "backtrace"
version = "0.3.59" version = "0.3.60"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4717cfcbfaa661a0fd48f8453951837ae7e8f81e481fbb136e3202d72805a744" checksum = "b7815ea54e4d821e791162e078acbebfd6d8c8939cd559c9335dceb1c8ca7282"
dependencies = [ dependencies = [
"addr2line", "addr2line",
"cc", "cc",
@ -1807,9 +1807,9 @@ dependencies = [
[[package]] [[package]]
name = "heck" name = "heck"
version = "0.3.2" version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87cbf45460356b7deeb5e3415b5563308c0a9b057c85e12b06ad551f98d0a6ac" checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
dependencies = [ dependencies = [
"unicode-segmentation", "unicode-segmentation",
] ]
@ -2458,9 +2458,12 @@ dependencies = [
[[package]] [[package]]
name = "object" name = "object"
version = "0.24.0" version = "0.25.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a5b3dd1c072ee7963717671d1ca129f1048fda25edea6b752bfc71ac8854170" checksum = "f8bc1d42047cf336f0f939c99e97183cf31551bf0f2865a2ec9c8d91fd4ffb5e"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
@ -3226,11 +3229,10 @@ dependencies = [
[[package]] [[package]]
name = "regex-automata" name = "regex-automata"
version = "0.1.9" version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae1ded71d66a4a97f5e961fd0cb25a5f366a42a41570d16a763a69c092c26ae4" checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
dependencies = [ dependencies = [
"byteorder",
"regex-syntax", "regex-syntax",
] ]
@ -3410,9 +3412,9 @@ dependencies = [
[[package]] [[package]]
name = "security-framework" name = "security-framework"
version = "2.2.0" version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3670b1d2fdf6084d192bc71ead7aabe6c06aa2ea3fbd9cc3ac111fa5c2b1bd84" checksum = "b239a3d5db51252f6f48f42172c65317f37202f4a21021bf5f9d40a408f4592c"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"core-foundation", "core-foundation",
@ -3423,9 +3425,9 @@ dependencies = [
[[package]] [[package]]
name = "security-framework-sys" name = "security-framework-sys"
version = "2.2.0" version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3676258fd3cfe2c9a0ec99ce3038798d847ce3e4bb17746373eb9f0f1ac16339" checksum = "7e4effb91b4b8b6fb7732e670b6cee160278ff8e6bf485c7805d9e319d76e284"
dependencies = [ dependencies = [
"core-foundation-sys", "core-foundation-sys",
"libc", "libc",
@ -3599,9 +3601,9 @@ dependencies = [
[[package]] [[package]]
name = "signal-hook-registry" name = "signal-hook-registry"
version = "1.3.0" version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16f1d0fef1604ba8f7a073c7e701f213e056707210e9020af4528e0101ce11a6" checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0"
dependencies = [ dependencies = [
"libc", "libc",
] ]
@ -4545,9 +4547,9 @@ dependencies = [
[[package]] [[package]]
name = "unicode-normalization" name = "unicode-normalization"
version = "0.1.18" version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33717dca7ac877f497014e10d73f3acf948c342bee31b5ca7892faf94ccc6b49" checksum = "d54590932941a9e9266f0832deed84ebe1bf2e4c9e4a3554d393d18f5e854bf9"
dependencies = [ dependencies = [
"tinyvec", "tinyvec",
] ]

View File

@ -1,5 +1,3 @@
use std::cell::RefCell;
use enum_map::EnumMap; use enum_map::EnumMap;
use fluent::fluent_args; use fluent::fluent_args;
use image::{DynamicImage, ImageOutputFormat, Luma}; use image::{DynamicImage, ImageOutputFormat, Luma};
@ -26,9 +24,12 @@ pub fn init(mut url: Url, orders: &mut impl Orders<Msg>, i18n: I18n) -> Model {
orders.send_msg(Msg::Query(QueryMsg::Fetch)); orders.send_msg(Msg::Query(QueryMsg::Fetch));
// if the url contains create_link set the edit_link variable. // if the url contains create_link set the edit_link variable.
// This variable then opens the create link dialog. // This variable then opens the create link dialog.
let edit_link = match url.next_path_part() { let dialog = match url.next_path_part() {
Some("create_link") => Some(RefCell::new(LinkDelta::default())), Some("create_link") => Dialog::EditLink {
None | Some(_) => None, link_delta: LinkDelta::default(),
qr: Loadable::Data(None),
},
None | Some(_) => Dialog::None,
}; };
Model { Model {
@ -36,7 +37,7 @@ pub fn init(mut url: Url, orders: &mut impl Orders<Msg>, i18n: I18n) -> Model {
i18n, // to translate i18n, // to translate
formconfig: LinkRequestForm::default(), // when requesting links the form is stored here formconfig: LinkRequestForm::default(), // when requesting links the form is stored here
inputs: EnumMap::default(), // the input fields for the searches inputs: EnumMap::default(), // the input fields for the searches
dialog: Dialog::None, dialog,
} }
} }
@ -49,10 +50,10 @@ pub struct Model {
dialog: Dialog, // User interaction - there can only ever be one dialog open. dialog: Dialog, // User interaction - there can only ever be one dialog open.
} }
#[derive(Debug)] #[derive(Debug, Clone)]
enum Dialog { enum Dialog {
EditLink { EditLink {
link_delta: RefCell<LinkDelta>, link_delta: LinkDelta,
qr: Loadable<QrGuard>, qr: Loadable<QrGuard>,
}, },
Message(Status), Message(Status),
@ -67,20 +68,19 @@ pub struct QrGuard {
} }
impl QrGuard { impl QrGuard {
fn new(link_delta: RefCell<LinkDelta>) -> Self { fn new(link_delta: LinkDelta) -> Self {
log!("Generating new QrCode");
let link_delta = link_delta.borrow();
let svg = generate_qr_from_code(&link_delta.code);
use std::array;
use std::iter::FromIterator; use std::iter::FromIterator;
log!("Generating new QrCode");
let LinkDelta { code, .. } = link_delta;
let svg = generate_qr_from_code(&code);
let mut properties = web_sys::BlobPropertyBag::new(); let mut properties = web_sys::BlobPropertyBag::new();
properties.type_("image/png"); properties.type_("image/png");
let png_vec = generate_qr_png(&link_delta.code); let png_vec = generate_qr_png(&code);
let png_jsarray: JsValue = js_sys::Uint8Array::from(&png_vec[..]).into(); let png_jsarray: JsValue = js_sys::Uint8Array::from(&png_vec[..]).into();
// the buffer has to be an array of arrays // the buffer has to be an array of arrays
let png_buffer = js_sys::Array::from_iter(array::IntoIter::new([png_jsarray])); let png_buffer = js_sys::Array::from_iter(std::array::IntoIter::new([png_jsarray]));
let png_blob = let png_blob =
web_sys::Blob::new_with_buffer_source_sequence_and_options(&png_buffer, &properties) web_sys::Blob::new_with_buffer_source_sequence_and_options(&png_buffer, &properties)
.unwrap(); .unwrap();
@ -264,7 +264,7 @@ fn load_links(model: &Model, orders: &mut impl Orders<Msg>) {
pub fn process_edit_messages(msg: EditMsg, model: &mut Model, orders: &mut impl Orders<Msg>) { pub fn process_edit_messages(msg: EditMsg, model: &mut Model, orders: &mut impl Orders<Msg>) {
match msg { match msg {
EditMsg::EditSelected(link) => { EditMsg::EditSelected(link) => {
let link_delta = RefCell::new(link); let link_delta = link;
model.dialog = Dialog::EditLink { model.dialog = Dialog::EditLink {
link_delta: link_delta.clone(), link_delta: link_delta.clone(),
qr: Loadable::Data(None), qr: Loadable::Data(None),
@ -301,7 +301,7 @@ pub fn process_edit_messages(msg: EditMsg, model: &mut Model, orders: &mut impl
EditMsg::CreateNewLink => { EditMsg::CreateNewLink => {
clear_all(model); clear_all(model);
model.dialog = Dialog::EditLink { model.dialog = Dialog::EditLink {
link_delta: RefCell::new(LinkDelta::default()), link_delta: LinkDelta::default(),
qr: Loadable::Data(None), qr: Loadable::Data(None),
} }
} }
@ -311,31 +311,31 @@ pub fn process_edit_messages(msg: EditMsg, model: &mut Model, orders: &mut impl
orders.send_msg(Msg::Query(QueryMsg::Fetch)); orders.send_msg(Msg::Query(QueryMsg::Fetch));
} }
EditMsg::EditCodeChanged(s) => { EditMsg::EditCodeChanged(s) => {
if let Dialog::EditLink { ref link_delta, .. } = model.dialog { if let Dialog::EditLink {
link_delta ref mut link_delta, ..
.try_borrow_mut() } = model.dialog
.expect("Failed to borrow mutably") {
.code = s; link_delta.code = s;
} }
} }
EditMsg::EditDescriptionChanged(s) => { EditMsg::EditDescriptionChanged(s) => {
if let Dialog::EditLink { ref link_delta, .. } = model.dialog { if let Dialog::EditLink {
link_delta ref mut link_delta, ..
.try_borrow_mut() } = model.dialog
.expect("Failed to borrow mutably") {
.title = s; link_delta.title = s;
} }
} }
EditMsg::EditTargetChanged(s) => { EditMsg::EditTargetChanged(s) => {
if let Dialog::EditLink { ref link_delta, .. } = model.dialog { if let Dialog::EditLink {
link_delta ref mut link_delta, ..
.try_borrow_mut() } = model.dialog
.expect("Failed to borrow mutably") {
.target = s; link_delta.target = s;
} }
} }
EditMsg::SaveLink => { EditMsg::SaveLink => {
if let Dialog::EditLink { ref link_delta, .. } = model.dialog { if let Dialog::EditLink { link_delta, .. } = model.dialog.clone() {
save_link(link_delta, orders); save_link(link_delta, orders);
} }
} }
@ -391,8 +391,8 @@ pub fn process_edit_messages(msg: EditMsg, model: &mut Model, orders: &mut impl
} }
/// Send a link save request to the server. /// Send a link save request to the server.
fn save_link(link_delta: &RefCell<LinkDelta>, orders: &mut impl Orders<Msg>) { fn save_link(link_delta: LinkDelta, orders: &mut impl Orders<Msg>) {
let data = link_delta.borrow().clone(); let data = link_delta;
orders.perform_cmd(async { orders.perform_cmd(async {
let data = data; let data = data;
// create the request // create the request
@ -455,7 +455,6 @@ pub fn view(model: &Model) -> Node<Msg> {
), ),
a![t("no"), C!["button"], ev(Ev::Click, |_| Msg::ClearAll)], a![t("no"), C!["button"], ev(Ev::Click, |_| Msg::ClearAll)],
a![t("yes"), C!["button"], { a![t("yes"), C!["button"], {
let l = l.clone();
ev(Ev::Click, move |_| Msg::Edit(EditMsg::DeleteSelected(l))) ev(Ev::Click, move |_| Msg::Edit(EditMsg::DeleteSelected(l)))
}] }]
]] ]]
@ -625,11 +624,10 @@ fn view_link(l: &FullLink) -> Node<Msg> {
/// display a link editing dialog with save and close button /// display a link editing dialog with save and close button
fn edit_or_create_link<F: Fn(&str) -> String>( fn edit_or_create_link<F: Fn(&str) -> String>(
l: &RefCell<LinkDelta>, link: &LinkDelta,
qr: &Loadable<QrGuard>, qr: &Loadable<QrGuard>,
t: F, t: F,
) -> Node<Msg> { ) -> Node<Msg> {
let link = l.borrow();
div![ div![
// close button top right // close button top right
C!["editdialog", "center"], C!["editdialog", "center"],
@ -678,7 +676,7 @@ fn edit_or_create_link<F: Fn(&str) -> String>(
th![t("qr-code")], th![t("qr-code")],
if let Loadable::Data(Some(qr)) = qr { if let Loadable::Data(Some(qr)) = qr {
td![a![ td![a![
span!["Download", /* raw!(&qr.svg) */], span!["Download", raw!(&qr.svg),],
attrs!(At::Href => qr.url, At::Download => "qr-code.png") attrs!(At::Href => qr.url, At::Download => "qr-code.png")
]] ]]
} else { } else {
@ -735,7 +733,5 @@ fn generate_qr_png(code: &str) -> Vec<u8> {
DynamicImage::ImageLuma8(png) DynamicImage::ImageLuma8(png)
.write_to(&mut temporary_data, ImageOutputFormat::Png) .write_to(&mut temporary_data, ImageOutputFormat::Png)
.unwrap(); .unwrap();
let image_data = temporary_data.into_inner(); temporary_data.into_inner()
image_data
} }