node-yara-rs

git clone git://archive.git.mtrnord.blog/MTRNord/node-yara-rs.git
Log | Files | Refs | README | LICENSE

commit 364c7e7f5affe2196633dd15f0f715a37ea9117f
parent 9b8130a537faba034a8ad5533070790cd3a1d31b
Author: MTRNord <mtrnord1@gmail.com>
Date:   Tue,  3 Oct 2023 21:09:46 +0200

Add towncrier and utilize Either types

Diffstat:
A.github/workflows/newsfile.yml | 21+++++++++++++++++++++
Achangelog.d/+either.feature | 2++
Achangelog.d/+towncrier.misc | 2++
Mindex.d.ts | 15+++------------
Apyproject.toml | 29+++++++++++++++++++++++++++++
Msrc/lib.rs | 93+++++++++++++++++++++++++++++++++----------------------------------------------
6 files changed, 96 insertions(+), 66 deletions(-)

diff --git a/.github/workflows/newsfile.yml b/.github/workflows/newsfile.yml @@ -0,0 +1,20 @@ +name: Newsfile + +on: + pull_request: + branches: [ main ] + merge_group: + +jobs: + changelog: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: # Needed for comparison + fetch-depth: 0 + - uses: actions/setup-python@v1 + with: + python-version: '3.9' + - run: pip install towncrier==21.9.0 + - name: ":newspaper: Newsfile" + run: python3 -m towncrier.check --compare-with=origin/main +\ No newline at end of file diff --git a/changelog.d/+either.feature b/changelog.d/+either.feature @@ -0,0 +1 @@ +Improve the arguments by utilizing the napi Either type +\ No newline at end of file diff --git a/changelog.d/+towncrier.misc b/changelog.d/+towncrier.misc @@ -0,0 +1 @@ +Add Towncrier for changelog managment +\ No newline at end of file diff --git a/index.d.ts b/index.d.ts @@ -10,14 +10,7 @@ export interface YaraRule { } export interface YaraVariable { id: string - /** Limitation of napi-rs which doesnt support any */ - integerValue?: number - /** Limitation of napi-rs which doesnt support any */ - floatValue?: number - /** Limitation of napi-rs which doesnt support any */ - boolValue?: boolean - /** Limitation of napi-rs which doesnt support any */ - stringValue?: string + value: number | number | boolean | string } export interface YaraRuleResult { /** Name of the rule. */ @@ -33,9 +26,7 @@ export interface YaraRuleResult { } export interface YaraRuleMetadata { identifier: string - integerValue?: number - stringValue?: string - boolValue?: boolean + value: number | string | boolean } export interface YaraString { /** Name of the string, with the '$'. */ @@ -116,5 +107,5 @@ export class YaraScanner { * @returns The results of yara scan_mem. */ scanProcess(pid: number): Array<YaraRuleResult> - defineVariable(identifier: string, stringValue?: string | undefined | null, integerValue?: number | undefined | null, floatValue?: number | undefined | null, boolValue?: boolean | undefined | null): void + defineVariable(identifier: string, value: string | number | number | boolean): void } diff --git a/pyproject.toml b/pyproject.toml @@ -0,0 +1,29 @@ +[tool.towncrier] +filename = "CHANGELOG.md" +directory = "changelog.d" +issue_format = "[\\#{issue}](https://github.com/MTRNord/node-yara-rs/issues/{issue})" + +[[tool.towncrier.type]] +directory = "feature" +name = "Features" +showcontent = true + +[[tool.towncrier.type]] +directory = "bugfix" +name = "Bugfixes" +showcontent = true + +[[tool.towncrier.type]] +directory = "doc" +name = "Improved Documentation" +showcontent = true + +[[tool.towncrier.type]] +directory = "removal" +name = "Deprecations and Removals" +showcontent = true + +[[tool.towncrier.type]] +directory = "misc" +name = "Internal Changes" +showcontent = true diff --git a/src/lib.rs b/src/lib.rs @@ -1,8 +1,8 @@ #![deny(clippy::all)] use napi::{ - anyhow::{anyhow, Context}, - bindgen_prelude::{Buffer, Reference, SharedReference}, + anyhow::Context, + bindgen_prelude::{Buffer, Either3, Either4, Reference, SharedReference}, Env, Result, }; use yara::{Compiler, MetadataValue, Rule as ExtYaraRule, Rules, Scanner}; @@ -22,14 +22,7 @@ pub struct YaraRule { #[derive(Debug)] pub struct YaraVariable { pub id: String, - /// Limitation of napi-rs which doesnt support any - pub integer_value: Option<i64>, - /// Limitation of napi-rs which doesnt support any - pub float_value: Option<f64>, - /// Limitation of napi-rs which doesnt support any - pub bool_value: Option<bool>, - /// Limitation of napi-rs which doesnt support any - pub string_value: Option<String>, + pub value: Either4<i64, f64, bool, String>, } /// An interface to use yara with node in a stable manner using Rust @@ -62,12 +55,10 @@ pub struct YaraRuleResult { } #[napi(object)] -#[derive(Debug, Default)] +#[derive(Debug)] pub struct YaraRuleMetadata { pub identifier: String, - pub integer_value: Option<i64>, - pub string_value: Option<String>, - pub bool_value: Option<bool>, + pub value: Either3<i64, String, bool>, } #[napi(object)] @@ -107,22 +98,27 @@ impl YaraCompiler { // Load variables for variable in variables { - if let Some(string_value) = variable.string_value { - compiler - .define_variable(&variable.id, string_value.as_str()) - .context(format!("Failed to set variable with id: {}", variable.id))?; - } else if let Some(bool_value) = variable.bool_value { - compiler - .define_variable(&variable.id, bool_value) - .context(format!("Failed to set variable with id: {}", variable.id))?; - } else if let Some(float_value) = variable.float_value { - compiler - .define_variable(&variable.id, float_value) - .context(format!("Failed to set variable with id: {}", variable.id))?; - } else if let Some(integer_value) = variable.integer_value { - compiler - .define_variable(&variable.id, integer_value) - .context(format!("Failed to set variable with id: {}", variable.id))?; + match variable.value { + Either4::A(integer_value) => { + compiler + .define_variable(&variable.id, integer_value) + .context(format!("Failed to set variable with id: {}", variable.id))?; + } + Either4::B(float_value) => { + compiler + .define_variable(&variable.id, float_value) + .context(format!("Failed to set variable with id: {}", variable.id))?; + } + Either4::C(bool_value) => { + compiler + .define_variable(&variable.id, bool_value) + .context(format!("Failed to set variable with id: {}", variable.id))?; + } + Either4::D(string_value) => { + compiler + .define_variable(&variable.id, string_value.as_str()) + .context(format!("Failed to set variable with id: {}", variable.id))?; + } } } @@ -189,18 +185,15 @@ impl YaraScanner { .map(|metadata| match metadata.value { MetadataValue::Integer(int) => YaraRuleMetadata { identifier: metadata.identifier.to_string(), - integer_value: Some(int), - ..Default::default() + value: Either3::A(int), }, MetadataValue::String(string) => YaraRuleMetadata { identifier: metadata.identifier.to_string(), - string_value: Some(string.to_string()), - ..Default::default() + value: Either3::B(string.to_string()), }, MetadataValue::Boolean(boolean) => YaraRuleMetadata { identifier: metadata.identifier.to_string(), - bool_value: Some(boolean), - ..Default::default() + value: Either3::C(boolean), }, }) .collect(), @@ -297,41 +290,33 @@ impl YaraScanner { pub fn define_variable( &mut self, identifier: String, - string_value: Option<String>, - integer_value: Option<i64>, - float_value: Option<f64>, - bool_value: Option<bool>, + value: Either4<String, i64, f64, bool>, ) -> Result<()> { - if let Some(string_value) = string_value { - Ok( + match value { + Either4::A(string_value) => Ok( self .scanner .define_variable(&identifier, string_value.as_str()) .context(format!("Failed to define string variable: {identifier}"))?, - ) - } else if let Some(bool_value) = bool_value { - Ok( + ), + Either4::B(bool_value) => Ok( self .scanner .define_variable(&identifier, bool_value) .context(format!("Failed to define bool variable: {identifier}"))?, - ) - } else if let Some(float_value) = float_value { - Ok( + ), + Either4::C(float_value) => Ok( self .scanner .define_variable(&identifier, float_value) .context(format!("Failed to define float variable: {identifier}"))?, - ) - } else if let Some(integer_value) = integer_value { - Ok( + ), + Either4::D(integer_value) => Ok( self .scanner .define_variable(&identifier, integer_value) .context(format!("Failed to define integer variable: {identifier}"))?, - ) - } else { - Err(anyhow!("You must at least define one of the value types!").into()) + ), } } }