8000 Give the fallback names of control types and the { name, type } objects one place each by dominion525 · Pull Request #105 · dominion525/jetdb · GitHub
[go: up one dir, main page]

Skip to content
Merged
8000
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions crates/jetdb-cli/src/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,7 @@ fn run_controls(args: &FormControlsArgs, password: Option<&str>) -> Result<(), S
.map_err(|e| e.to_string())?;

for (index, ctrl) in form_props.controls.iter().enumerate() {
let type_name = jetdb::control_type_name(ctrl.type_code)
.map(|s| s.to_string())
.unwrap_or_else(|| format!("0x{:04X}", ctrl.type_code));
let type_name = jetdb::control_type_label(ctrl.type_code);
println!("{}\t{}\t{}", ctrl.name, type_name, index);
}

Expand Down Expand Up @@ -285,9 +283,7 @@ fn run_props(args: &FormPropsArgs, password: Option<&str>) -> Result<(), String>

for ctrl in &form_props.controls {
println!();
let type_label = jetdb::control_type_name(ctrl.type_code)
.map(|s| s.to_string())
.unwrap_or_else(|| format!("0x{:04X}", ctrl.type_code));
let type_label = jetdb::control_type_label(ctrl.type_code);
println!(" Control: {} ({})", ctrl.name, type_label);
print_properties(&ctrl.properties);
}
Expand Down
30 changes: 12 additions & 18 deletions crates/jetdb-wasm/src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,12 +646,7 @@ impl JsDatabase {
let queries = self.inner.queries(include_system).map_err(to_js_error)?;
Ok(queries
.iter()
.map(|q| {
let object = Object::new();
set(&object, "name", q.name.as_str().into());
set(&object, "type", q.type_name.into());
object
})
.map(|q| name_and_type(&q.name, q.type_name))
.collect())
}

Expand All @@ -674,12 +669,7 @@ impl JsDatabase {
let modules = self.inner.modules().map_err(to_js_error)?;
Ok(modules
.iter()
.map(|m| {
let object = Object::new();
set(&object, "name", m.name.as_str().into());
set(&object, "type", m.type_name.into());
object
})
.map(|m| name_and_type(&m.name, m.type_name))
.collect())
}

Expand All @@ -702,12 +692,7 @@ impl JsDatabase {
let forms = self.inner.forms().map_err(to_js_error)?;
Ok(forms
.iter()
.map(|f| {
let object = Object::new();
set(&object, "name", f.name.as_str().into());
set(&object, "type", f.type_name.into());
object
})
.map(|f| name_and_type(&f.name, f.type_name))
.collect())
}

Expand Down Expand Up @@ -1350,6 +1335,15 @@ fn cell_value(cell: Cell) -> JsValue {
}

/// Sets a property on an object made here, which cannot fail.
/// An object `{ name, type }`, as `queries`, `modules` and `forms` give one
/// for each query, module, and form or report.
fn name_and_type(name: &str, type_name: &str) -> Object {
let object = Object::new();
set(&object, "name", name.into());
set(&object, "type", type_name.into());
object
}

