From 01df04be60478017fdc309e96af3d7e296b51128 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Cerveau?= Date: Wed, 23 Feb 2022 12:51:14 +0100 Subject: [PATCH] move glib value_as_str to common module --- src/common.rs | 17 +++++++++++++++++ src/ui/properties.rs | 21 +++------------------ 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/common.rs b/src/common.rs index 42b83d8..3bd823f 100644 --- a/src/common.rs +++ b/src/common.rs @@ -7,6 +7,7 @@ // SPDX-License-Identifier: GPL-3.0-only use anyhow::Result; +use gtk::glib; pub fn init() -> Result<()> { unsafe { @@ -16,3 +17,19 @@ pub fn init() -> Result<()> { gtk::init()?; Ok(()) } + +pub fn value_as_str(v: &glib::Value) -> Option { + match v.type_() { + glib::Type::I8 => Some(str_some_value!(v, i8).to_string()), + glib::Type::U8 => Some(str_some_value!(v, u8).to_string()), + glib::Type::BOOL => Some(str_some_value!(v, bool).to_string()), + glib::Type::I32 => Some(str_some_value!(v, i32).to_string()), + glib::Type::U32 => Some(str_some_value!(v, u32).to_string()), + glib::Type::I64 => Some(str_some_value!(v, i64).to_string()), + glib::Type::U64 => Some(str_some_value!(v, u64).to_string()), + glib::Type::F32 => Some(str_some_value!(v, f32).to_string()), + glib::Type::F64 => Some(str_some_value!(v, f64).to_string()), + glib::Type::STRING => str_opt_value!(v, String), + _ => None, + } +} diff --git a/src/ui/properties.rs b/src/ui/properties.rs index 6a47dcc..439d5d4 100644 --- a/src/ui/properties.rs +++ b/src/ui/properties.rs @@ -7,6 +7,7 @@ // SPDX-License-Identifier: GPL-3.0-only use crate::app::GPSApp; +use crate::common; use crate::gps as GPS; use crate::logger; use crate::ui as GPSUI; @@ -18,22 +19,6 @@ use std::cell::RefCell; use std::collections::HashMap; use std::rc::Rc; -fn value_as_str(v: &glib::Value) -> Option { - match v.type_() { - glib::Type::I8 => Some(str_some_value!(v, i8).to_string()), - glib::Type::U8 => Some(str_some_value!(v, u8).to_string()), - glib::Type::BOOL => Some(str_some_value!(v, bool).to_string()), - glib::Type::I32 => Some(str_some_value!(v, i32).to_string()), - glib::Type::U32 => Some(str_some_value!(v, u32).to_string()), - glib::Type::I64 => Some(str_some_value!(v, i64).to_string()), - glib::Type::U64 => Some(str_some_value!(v, u64).to_string()), - glib::Type::F32 => Some(str_some_value!(v, f32).to_string()), - glib::Type::F64 => Some(str_some_value!(v, f64).to_string()), - glib::Type::STRING => str_opt_value!(v, String), - _ => None, - } -} - pub fn property_to_widget( app: &GPSApp, node_id: u32, @@ -57,7 +42,7 @@ pub fn property_to_widget( { check_button.set_active(value.parse::().unwrap_or(false)); } - } else if let Some(value) = value_as_str(param.default_value()) { + } else if let Some(value) = common::value_as_str(param.default_value()) { check_button.set_active(value.parse::().unwrap_or(false)); } check_button.connect_toggled(glib::clone!(@weak check_button => move |c| { @@ -88,7 +73,7 @@ pub fn property_to_widget( { entry.set_text(&value); } - } else if let Some(value) = value_as_str(param.default_value()) { + } else if let Some(value) = common::value_as_str(param.default_value()) { entry.set_text(&value); }