From f14c8a2aeab18a9bfa380df28f94736580e08f48 Mon Sep 17 00:00:00 2001
From: JSDurand <mmemmew@gmail.com>
Date: Wed, 9 Aug 2023 11:42:03 +0800
Subject: Fix a bug of using incorrect forest nodes to plant.

Previously some incorrect forest nodes will be used for planting new
nodes.  I cannot fix the root cause of their presence in the
chain-rule machine.  But I can ignore them when they are encountered.

Of course I would like to really prevent them from existing, but still
cannot figure out how.
---
 src/lib.rs | 56 ++++++++++++++++++--------------------------------------
 1 file changed, 18 insertions(+), 38 deletions(-)

(limited to 'src')

diff --git a/src/lib.rs b/src/lib.rs
index aed8536..3483847 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -556,23 +556,15 @@ extern "C" fn parser_parse(
 fn read_label(label: *mut std::os::raw::c_uchar) -> ForestLabel<GrammarLabel> {
     let status: u8 = unsafe { *label };
 
-    let label_status: ForestLabelType;
-
-    match status {
-        0 => {
-            label_status = ForestLabelType::Plain;
-        }
-        1 => {
-            label_status = ForestLabelType::Packed;
-        }
-        _ => {
-            label_status = ForestLabelType::Cloned(usize::from_be_bytes(
-                unsafe { std::slice::from_raw_parts(label.add(1), 8) }
-                    .try_into()
-                    .unwrap(),
-            ));
-        }
-    }
+    let label_status: ForestLabelType = match status {
+        0 => ForestLabelType::Plain,
+        1 => ForestLabelType::Packed,
+        _ => ForestLabelType::Cloned(usize::from_be_bytes(
+            unsafe { std::slice::from_raw_parts(label.add(1), 8) }
+                .try_into()
+                .unwrap(),
+        )),
+    };
 
     let start = usize::from_be_bytes(
         unsafe { std::slice::from_raw_parts(label.add(9), 8) }
@@ -594,19 +586,11 @@ fn read_label(label: *mut std::os::raw::c_uchar) -> ForestLabel<GrammarLabel> {
             .unwrap(),
     );
 
-    let inner_label: GrammarLabel;
-
-    match discriminant {
-        0 => {
-            inner_label = GrammarLabel::new_closed(TNT::Ter(content), start, end);
-        }
-        1 => {
-            inner_label = GrammarLabel::new_closed(TNT::Non(content), start, end);
-        }
-        _ => {
-            inner_label = GrammarLabel::new_closed(content, start, end);
-        }
-    }
+    let inner_label: GrammarLabel = match discriminant {
+        0 => GrammarLabel::new_closed(TNT::Ter(content), start, end),
+        1 => GrammarLabel::new_closed(TNT::Non(content), start, end),
+        _ => GrammarLabel::new_closed(content, start, end),
+    };
 
     ForestLabel::new(inner_label, label_status)
 }
@@ -667,7 +651,7 @@ extern "C" fn print_forest(
 
     let special_marks = unsafe { std::slice::from_raw_parts((*forest_vec).data.add(8), 3) };
 
-    if special_marks != &[114, 101, 112] {
+    if special_marks != [114, 101, 112] {
         return_error!(
             format!(
                 "the forest does not begin with the special mark\nThe first bytes are: \
@@ -785,16 +769,12 @@ extern "C" fn print_forest(
 
     let result = format!("{preamble}{post}");
 
-    let parsed_filename;
-
-    match unsafe { std::ffi::CStr::from_ptr(filename).to_str() } {
-        Ok(ccstr) => {
-            parsed_filename = ccstr;
-        }
+    let parsed_filename = match unsafe { std::ffi::CStr::from_ptr(filename).to_str() } {
+        Ok(ccstr) => ccstr,
         Err(e) => {
             return_error!(format!("error: {e}"), error_len, error_cap, error_vec);
         }
-    }
+    };
 
     if std::fs::metadata(parsed_filename).is_ok() {
         let _ = std::fs::remove_file(parsed_filename);
-- 
cgit v1.2.3-18-g5258