fn set(object: &Object, key: &str, value: JsValue) {
Reflect::set(object, &JsValue::from_str(key), &value).expect("set a property");
}
Expand Down
27 changes: 6 additions & 21 deletions crates/jetdb-wasm/src/lib.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::io::Cursor;
use jetdb::ddl::{self, with_calculated_column_types, DdlDialect};
use jetdb::format::{index_flags, index_type, ColumnType, ObjectType};
use jetdb::{
calculated_column_types_in, control_type_name, data_macros_to_text, embedded_macro_to_text,
calculated_column_types_in, control_type_label, data_macros_to_text, embedded_macro_to_text,
entry_properties, find_table, list_forms_in, list_macros_in, query_to_sql, read_access_storage,
read_catalog, read_complex_column, read_data_macros_in, read_embedded_macros_in,
read_form_properties_in, read_form_stream_in, read_macro_in, read_object_properties,
Expand Down Expand Up @@ -257,7 +257,7 @@ pub fn number_position(number: f64) -> Option<RowPosition> {
#[derive(Debug, Clone, PartialEq)]
pub struct ControlEntry {
pub name: String,
/// The type name (see [`control_type`]).
/// The type name (see [`control_type_label`]).
pub type_name: String,
pub type_code: u16,
/// The place of the control in the list, from 0.
Expand All @@ -272,20 +272,14 @@ fn controls_of(design: jetdb::FormProperties) -> Vec<ControlEntry> {
.into_iter()
.zip(0..)
.map(|(c, index)| ControlEntry {
type_name: control_type(c.type_code),
type_name: control_type_label(c.type_code),
name: c.name,
type_code: c.type_code,
index,
})
.collect()
}

/// The name of the control type `code`, or the code as `0x....` for a type
/// jetdb does not know, as `jetdb form controls` shows it.
fn control_type(code: u16) -> String {
control_type_name(code).map_or_else(|| format!("0x{code:04X}"), str::to_string)
}

/// A value of a row, in the JavaScript type it becomes.
#[derive(Debug, Clone, PartialEq)]
pub enum Cell {
Expand Down Expand Up @@ -787,7 +781,7 @@ impl Database {
.into_iter()
.map(|c| Control {
name: c.name,
type_name: control_type(c.type_code),
type_name: control_type_label(c.type_code),
type_code: c.type_code,
properties: properties(c.properties),
})
Expand Down Expand Up @@ -1915,7 +1909,7 @@ mod tests {
.into_iter()
.zip(0..)
.map(|(c, index)| ControlEntry {
type_name: control_name_of_cli(c.type_code),
type_name: control_type_label(c.type_code),
name: c.name,
type_code: c.type_code,
index,
Expand All @@ -1932,13 +1926,6 @@ mod tests {
(got, expected)
}

/// The type name jetdb form controls prints for `code`.
fn control_name_of_cli(code: u16) -> String {
control_type_name(code)
.map(|s| s.to_string())
.unwrap_or_else(|| format!("0x{code:04X}"))
}

#[test]
fn form_controls_as_jetdb_form_controls_lists_them() {
// The controls of each form and report are those jetdb form controls
Expand Down Expand Up @@ -2075,9 +2062,7 @@ mod tests {
assert_eq!(design.controls.len(), read.controls.len(), "{name}");
for (control, read) in design.controls.iter().zip(&read.controls) {
assert_eq!(control.name, read.name);
let type_name = control_type_name(read.type_code)
.map_or_else(|| format!("0x{:04X}", read.type_code), str::to_string);
assert_eq!(control.type_name, type_name);
assert_eq!(control.type_name, control_type_label(read.type_code));
assert_eq!(control.type_code, read.type_code);
assert_eq!(names(&control.properties), read_names(&read.properties));
}
Expand Down
12 changes: 12 additions & 0 deletions crates/jetdb/src/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,12 @@ pub fn control_type_name(type_code: u16) -> Option<&'static str> {
}
}

/// The known control type name for `type_code`, or the code as `0x....` for
/// a type jetdb does not know, as `jetdb form controls` shows it.
pub fn control_type_label(type_code: u16) -> String {
control_type_name(type_code).map_or_else(|| format!("0x{type_code:04X}"), str::to_string)
}

impl BlobProperty {
/// Return the known property name, or `None` for unknown IDs: a text
/// value is named by [`prop_id_name`], an integer or Yes/No value by
Expand Down Expand Up @@ -2218,6 +2224,12 @@ mod tests {
}
}

#[test]
fn control_type_label_names_or_numbers() {
assert_eq!(control_type_label(0x126D), "TextBox");
assert_eq!(control_type_label(0x0099), "0x0099");
}

#[test]
fn control_type_name_unknown_returns_none() {
assert_eq!(control_type_name(0x0000), None);
Expand Down
10 changes: 5 additions & 5 deletions crates/jetdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ pub use data::{
read_table_rows_with, ReadResult, RowPosition, TableRow, TableRows, Value,
};
pub use form::{
control_type_name, list_forms, list_forms_in, read_form_properties, read_form_properties_in,
read_form_properties_of_type, read_form_stream, read_form_stream_in, read_form_stream_of_type,
read_form_type_info, read_form_type_info_in, read_form_type_info_of_type, BlobProperty,
BlobValue, ControlInfo, ControlProperties, FormEntry, FormObjectType, FormProperties,
FormStream, FormTypeInfo, StreamKind,
control_type_label, control_type_name, list_forms, list_forms_in, read_form_properties,
read_form_properties_in, read_form_properties_of_type, read_form_stream, read_form_stream_in,
read_form_stream_of_type, read_form_type_info, read_form_type_info_in,
read_form_type_info_of_type, BlobProperty, BlobValue, ControlInfo, ControlProperties,
FormEntry, FormObjectType, FormProperties, FormStream, FormTypeInfo, StreamKind,
};
pub use macro_action::{macro_action, macro_argument_value_name, MacroAction, MACRO_ACTIONS};
pub use macro_def::{
Expand Down
Loading
0