Skip to content

Commit fe00882

Browse files
committed
docs: be explicit about the limitations of using JSON values in define_global.
Also, make the error message for `VariableError::InvalidIdentifier` more generic.
1 parent 946665d commit fe00882

5 files changed

Lines changed: 18 additions & 6 deletions

File tree

capi/include/yara_x.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,9 @@ enum YRX_RESULT yrx_compiler_define_global_float(struct YRX_COMPILER *compiler,
452452
// (e.g., booleans, integers, strings), prefer dedicated functions to avoid
453453
// the overhead of JSON deserialization.
454454
//
455-
// When defining a map, keys must be of string type, and values can be
456-
// any of the types supported by YARA, including other maps. Arrays must be
457-
// homogeneous (all elements must be the same type).
455+
// When defining a map, keys must be of string type and valid YARA identifiers,
456+
// and values can be any of the types supported by YARA, including other maps.
457+
// Arrays must be homogeneous (all elements must be the same type).
458458
enum YRX_RESULT yrx_compiler_define_global_json(struct YRX_COMPILER *compiler,
459459
const char *ident,
460460
const char *value);

lib/src/compiler/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,12 @@ impl<'a> Compiler<'a> {
646646
/// `i64`, `i32`, `i16`, `i8`, `u32`, `u16`, `u8`, `f64`, `f32`, `bool`,
647647
/// `&str`, `String` and [`serde_json::Value`].
648648
///
649+
/// When using a [`serde_json::Value`] there are certain limitations: keys
650+
/// in maps must be valid YARA identifiers (the first character must be `_`
651+
/// or a letter, the remaining ones must be `_`, a letter or a digit),
652+
/// because these maps are translated into YARA structures. Also, all items
653+
/// in an array must have the same type.
654+
///
649655
/// ```
650656
/// # use yara_x::Compiler;
651657
/// assert!(Compiler::new()

lib/src/variables.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub enum VariableError {
3737

3838
/// The identifier is not valid. Identifiers can only contain alphanumeric
3939
/// characters and underscores, and can't start with a digit.
40-
#[error("invalid variable identifier `{0}`")]
40+
#[error("invalid identifier `{0}`")]
4141
InvalidIdentifier(String),
4242

4343
/// The value of a variable cannot be null. This may happen when using a

py/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,10 @@ impl Compiler {
497497
/// each scanner can change the variable's value by calling
498498
/// [`crate::Scanner::set_global`].
499499
///
500-
/// The type of `value` must be: bool, str, bytes, int or float.
500+
/// The type of `value` must be: bool, str, bytes, int, float or dict. When
501+
/// the value is a dict, keys must be of type string and valid YARA identifiers
502+
/// (the first character must be `_` or a letter, and the remaining ones must
503+
/// be a `_`, a letter or a digit).
501504
///
502505
/// # Raises
503506
///

py/yara_x.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ class Compiler:
9696
each scanner can change the variable's value by calling
9797
[`crate::Scanner::set_global`].
9898
99-
The type of `value` must be: bool, str, bytes, int or float.
99+
The type of `value` must be: bool, str, bytes, int, float or dict. When
100+
the value is a dict, keys must be of type string and valid YARA identifiers
101+
(the first character must be `_` or a letter, and the remaining ones must
102+
be a `_`, a letter or a digit).
100103
101104
# Raises
102105

0 commit comments

Comments
 (0)