Use pattern matching instead of if and else that return true and false

Differential Revision: https://phabricator.kde.org/D16576
master
Carl Schwan 2018-11-01 00:52:48 +01:00
parent b8ebcfae05
commit ec81047ac1
No known key found for this signature in database
GPG Key ID: E8A8FFA1234DF4A2
4 changed files with 24 additions and 56 deletions

View File

@ -229,36 +229,14 @@ impl SimpleTypePrivate for SimpleType {
match self { match self {
SimpleType::QString => "const QString&", SimpleType::QString => "const QString&",
SimpleType::QByteArray => "const QByteArray&", SimpleType::QByteArray => "const QByteArray&",
SimpleType::Bool => "bool", _ => self.name(),
SimpleType::Float => "float",
SimpleType::Double => "double",
SimpleType::Void => "void",
SimpleType::Qint8 => "qint8",
SimpleType::Qint16 => "qint16",
SimpleType::Qint32 => "qint32",
SimpleType::Qint64 => "qint64",
SimpleType::QUint8 => "quint8",
SimpleType::QUint16 => "quint16",
SimpleType::QUint32 => "quint32",
SimpleType::QUint64 => "quint64",
} }
} }
fn c_set_type(&self) -> &str { fn c_set_type(&self) -> &str {
match self { match self {
SimpleType::QString => "qstring_t", SimpleType::QString => "qstring_t",
SimpleType::QByteArray => "qbytearray_t", SimpleType::QByteArray => "qbytearray_t",
SimpleType::Bool => "bool", _ => self.name(),
SimpleType::Float => "float",
SimpleType::Double => "double",
SimpleType::Void => "void",
SimpleType::Qint8 => "qint8",
SimpleType::Qint16 => "qint16",
SimpleType::Qint32 => "qint32",
SimpleType::Qint64 => "qint64",
SimpleType::QUint8 => "quint8",
SimpleType::QUint16 => "quint16",
SimpleType::QUint32 => "quint32",
SimpleType::QUint64 => "quint64",
} }
} }
fn rust_type(&self) -> &str { fn rust_type(&self) -> &str {
@ -284,21 +262,13 @@ impl SimpleTypePrivate for SimpleType {
SimpleType::QString => "String::new()", SimpleType::QString => "String::new()",
SimpleType::QByteArray => "Vec::new()", SimpleType::QByteArray => "Vec::new()",
SimpleType::Bool => "false", SimpleType::Bool => "false",
SimpleType::Float => "0.0", SimpleType::Float | SimpleType::Double => "0.0",
SimpleType::Double => "0.0",
SimpleType::Void => "()", SimpleType::Void => "()",
SimpleType::Qint8 => "0", _ => "0",
SimpleType::Qint16 => "0",
SimpleType::Qint32 => "0",
SimpleType::Qint64 => "0",
SimpleType::QUint8 => "0",
SimpleType::QUint16 => "0",
SimpleType::QUint32 => "0",
SimpleType::QUint64 => "0",
} }
} }
fn is_complex(self) -> bool { fn is_complex(&self) -> bool {
self == SimpleType::QString || self == SimpleType::QByteArray self == &SimpleType::QString || self == &SimpleType::QByteArray
} }
} }
@ -310,17 +280,15 @@ pub enum Type {
impl TypePrivate for Type { impl TypePrivate for Type {
fn is_object(&self) -> bool { fn is_object(&self) -> bool {
if let Type::Object(_) = self { match self {
true Type::Object(_) => true,
} else { _ => false,
false
} }
} }
fn is_complex(&self) -> bool { fn is_complex(&self) -> bool {
if let Type::Simple(simple) = self { match self {
simple.is_complex() Type::Simple(simple) => simple.is_complex(),
} else { _ => false,
false
} }
} }
fn name(&self) -> &str { fn name(&self) -> &str {
@ -381,9 +349,9 @@ impl ItemPropertyPrivate for ItemProperty {
self.item_property_type.is_complex() self.item_property_type.is_complex()
} }
fn cpp_set_type(&self) -> String { fn cpp_set_type(&self) -> String {
let mut t = self.item_property_type.cpp_set_type().to_string(); let t = self.item_property_type.cpp_set_type().to_string();
if self.optional { if self.optional {
t = "option_".to_string() + &t; return "option_".to_string() + &t;
} }
t t
} }

View File

@ -37,7 +37,7 @@ pub trait SimpleTypePrivate {
fn c_set_type(&self) -> &str; fn c_set_type(&self) -> &str;
fn rust_type(&self) -> &str; fn rust_type(&self) -> &str;
fn rust_type_init(&self) -> &str; fn rust_type_init(&self) -> &str;
fn is_complex(self) -> bool; fn is_complex(&self) -> bool;
} }
pub trait ItemPropertyPrivate { pub trait ItemPropertyPrivate {

View File

@ -1,3 +1,5 @@
//! `cpp` is the module that generates the cpp code for the bindings
use configuration::*; use configuration::*;
use configuration_private::*; use configuration_private::*;
use std::io::{Result, Write}; use std::io::{Result, Write};
@ -5,10 +7,9 @@ use util::{snake_case, write_if_different};
fn property_type(p: &ItemProperty) -> String { fn property_type(p: &ItemProperty) -> String {
if p.optional && !p.item_property_type.is_complex() { if p.optional && !p.item_property_type.is_complex() {
"QVariant".into() return "QVariant".into();
} else {
p.type_name().to_string()
} }
p.type_name().to_string()
} }
fn upper_initial(name: &str) -> String { fn upper_initial(name: &str) -> String {
@ -210,12 +211,9 @@ public:
} }
fn is_column_write(o: &Object, col: usize) -> bool { fn is_column_write(o: &Object, col: usize) -> bool {
for ip in o.item_properties.values() { o.item_properties
if ip.write && (col == 0 || (ip.roles.len() > col && !ip.roles[col].is_empty())) { .values()
return true; .any(|ip|ip.write && (col == 0 || (ip.roles.len() > col && !ip.roles[col].is_empty())))
}
}
false
} }
fn write_function_c_decl( fn write_function_c_decl(

View File

@ -1,3 +1,5 @@
//! `rust` is the module that generates the rust code for the binding
use configuration::*; use configuration::*;
use configuration_private::*; use configuration_private::*;
use std::io::{Result, Write}; use std::io::{Result, Write};