1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
//! Hello fellow Rustacians! RustLogic is a crate for parsing and handling simple logical expressings.
//!
//! This crate contains the basic foundation for handling stringed logical formulas.
//! If you want to want to submit an issue or a pull request,
//! please visit the [GitHub](https://github.com/coastalwhite/rustlogic) page.
//!
//! Thanks for your time and enjoy this crate!
//!
//! # Examples
//! ## 4-1 Multiplexer
//! ```
//! # use std::collections::HashMap;
//! let multiplexer_4_to_1 =
//!     rustlogic::parse(
//!         "([a]&~[x]&~[y])|([b]&~[x]&[y])|([c]&[x]&~[y])|([d]&[x]&[y])"
//!     )
//!     .expect("Failed to parse 4-1 multiplexer");
//!
//! let mut variable_map = HashMap::new();
//!
//! // Input: 1001
//! variable_map.insert("a", true);
//! variable_map.insert("b", false);
//! variable_map.insert("c", false);
//! variable_map.insert("d", true);
//!
//! // Selector: 11
//! variable_map.insert("x", true);
//! variable_map.insert("y", true);
//!
//! // Should select fourth item from bus so true
//! let value = multiplexer_4_to_1.get_value_from_variables(&variable_map).unwrap();
//! println!("{}", value); // Will print true!
//! # assert!(value);
//! # variable_map.insert("d", false);
//! # let value = multiplexer_4_to_1.get_value_from_variables(&variable_map).unwrap();
//! # assert!(!value);
//! ```
//!
//! ## Evaluating a logical string with variables and custom logical operators
//! ```
//! use rustlogic::operators;
//! use std::collections::HashMap;
//!
//! // Define the set
//! let operator_set = operators::common_sets::worded();
//!
//! let parsed = rustlogic::custom_parse("(NOT $[A] AND TRUE) OR $[B]", &operator_set);
//! # assert!(parsed.is_ok());
//!
//! // We assign the variables to their values
//! let mut hm = HashMap::new();
//! hm.insert("A", false);
//! hm.insert("B", false);
//! # assert!(parsed.unwrap().get_value_from_variables(&hm).is_ok());
//!
//! // Now contains the value of the logical expression
//! # let parsed = rustlogic::custom_parse("(NOT $[A] AND TRUE) OR $[B]", &operator_set);
//! let value = parsed.unwrap().get_value_from_variables(&hm).unwrap();
//! # assert!(value);
//! ```

mod multidimensional_logicnode;
pub mod operators;
mod util;

use operators::OperatorSet;
use std::collections::HashMap;

/// An Enum of all the possible states of a head of a logical formula
#[derive(Debug, Clone)]
pub enum LogicNode {
    /// AND operation of left and right
    And(Box<LogicNode>, Box<LogicNode>),

    /// OR operation of left and right
    Or(Box<LogicNode>, Box<LogicNode>),

    /// NOT operation of child
    Not(Box<LogicNode>),

    /// True, always returns true
    True,

    /// False, always returns false
    False,

    /// Variable, always returns value of variable passed in
    Variable(String),
}

impl LogicNode {
    /// Will retrieve the value of a [LogicNode](enum.LogicNode.html)
    /// given a [HashMap](https://doc.rust-lang.org/std/collections/struct.HashMap.html) of variables
    ///
    /// # Output
    /// Will output a result type with either the value of the [LogicNode](enum.LogicNode.html)
    /// given the [Hashmap](https://doc.rust-lang.org/std/collections/struct.HashMap.html)
    /// or will give a error with the variable missing from the 'variables' argument given.
    ///
    /// # Examples
    /// ## X-OR
    /// ```
    /// # use std::collections::HashMap;
    /// # use rustlogic::parse;
    /// let xor = parse("([a]|[b])&~([a]&[b])")
    ///     .expect("Failed to parse xor gate");
    ///
    /// let mut variable_map = HashMap::new();
    /// variable_map.insert("a", true);
    /// variable_map.insert("b", true);
    /// let value_1 = xor.get_value_from_variables(&variable_map).unwrap();
    /// println!("First value: {}!", value_1); // Will print false!
    /// # assert!(!value_1);
    ///
    /// variable_map.insert("a", false);
    /// let value_2 = xor.get_value_from_variables(&variable_map).unwrap();
    /// println!("Second value: {}!", value_1); // Will print true!
    /// # assert!(value_2);
    ///
    /// variable_map.insert("b", false);
    /// let value_3 = xor.get_value_from_variables(&variable_map).unwrap();
    /// println!("Third value: {}!", value_1); // Will print false!
    /// # assert!(!value_3);
    /// ```
    pub fn get_value_from_variables(
        &self,
        variables: &HashMap<&str, bool>,
    ) -> Result<bool, String> {
        use LogicNode::*;

        match self {
            And(left, right) => Ok(left.get_value_from_variables(variables)?
                && right.get_value_from_variables(variables)?),

            Or(left, right) => Ok(left.get_value_from_variables(variables)?
                || right.get_value_from_variables(variables)?),

            Not(child) => Ok(!child.get_value_from_variables(variables)?),

            True => Ok(true),

            False => Ok(false),

            // Fetch variable, if it cannot be found through error with name of that variable
            Variable(variable) => Ok(variables
                .get(&variable[..])
                .ok_or(variable.clone())?
                .clone()),
        }
    }

    /// Will retrieve the value of a [LogicNode](enum.LogicNode.html) given purely the formula
    ///
    /// # Output
    /// Will return a result with either the value of the [LogicNode](enum.LogicNode.html) or an error value containing
    /// the name of the (or one of the) variable contained within the [LogicNode](enum.LogicNode.html).
    ///
    /// # Examples
    /// ## Basic usage
    /// ```
    /// # use std::collections::HashMap;
    /// # use rustlogic::parse;
    /// let and_of_true_and_false = parse("0&1")
    ///     .expect("Unable to parse AND of true and false");
    /// let or_of_true_and_false = parse("0|1")
    ///     .expect("Unable to parse OR of true and false");
    ///
    /// let value_1 = and_of_true_and_false.get_value().unwrap();
    /// println!("0&1: {}", value_1); // Will return false!
    /// # assert!(!value_1);
    ///
    /// let value_2 = or_of_true_and_false.get_value().unwrap();
    /// println!("0|1: {}", value_2); // Will return true!
    /// # assert!(value_2);
    /// ```
    pub fn get_value(&self) -> Result<bool, String> {
        use LogicNode::*;

        match self {
            And(left, right) => Ok(left.get_value()? && right.get_value()?),

            Or(left, right) => Ok(left.get_value()? || right.get_value()?),

            Not(child) => Ok(!child.get_value()?),

            True => Ok(true),

            False => Ok(false),

            // Tree contains variable so return error
            Variable(variable) => Err(variable.clone()),
        }
    }

    /// Will return whether a given [LogicNode](enum.LogicNode.html) contains a variable
    ///
    /// # Examples
    /// ## Basic usage
    /// ```
    /// # use std::collections::HashMap;
    /// # use rustlogic::parse;
    /// let with_variable = parse("([a]&0)")
    ///     .expect("Unable to parse with variable");
    /// let without_variable = parse("(1&0)")
    ///     .expect("Unable to parse without variable");
    ///
    /// // Will return true!
    /// println!("First contains variable: {}", with_variable.contains_variable());
    /// # assert!(with_variable.contains_variable());
    ///
    /// // Will return false!
    /// println!("Second contains variable: {}", without_variable.contains_variable());
    /// # assert!(!without_variable.contains_variable());
    /// ```
    pub fn contains_variable(&self) -> bool {
        use LogicNode::*;

        match self {
            And(left, right) | Or(left, right) => {
                left.contains_variable() || right.contains_variable()
            }

            Not(child) => child.contains_variable(),

            True | False => false,

            Variable(_) => true,
        }
    }

    /// Returns a sorted vector of all Variables used in a LogicNode
    ///
    /// # Examples
    /// ## NOR
    /// ```
    /// # use std::collections::HashMap;
    /// # use rustlogic::parse;
    /// let nor = parse("~([a]|[b])")
    ///     .expect("Unable to parse with variable");
    ///
    /// // Will return ["a", "b"]
    /// println!("Variables in nor: {:?}", nor.get_variables());
    /// # assert_eq!(nor.get_variables(), vec!["a", "b"]);
    /// ```
    pub fn get_variables(&self) -> Vec<String> {
        let mut variables = Vec::new();

        use LogicNode::*;

        // Go through elements recurvely
        match self {
            And(left, right) | Or(left, right) => {
                variables.extend(left.get_variables().iter().cloned());
                variables.extend(right.get_variables().iter().cloned());
            }

            Not(child) => variables.extend(child.get_variables().iter().cloned()),

            True | False => (),

            Variable(var) => variables = vec![var.clone()],
        }

        // Sort the variables in preparation for the deduplication
        variables.sort();

        // Remove duplicates
        variables.dedup();

        variables
    }

    /// Inserts formula in place of variable
    ///
    /// # Examples
    /// ## Insert AND in OR
    /// ```rust
    /// # use std::collections::HashMap;
    /// # use rustlogic::parse;
    /// let or = parse("[AND]|[b]").expect("Error parsing or");
    ///
    /// # assert_eq!(or.to_string(), "( [AND] | [b] )");
    /// let and_in_or = or.insert_formula(
    ///     "AND",
    ///     &parse("[x]&[y]").expect("Error parsing AND")
    /// );
    ///
    /// println!("{}", and_in_or); // Will print ( ( [x] & [y] ) | [b] )
    /// # assert_eq!(and_in_or.to_string(), "( ( [x] & [y] ) | [b] )");
    pub fn insert_formula(&self, variable: &str, formula: &LogicNode) -> LogicNode {
        use LogicNode::*;

        match self {
            And(left, right) => And(
                Box::new(left.insert_formula(variable, formula)),
                Box::new(right.insert_formula(variable, formula)),
            ),

            Or(left, right) => Or(
                Box::new(left.insert_formula(variable, formula)),
                Box::new(right.insert_formula(variable, formula)),
            ),

            Not(child) => Not(Box::new(child.insert_formula(variable, formula))),

            Variable(var) => {
                if var == variable {
                    return formula.clone();
                }

                Variable(var.clone())
            }

            True => True,
            False => False,
        }
    }

    /// Convert a Logical node to a String using a certain Operator Set
    ///
    /// # Example
    /// ```
    /// use rustlogic::operators;
    ///
    /// // Create our logical node
    /// let parsed = rustlogic::parse("(~[A]&[B])|[C]").unwrap();
    ///
    /// // Create a string using the worded operator set
    /// let worded = parsed.to_string_using_set(&operators::common_sets::worded());
    ///
    /// // Will print "((NOT $[A] AND $[B]) OR $[C])"!
    /// println!("{}", worded);
    /// assert_eq!(worded, String::from("((NOT $[A] AND $[B]) OR $[C])"));
    /// ```
    pub fn to_string_using_set(&self, operator_set: &OperatorSet) -> String {
        multidimensional_logicnode::MultiDimensionalLogicNode::new(self)
            .to_string_using_set(operator_set)
    }
}

impl std::fmt::Display for LogicNode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let multi_dim = multidimensional_logicnode::MultiDimensionalLogicNode::new(self);
        write!(f, "{}", multi_dim)
    }
}

/// Function used to find the matching parenthesis given a string
/// and a position open a opening group symbol (taking into account depth)
/// Will return None if position given is not a bracket or
/// if no matching bracket was found
fn get_group_content(
    input_string: &str,
    position: usize,
    operator_set: &OperatorSet,
) -> Option<String> {
    use util::{multi_search, search};

    let opener = operator_set.group_open();
    let closer = operator_set.group_close();
    let var_opener = operator_set.variable_open();
    let var_closer = operator_set.variable_close();

    // Check whether input_string starts with opening symbol
    if search(&input_string[position..], opener) != Some(0) {
        return None;
    }

    let mut depth: u16 = 0;
    let mut search_location = position + opener.len();

    let multi_search_query = [opener, closer, var_opener];

    // Search through string for the first occurance of a opening or closing symbol
    let mut search_result = multi_search(&input_string[search_location..], &multi_search_query);
    while search_result != None {
        let unwrapped_search_result = search_result.unwrap();

        let query_index = unwrapped_search_result.0;
        let pool_index = unwrapped_search_result.1;

        match query_index {
            0 => {
                // Opening symbol found at pool_index

                depth += 1;
                search_location += pool_index + opener.len();
            }
            1 => {
                // Closing symbol found at pool_index

                if depth == 0 {
                    return Some(String::from(
                        &input_string[position + opener.len()..pool_index + search_location],
                    ));
                }

                depth -= 1;
                search_location += pool_index + closer.len();
            }
            _ => {
                // Variable open symbol found at pool_index

                let var_closer_loc = search(
                    &input_string[search_location + pool_index + var_opener.len()..],
                    var_closer,
                );

                match var_closer_loc {
                    None => {
                        return None;
                    }
                    Some(var_closer_loc) => {
                        search_location +=
                            pool_index + var_opener.len() + var_closer_loc + var_closer.len();
                    }
                }
            }
        }

        search_result = multi_search(&input_string[search_location..], &multi_search_query);
    }

    // No matching closing symbol found so return nothing
    None
}

fn get_variable_content(
    input_string: &str,
    position: usize,
    operator_set: &OperatorSet,
) -> Option<String> {
    use util::search;

    let opener = operator_set.variable_open();
    let closer = operator_set.variable_close();

    if search(&input_string[position..], opener) != Some(0) {
        return None;
    }

    let search_string = &input_string[position + opener.len()..];
    let closer_location = search(search_string, closer);

    match closer_location {
        None => None,
        Some(closer_location) => Some(String::from(&search_string[..closer_location])),
    }
}

fn easy_parse(input_string: &str, operator_set: &OperatorSet) -> Option<Result<LogicNode, usize>> {
    use LogicNode::*;

    if input_string.is_empty() {
        return Some(Err(0));
    }

    if input_string == operator_set.true_symbol() {
        return Some(Ok(True));
    }

    if input_string == operator_set.false_symbol() {
        return Some(Ok(False));
    }

    let variable_content_option = get_variable_content(input_string, 0, operator_set);
    if variable_content_option.is_some() {
        let variable_content = variable_content_option.unwrap();

        if variable_content.len()
            == input_string.len()
                - operator_set.variable_open().len()
                - operator_set.variable_close().len()
        {
            return Some(Ok(Variable(variable_content)));
        }
    }

    let group_content_option = get_group_content(input_string, 0, operator_set);
    if group_content_option.is_some() {
        let group_content = group_content_option.unwrap();

        if group_content.len()
            == input_string.len()
                - operator_set.group_open().len()
                - operator_set.group_close().len()
        {
            return Some(
                custom_parse(&group_content[..], operator_set)
                    .map_err(|err_loc| err_loc + operator_set.group_open().len()),
            );
        }
    }

    None
}

fn infix_parse(
    input_string: &str,
    position: usize,
    operator_set: &OperatorSet,
) -> Result<LogicNode, usize> {
    let symbol = if input_string[position..].starts_with(operator_set.and()) {
        operator_set.and()
    } else if input_string[position..].starts_with(operator_set.or()) {
        operator_set.or()
    } else {
        return Err(position);
    };

    let left = custom_parse(&input_string[..position], operator_set)?;
    let right = custom_parse(&input_string[position + symbol.len()..], operator_set)
        .map_err(|err_loc| position + symbol.len() + err_loc)?;

    Ok(
        match input_string[position..].starts_with(operator_set.and()) {
            true => LogicNode::And(Box::new(left), Box::new(right)),
            false => LogicNode::Or(Box::new(left), Box::new(right)),
        },
    )
}

/// Parsing of logic formulas with non-default operator symbols
/// Use the [OperatorSet](struct.OperatorSet.html) Struct
///
/// # Examples
/// ## Using a worded set
/// ```
/// use rustlogic::operators;
///
/// // Define the set
/// let operator_set = operators::common_sets::worded();
///
/// let parsed = rustlogic::custom_parse("(NOT $[A] AND $[B]) OR $[C]", &operator_set);
/// # assert!(parsed.is_ok());
///
/// // -- snipp
/// ```
///
/// ## Having a customized operator set
/// ```
/// use rustlogic::operators;
///
/// // This will be our new symbol for the AND logical function
/// let new_and_operator = "<AND>";
///
/// // Then we take the default set and adjust the AND symbol
/// let custom_operator_set = operators::common_sets::default()
///     .adjust_and(new_and_operator);
///
/// // Now we can use this set to parse logical strings
/// let parsed = rustlogic::custom_parse("([A]<AND>[B])|[C]", &custom_operator_set);
/// # assert!(parsed.is_ok());
///
/// // -- snipp
/// ```
///
/// ## Evaluating a custom logical string
/// ```
/// use rustlogic::operators;
///
/// // Define the set
/// let operator_set = operators::common_sets::worded();
///
/// let parsed = rustlogic::custom_parse("(NOT FALSE AND TRUE) OR FALSE", &operator_set);
/// # assert!(parsed.is_ok());
/// # assert!(parsed.unwrap().get_value().is_ok());
///
/// /// Now contains the value of the logical expression
/// # let parsed = rustlogic::custom_parse("(NOT FALSE AND TRUE) OR FALSE", &operator_set);
/// let value = parsed.unwrap().get_value().unwrap();
/// # assert!(value);
/// ```
///
/// ## Evaluating a custom logical string with variables
/// ```
/// use rustlogic::operators;
/// use std::collections::HashMap;
///
/// // Define the set
/// let operator_set = operators::common_sets::worded();
///
/// let parsed = rustlogic::custom_parse("(NOT $[A] AND $[B]) OR $[C]", &operator_set);
/// # assert!(parsed.is_ok());
///
/// // We assign the variables to their values
/// let mut hm = HashMap::new();
/// hm.insert("A", false);
/// hm.insert("B", true);
/// hm.insert("C", false);
/// # assert!(parsed.unwrap().get_value_from_variables(&hm).is_ok());
///
/// // Now contains the value of the logical expression
/// # let parsed = rustlogic::custom_parse("(NOT $[A] AND $[B]) OR $[C]", &operator_set);
/// let value = parsed.unwrap().get_value_from_variables(&hm).unwrap();
/// # assert!(value);
/// ```
pub fn custom_parse(input_string: &str, operator_set: &OperatorSet) -> Result<LogicNode, usize> {
    let easy_parse_option = easy_parse(input_string, operator_set);
    if easy_parse_option.is_some() {
        return easy_parse_option.unwrap();
    }

    if input_string.starts_with(operator_set.not()) {
        let easy_parse_option = easy_parse(&input_string[operator_set.not().len()..], operator_set);
        if easy_parse_option.is_some() {
            return easy_parse_option
                .unwrap()
                .map_err(|err_loc| err_loc + operator_set.not().len())
                .map(|node| LogicNode::Not(Box::new(node)));
        }
    }

    use util::multi_search;

    let multi_search_query = [
        operator_set.group_open(),
        operator_set.variable_open(),
        operator_set.and(),
        operator_set.or(),
    ];
    let mut ms_start = 0;
    let mut ms_result = multi_search(input_string, &multi_search_query);

    while ms_result.is_some() {
        match ms_result {
            None => return Err(input_string.len()), // Unreachable branch
            Some((0, pool_index)) => {
                let group_content =
                    get_group_content(input_string, ms_start + pool_index, operator_set);

                if group_content.is_none() {
                    return Err(ms_start + pool_index + operator_set.group_open().len());
                }

                ms_start += pool_index
                    + group_content.unwrap().len()
                    + operator_set.group_open().len()
                    + operator_set.group_close().len();
                ms_result = multi_search(&input_string[ms_start..], &multi_search_query);
            }
            Some((1, pool_index)) => {
                let variable_content =
                    get_variable_content(input_string, ms_start + pool_index, operator_set);

                if variable_content.is_none() {
                    return Err(ms_start + pool_index + operator_set.variable_open().len());
                }

                ms_start += pool_index
                    + variable_content.unwrap().len()
                    + operator_set.variable_open().len()
                    + operator_set.variable_close().len();
                ms_result = multi_search(&input_string[ms_start..], &multi_search_query);
            }
            Some((_, pool_index)) => {
                return infix_parse(input_string, ms_start + pool_index, operator_set)
            }
        };
    }

    Err(input_string.len())
}

/// Parse a formula string into in [LogicNode](enum.LogicNode.html) object
///
/// Outputs a results with an ok of the [LogicNode](enum.LogicNode.html) or the location of the error
///
/// # Examples
///
/// ## Basic usage (A pure variable)
/// ```
/// # use std::collections::HashMap;
/// # use rustlogic::parse;
/// let a_variable = parse("[a]").expect("Failed to parse variable");
///
/// let mut variable_map = HashMap::new();
/// variable_map.insert("a", true);
///
/// let value = a_variable.get_value_from_variables(&variable_map).unwrap();
/// println!("{}", value); // Will print true!
/// # assert!(value);
/// ```
///
/// ## 4-1 Multiplexer
/// ```
/// # use std::collections::HashMap;
/// # use rustlogic::parse;
/// let multiplexer_4_to_1 = parse("([a]&~[x]&~[y])|([b]&~[x]&[y])|([c]&[x]&~[y])|([d]&[x]&[y])")
///     .expect("Failed to parse 4-1 multiplexer");
///
/// let mut variable_map = HashMap::new();
///
/// // Input: 1001
/// variable_map.insert("a", true);
/// variable_map.insert("b", false);
/// variable_map.insert("c", false);
/// variable_map.insert("d", true);
///
/// // Selector: 11
/// variable_map.insert("x", true);
/// variable_map.insert("y", true);
///
/// // Should select fourth item from bus so true
/// let value = multiplexer_4_to_1.get_value_from_variables(&variable_map).unwrap();
/// println!("{}", value); // Will print true!
/// # assert!(value);
/// # variable_map.insert("d", false);
/// # let value = multiplexer_4_to_1.get_value_from_variables(&variable_map).unwrap();
/// # assert!(!value);
/// ```
pub fn parse(input_string: &str) -> Result<LogicNode, usize> {
    let operator_set = operators::common_sets::default();
    custom_parse(input_string, &operator_set)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_get_group_content() {
        let default_op = operators::common_sets::default();

        assert_eq!(
            get_group_content("(Hi)", 0, &default_op),
            Some(String::from("Hi"))
        );

        assert_eq!(
            get_group_content("  (before(inbetween(in)[var)test])after)", 2, &default_op),
            Some(String::from("before(inbetween(in)[var)test])after"))
        );

        let non_default_op = operators::common_sets::default()
            .adjust_group_open(" { ")
            .adjust_group_close(" } ");

        assert_eq!(
            get_group_content(
                " { before { inbetween { in } [var } test] } after } ",
                0,
                &non_default_op
            ),
            Some(String::from(
                "before { inbetween { in } [var } test] } after"
            ))
        );

        assert_eq!(
            get_group_content(
                "before { inbetween { in } [var } test] } after } ",
                0,
                &non_default_op
            ),
            None
        );

        assert_eq!(
            get_group_content(
                " { before { inbetween { in } [var } test] } after",
                0,
                &non_default_op
            ),
            None
        );
    }

    #[test]
    fn test_get_variable_content() {
        let default_op = operators::common_sets::default();

        assert_eq!(
            get_variable_content("[Hi]", 0, &default_op),
            Some(String::from("Hi"))
        );

        assert_eq!(
            get_variable_content("  [before(inbetween(in)[var)test])after]", 2, &default_op),
            Some(String::from("before(inbetween(in)[var)test"))
        );

        let non_default_op = operators::common_sets::default()
            .adjust_variable_open(" { ")
            .adjust_variable_close(" } ");

        assert_eq!(
            get_variable_content(
                " { before { inbetween { in } [var } test] } after } ",
                0,
                &non_default_op
            ),
            Some(String::from("before { inbetween { in"))
        );

        assert_eq!(
            get_group_content(
                " { before { inbetween { in [var test] after ",
                0,
                &non_default_op
            ),
            None
        );
    }

    #[test]
    fn test_operator_set() {
        let operator_set = operators::common_sets::worded();

        let mut hm = HashMap::new();

        hm.insert("A", false);
        hm.insert("B", true);
        hm.insert("C", false);

        assert!(custom_parse("(NOT $[A] AND $[B]) OR $[C]", &operator_set)
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());

        assert!(custom_parse("(($[A]) AND $[B]) OR NOT $[C]", &operator_set)
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());

        assert!(!custom_parse("($[A] AND NOT $[B]) OR $[C]", &operator_set)
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
    }

    #[test]
    fn test_parsing() {
        assert!(parse("[a]").is_ok());
        assert!(parse("~[a]").is_ok());
        assert!(parse("([a])").is_ok());
        assert!(parse("[a]|[b]").is_ok());
        assert!(parse("[a]&[b]").is_ok());
        assert!(parse("1").is_ok());
        assert!(parse("0").is_ok());
        assert!(parse("([a]&([b]|0))").is_ok());

        assert!(parse("[a]&&[b]").is_err());
        assert!(parse("[a]||[b]").is_err());
        assert!(parse("~[a]&[b]").is_ok());
    }

    #[test]
    fn test_values() {
        let mut hm = HashMap::new();

        hm.insert("a", false);
        hm.insert("b", false);

        assert!(parse("][]").is_err());
        assert!(!parse("[a]").unwrap().get_value_from_variables(&hm).unwrap());
        assert!(parse("~[a]")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
        assert!(!parse("([a])")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
        assert!(!parse("[a]|[b]")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
        assert!(!parse("[a]&[b]")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
        assert!(parse("1").unwrap().get_value_from_variables(&hm).unwrap());
        assert!(!parse("0").unwrap().get_value_from_variables(&hm).unwrap());
        assert!(!parse("([a]&([b]|0))")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
        assert!(!parse("~[a]&[b]")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());

        hm.insert("a", false);
        hm.insert("b", true);
        assert!(!parse("[a]").unwrap().get_value_from_variables(&hm).unwrap());
        assert!(parse("~[a]")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
        assert!(!parse("([a])")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
        assert!(parse("[a]|[b]")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
        assert!(!parse("[a]&[b]")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
        assert!(parse("1").unwrap().get_value_from_variables(&hm).unwrap());
        assert!(!parse("0").unwrap().get_value_from_variables(&hm).unwrap());
        assert!(!parse("([a]&([b]|0))")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
        assert!(parse("~[a]&[b]")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());

        hm.insert("a", true);
        hm.insert("b", false);
        assert!(parse("[a]").unwrap().get_value_from_variables(&hm).unwrap());
        assert!(!parse("~[a]")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
        assert!(parse("([a])")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
        assert!(parse("[a]|[b]")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
        assert!(!parse("[a]&[b]")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
        assert!(parse("1").unwrap().get_value_from_variables(&hm).unwrap());
        assert!(!parse("0").unwrap().get_value_from_variables(&hm).unwrap());
        assert!(!parse("([a]&([b]|0))")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
        assert!(!parse("~[a]&[b]")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());

        hm.insert("a", true);
        hm.insert("b", true);
        assert!(parse("[a]").unwrap().get_value_from_variables(&hm).unwrap());
        assert!(!parse("~[a]")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
        assert!(parse("([a])")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
        assert!(parse("[a]|[b]")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
        assert!(parse("[a]&[b]")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
        assert!(parse("1").unwrap().get_value_from_variables(&hm).unwrap());
        assert!(!parse("0").unwrap().get_value_from_variables(&hm).unwrap());
        assert!(parse("([a]&([b]|0))")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
        assert!(!parse("~[a]&[b]")
            .unwrap()
            .get_value_from_variables(&hm)
            .unwrap());
    }

    #[test]
    fn test_display() {
        let three_way_and = parse("([a]&[b]&[c])").expect("Unable to parse three way and");
        assert_eq!("( [a] & [b] & [c] )", format!("{}", three_way_and));

        let three_way_and = parse("(([a]&[b])&[c])").expect("Unable to parse three way and");
        assert_eq!("( [a] & [b] & [c] )", format!("{}", three_way_and));

        let three_way_and = parse("([a]&([b]&[c]))").expect("Unable to parse three way and");
        assert_eq!("( [a] & [b] & [c] )", format!("{}", three_way_and));

        let three_way_or = parse("([a]|[b]|[c])").expect("Unable to parse three way or");
        assert_eq!("( [a] | [b] | [c] )", format!("{}", three_way_or));

        let formula = parse("(~[a]&~[b]&~[c])").expect("Unable to parse three way and");
        assert_eq!("( ~[a] & ~[b] & ~[c] )", format!("{}", formula));
    }
}