Use snake_case instead of lowercase
parent
a655e908a6
commit
630bf4b858
|
@ -4,7 +4,7 @@ use testinterface::*;
|
|||
|
||||
pub struct Test {
|
||||
emit: TestEmitter,
|
||||
username: String,
|
||||
user_name: String,
|
||||
age: c_int,
|
||||
active: bool,
|
||||
misc: Variant,
|
||||
|
@ -15,7 +15,7 @@ impl TestTrait for Test {
|
|||
fn create(emit: TestEmitter) -> Test {
|
||||
Test {
|
||||
emit: emit,
|
||||
username: String::new(),
|
||||
user_name: String::new(),
|
||||
age: 0,
|
||||
active: true,
|
||||
misc: Variant::None,
|
||||
|
@ -25,12 +25,12 @@ impl TestTrait for Test {
|
|||
fn emit(&self) -> &TestEmitter {
|
||||
&self.emit
|
||||
}
|
||||
fn get_username(&self) -> String {
|
||||
self.username.clone()
|
||||
fn get_user_name(&self) -> String {
|
||||
self.user_name.clone()
|
||||
}
|
||||
fn set_username(&mut self, value: String) {
|
||||
self.username = value;
|
||||
self.emit.username_changed();
|
||||
fn set_user_name(&mut self, value: String) {
|
||||
self.user_name = value;
|
||||
self.emit.user_name_changed();
|
||||
}
|
||||
fn get_age(&self) -> c_int {
|
||||
self.age
|
||||
|
@ -80,16 +80,16 @@ impl DirectoryTrait for Directory {
|
|||
fn row_count(&self) -> c_int {
|
||||
0
|
||||
}
|
||||
fn fileicon(&self, row: c_int) -> Variant {
|
||||
fn file_icon(&self, row: c_int) -> Variant {
|
||||
Variant::Bool(row > 0)
|
||||
}
|
||||
fn filepath(&self, row: c_int) -> Variant {
|
||||
fn file_path(&self, row: c_int) -> Variant {
|
||||
Variant::Bool(row > 0)
|
||||
}
|
||||
fn filename(&self, row: c_int) -> Variant {
|
||||
fn file_name(&self, row: c_int) -> Variant {
|
||||
Variant::Bool(row > 0)
|
||||
}
|
||||
fn filepermissions(&self, row: c_int) -> Variant {
|
||||
fn file_permissions(&self, row: c_int) -> Variant {
|
||||
Variant::Bool(row > 0)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -115,6 +115,11 @@ QString upperInitial(const QString& name) {
|
|||
return name.left(1).toUpper() + name.mid(1);
|
||||
}
|
||||
|
||||
QString snakeCase(const QString& name) {
|
||||
return name.left(1).toLower() + name.mid(1)
|
||||
.replace(QRegExp("([A-Z])"), "_\\1").toLower();
|
||||
}
|
||||
|
||||
QString writeProperty(const QString& name) {
|
||||
return "WRITE set" + upperInitial(name) + " ";
|
||||
}
|
||||
|
@ -157,7 +162,7 @@ signals:
|
|||
}
|
||||
|
||||
void writeCppListModel(QTextStream& cpp, const Object& o) {
|
||||
const QString lcname(o.name.toLower());
|
||||
const QString lcname(snakeCase(o.name));
|
||||
|
||||
cpp << "enum " << o.name << "Role {\n";
|
||||
for (auto role: o.roles) {
|
||||
|
@ -168,7 +173,7 @@ void writeCppListModel(QTextStream& cpp, const Object& o) {
|
|||
cpp << "extern \"C\" {\n";
|
||||
for (auto role: o.roles) {
|
||||
cpp << QString(" void %2_data_%3(%1Interface*, int, QVariant*, qvariant_set);\n")
|
||||
.arg(o.name, lcname, role.name.toLower());
|
||||
.arg(o.name, lcname, snakeCase(role.name));
|
||||
}
|
||||
cpp << QString(R"(
|
||||
int %2_row_count(%1Interface*, qmodelindex_t parent);
|
||||
|
@ -205,7 +210,7 @@ QVariant %1::data(const QModelIndex &index, int role) const
|
|||
for (auto role: o.roles) {
|
||||
cpp << QString(" case %1Role%2:\n").arg(o.name, role.name);
|
||||
cpp << QString(" %1_data_%2(d, index.row(), &v, set_qvariant);\n")
|
||||
.arg(lcname, role.name.toLower());
|
||||
.arg(lcname, snakeCase(role.name));
|
||||
cpp << " break;\n";
|
||||
}
|
||||
cpp << " }\n return v;\n}\n";
|
||||
|
@ -249,7 +254,7 @@ class %1 : public %3
|
|||
}
|
||||
|
||||
void writeObjectCDecl(QTextStream& cpp, const Object& o) {
|
||||
const QString lcname(o.name.toLower());
|
||||
const QString lcname(snakeCase(o.name));
|
||||
cpp << QString(" %1Interface* %2_new(%1*").arg(o.name, lcname);
|
||||
for (const Property& p: o.properties) {
|
||||
cpp << QString(", void (*)(%1*)").arg(o.name);
|
||||
|
@ -265,7 +270,7 @@ void writeObjectCDecl(QTextStream& cpp, const Object& o) {
|
|||
cpp << QString(" void %2_free(%1Interface*);").arg(o.name, lcname)
|
||||
<< endl;
|
||||
for (const Property& p: o.properties) {
|
||||
const QString base = QString("%1_%2").arg(lcname, p.name.toLower());
|
||||
const QString base = QString("%1_%2").arg(lcname, snakeCase(p.name));
|
||||
if (p.type.startsWith("Q")) {
|
||||
cpp << QString(" void %2_get(%1Interface*, %3);")
|
||||
.arg(o.name, base, cGetType(p)) << endl;
|
||||
|
@ -281,7 +286,7 @@ void writeObjectCDecl(QTextStream& cpp, const Object& o) {
|
|||
}
|
||||
|
||||
void writeCppObject(QTextStream& cpp, const Object& o) {
|
||||
const QString lcname(o.name.toLower());
|
||||
const QString lcname(snakeCase(o.name));
|
||||
cpp << QString("%1::%1(QObject *parent):\n %2(parent),")
|
||||
.arg(o.name, baseType(o)) << endl;
|
||||
cpp << QString(" d(%1_new(this").arg(lcname);
|
||||
|
@ -313,7 +318,7 @@ void writeCppObject(QTextStream& cpp, const Object& o) {
|
|||
)").arg(o.name, lcname);
|
||||
|
||||
for (const Property& p: o.properties) {
|
||||
const QString base = QString("%1_%2").arg(lcname, p.name.toLower());
|
||||
const QString base = QString("%1_%2").arg(lcname, snakeCase(p.name));
|
||||
cpp << QString("%3 %1::%2() const\n{\n").arg(o.name, p.name, p.type);
|
||||
if (p.type.startsWith("Q")) {
|
||||
cpp << " " << p.type << " v;\n";
|
||||
|
@ -525,7 +530,7 @@ QString rustTypeInit(const Property& p) {
|
|||
}
|
||||
|
||||
void writeRustInterfaceObject(QTextStream& r, const Object& o) {
|
||||
const QString lcname(o.name.toLower());
|
||||
const QString lcname(snakeCase(o.name));
|
||||
r << QString(R"(/* generated by rust_qt_binding_generator */
|
||||
pub struct %1QObject {}
|
||||
|
||||
|
@ -535,7 +540,7 @@ pub struct %1Emitter {
|
|||
)").arg(o.name);
|
||||
for (const Property& p: o.properties) {
|
||||
r << QString(" %2_changed: fn(*const %1QObject),\n")
|
||||
.arg(o.name, p.name.toLower());
|
||||
.arg(o.name, snakeCase(p.name));
|
||||
}
|
||||
r << QString(R"(}
|
||||
|
||||
|
@ -553,7 +558,7 @@ impl %1Emitter {
|
|||
(self.%1_changed)(ptr);
|
||||
}
|
||||
}
|
||||
)").arg(p.name.toLower());
|
||||
)").arg(snakeCase(p.name));
|
||||
}
|
||||
|
||||
QString modelStruct = "";
|
||||
|
@ -592,7 +597,7 @@ pub trait %1Trait {
|
|||
fn emit(&self) -> &%1Emitter;
|
||||
)").arg(o.name, modelStruct);
|
||||
for (const Property& p: o.properties) {
|
||||
const QString lc(p.name.toLower());
|
||||
const QString lc(snakeCase(p.name));
|
||||
r << QString(" fn get_%1(&self) -> %2;\n").arg(lc, rustType(p));
|
||||
if (p.write) {
|
||||
r << QString(" fn set_%1(&mut self, value: %2);\n").arg(lc, rustType(p));
|
||||
|
@ -602,7 +607,7 @@ pub trait %1Trait {
|
|||
r << " fn row_count(&self) -> c_int;\n";
|
||||
for (auto role: o.roles) {
|
||||
r << QString(" fn %1(&self, row: c_int) -> Variant;\n")
|
||||
.arg(role.name.toLower());
|
||||
.arg(snakeCase(role.name));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -612,7 +617,7 @@ pub trait %1Trait {
|
|||
pub extern "C" fn %2_new(qobject: *const %1QObject)").arg(o.name, lcname);
|
||||
for (const Property& p: o.properties) {
|
||||
r << QString(",\n %2_changed: fn(*const %1QObject)")
|
||||
.arg(o.name, p.name.toLower());
|
||||
.arg(o.name, snakeCase(p.name));
|
||||
}
|
||||
if (o.type == ObjectTypeList) {
|
||||
r << QString(R"(,
|
||||
|
@ -631,7 +636,7 @@ pub extern "C" fn %2_new(qobject: *const %1QObject)").arg(o.name, lcname);
|
|||
qobject: Arc::new(Mutex::new(qobject)),
|
||||
)").arg(o.name);
|
||||
for (const Property& p: o.properties) {
|
||||
r << QString(" %1_changed: %1_changed,\n").arg(p.name.toLower());
|
||||
r << QString(" %1_changed: %1_changed,\n").arg(snakeCase(p.name));
|
||||
}
|
||||
QString model = "";
|
||||
if (o.type == ObjectTypeList) {
|
||||
|
@ -656,7 +661,7 @@ pub unsafe extern "C" fn %2_free(ptr: *mut %1) {
|
|||
}
|
||||
)").arg(o.name, lcname, model);
|
||||
for (const Property& p: o.properties) {
|
||||
const QString base = QString("%1_%2").arg(lcname, p.name.toLower());
|
||||
const QString base = QString("%1_%2").arg(lcname, snakeCase(p.name));
|
||||
QString ret = ") -> " + rustType(p);
|
||||
if (p.type.startsWith("Q")) {
|
||||
r << QString(R"(
|
||||
|
@ -667,7 +672,7 @@ pub unsafe extern "C" fn %2_get(ptr: *const %1,
|
|||
let data = (&*ptr).get_%3();
|
||||
set(p, %4::from(&data));
|
||||
}
|
||||
)").arg(o.name, base, p.name.toLower(), p.type);
|
||||
)").arg(o.name, base, snakeCase(p.name), p.type);
|
||||
if (p.write) {
|
||||
const QString type = p.type == "QString" ? "QStringIn" : p.type;
|
||||
r << QString(R"(
|
||||
|
@ -675,7 +680,7 @@ pub unsafe extern "C" fn %2_get(ptr: *const %1,
|
|||
pub unsafe extern "C" fn %2_set(ptr: *mut %1, v: %4) {
|
||||
(&mut *ptr).set_%3(v.convert());
|
||||
}
|
||||
)").arg(o.name, base, p.name.toLower(), type);
|
||||
)").arg(o.name, base, snakeCase(p.name), type);
|
||||
}
|
||||
} else {
|
||||
r << QString(R"(
|
||||
|
@ -683,14 +688,14 @@ pub unsafe extern "C" fn %2_set(ptr: *mut %1, v: %4) {
|
|||
pub unsafe extern "C" fn %2_get(ptr: *const %1) -> %4 {
|
||||
(&*ptr).get_%3()
|
||||
}
|
||||
)").arg(o.name, base, p.name.toLower(), rustType(p));
|
||||
)").arg(o.name, base, snakeCase(p.name), rustType(p));
|
||||
if (p.write) {
|
||||
r << QString(R"(
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn %2_set(ptr: *mut %1, v: %4) {
|
||||
(&mut *ptr).set_%3(v);
|
||||
}
|
||||
)").arg(o.name, base, p.name.toLower(), rustType(p));
|
||||
)").arg(o.name, base, snakeCase(p.name), rustType(p));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -711,7 +716,7 @@ pub unsafe extern "C" fn %2_data_%3(ptr: *const %1,
|
|||
let data = (& *ptr).%3(row);
|
||||
set(d, &QVariant::from(&data));
|
||||
}
|
||||
)").arg(o.name, lcname, role.name.toLower());
|
||||
)").arg(o.name, lcname, snakeCase(role.name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -748,7 +753,7 @@ use %1::*;
|
|||
}
|
||||
|
||||
void writeRustImplementationObject(QTextStream& r, const Object& o) {
|
||||
const QString lcname(o.name.toLower());
|
||||
const QString lcname(snakeCase(o.name));
|
||||
QString modelStruct = "";
|
||||
r << QString("pub struct %1 {\n emit: %1Emitter,\n").arg((o.name));
|
||||
if (o.type == ObjectTypeList) {
|
||||
|
@ -756,7 +761,7 @@ void writeRustImplementationObject(QTextStream& r, const Object& o) {
|
|||
r << QString(" model: %1List,\n").arg(o.name);
|
||||
}
|
||||
for (const Property& p: o.properties) {
|
||||
const QString lc(p.name.toLower());
|
||||
const QString lc(snakeCase(p.name));
|
||||
r << QString(" %1: %2,\n").arg(lc, rustType(p));
|
||||
}
|
||||
r << "}\n\n";
|
||||
|
@ -769,7 +774,7 @@ void writeRustImplementationObject(QTextStream& r, const Object& o) {
|
|||
r << QString(" model: model,\n");
|
||||
}
|
||||
for (const Property& p: o.properties) {
|
||||
const QString lc(p.name.toLower());
|
||||
const QString lc(snakeCase(p.name));
|
||||
r << QString(" %1: %2,\n").arg(lc, rustTypeInit(p));
|
||||
}
|
||||
r << QString(R"( }
|
||||
|
@ -779,7 +784,7 @@ void writeRustImplementationObject(QTextStream& r, const Object& o) {
|
|||
}
|
||||
)").arg(o.name);
|
||||
for (const Property& p: o.properties) {
|
||||
const QString lc(p.name.toLower());
|
||||
const QString lc(snakeCase(p.name));
|
||||
r << QString(" fn get_%1(&self) -> %2 {\n").arg(lc, rustType(p));
|
||||
if (p.type.startsWith("Q")) {
|
||||
r << QString(" self.%1.clone()\n").arg(lc);
|
||||
|
@ -799,7 +804,7 @@ void writeRustImplementationObject(QTextStream& r, const Object& o) {
|
|||
r << " fn row_count(&self) -> c_int {\n 0\n }\n";
|
||||
for (auto role: o.roles) {
|
||||
r << QString(" fn %1(&self, row: c_int) -> Variant {\n")
|
||||
.arg(role.name.toLower());
|
||||
.arg(snakeCase(role.name));
|
||||
r << " Variant::Bool(row > 0)\n";
|
||||
r << " }\n";
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue