Skip to main content

provenant/license_detection/expression/
simplify.rs

1//! License expression simplification and utilities.
2
3use std::collections::HashSet;
4
5use super::{LicenseExpression, ParseError};
6
7/// Simplify a license expression by deduplicating and reducing boolean clauses.
8///
9/// # Arguments
10/// * `expr` - The expression to simplify
11///
12/// # Returns
13/// Simplified expression with duplicate and subsumed licenses removed,
14/// preserving order.
15pub fn simplify_expression(expr: &LicenseExpression) -> LicenseExpression {
16    match expr {
17        LicenseExpression::License(key) => LicenseExpression::License(key.clone()),
18        LicenseExpression::LicenseRef(key) => LicenseExpression::LicenseRef(key.clone()),
19        LicenseExpression::With { left, right } => LicenseExpression::With {
20            left: Box::new(simplify_expression(left)),
21            right: Box::new(simplify_expression(right)),
22        },
23        LicenseExpression::And { .. } => {
24            let mut unique = Vec::new();
25            let mut seen = HashSet::new();
26            collect_unique_and(expr, &mut unique, &mut seen);
27            prune_subsumed_operands(&mut unique, true);
28            build_expression_from_list(&unique, true)
29        }
30        LicenseExpression::Or { .. } => {
31            let mut unique = Vec::new();
32            let mut seen = HashSet::new();
33            collect_unique_or(expr, &mut unique, &mut seen);
34            prune_subsumed_operands(&mut unique, false);
35            build_expression_from_list(&unique, false)
36        }
37    }
38}
39
40fn prune_subsumed_operands(operands: &mut Vec<LicenseExpression>, outer_is_and: bool) {
41    let inner_is_and = !outer_is_and;
42    let pruned: Vec<LicenseExpression> = operands
43        .iter()
44        .enumerate()
45        .filter(|(candidate_idx, candidate)| {
46            !operands.iter().enumerate().any(|(other_idx, other)| {
47                candidate_idx != &other_idx && operand_subsumes(other, candidate, inner_is_and)
48            })
49        })
50        .map(|(_, operand)| operand.clone())
51        .collect();
52
53    *operands = pruned;
54}
55
56fn operand_subsumes(
57    other: &LicenseExpression,
58    candidate: &LicenseExpression,
59    inner_is_and: bool,
60) -> bool {
61    let other_args = get_flat_args(other);
62    let candidate_args = get_flat_args(candidate);
63
64    if other_args.len() >= candidate_args.len() {
65        return false;
66    }
67
68    let relevant_operator = matches!(other, LicenseExpression::And { .. })
69        || matches!(other, LicenseExpression::Or { .. })
70        || matches!(candidate, LicenseExpression::And { .. })
71        || matches!(candidate, LicenseExpression::Or { .. });
72
73    if !relevant_operator {
74        return false;
75    }
76
77    let operator_matches = if inner_is_and {
78        matches!(candidate, LicenseExpression::And { .. })
79            || matches!(other, LicenseExpression::And { .. })
80    } else {
81        matches!(candidate, LicenseExpression::Or { .. })
82            || matches!(other, LicenseExpression::Or { .. })
83    };
84
85    if !operator_matches {
86        return false;
87    }
88
89    other_args.iter().all(|other_arg| {
90        candidate_args
91            .iter()
92            .any(|arg| expressions_equal(arg, other_arg))
93    })
94}
95
96fn collect_unique_and(
97    expr: &LicenseExpression,
98    unique: &mut Vec<LicenseExpression>,
99    seen: &mut HashSet<String>,
100) {
101    match expr {
102        LicenseExpression::And { left, right } => {
103            collect_unique_and(left, unique, seen);
104            collect_unique_and(right, unique, seen);
105        }
106        LicenseExpression::Or { .. } => {
107            let simplified = simplify_expression(expr);
108            let key = expression_to_string(&simplified);
109            if !seen.contains(&key) {
110                seen.insert(key);
111                unique.push(simplified);
112            }
113        }
114        LicenseExpression::With { left, right } => {
115            let simplified = LicenseExpression::With {
116                left: Box::new(simplify_expression(left)),
117                right: Box::new(simplify_expression(right)),
118            };
119            let key = expression_to_string(&simplified);
120            if !seen.contains(&key) {
121                seen.insert(key);
122                unique.push(simplified);
123            }
124        }
125        LicenseExpression::License(key) => {
126            if !seen.contains(key) {
127                seen.insert(key.clone());
128                unique.push(LicenseExpression::License(key.clone()));
129            }
130        }
131        LicenseExpression::LicenseRef(key) => {
132            if !seen.contains(key) {
133                seen.insert(key.clone());
134                unique.push(LicenseExpression::LicenseRef(key.clone()));
135            }
136        }
137    }
138}
139
140fn collect_unique_or(
141    expr: &LicenseExpression,
142    unique: &mut Vec<LicenseExpression>,
143    seen: &mut HashSet<String>,
144) {
145    match expr {
146        LicenseExpression::Or { left, right } => {
147            collect_unique_or(left, unique, seen);
148            collect_unique_or(right, unique, seen);
149        }
150        LicenseExpression::And { .. } => {
151            let simplified = simplify_expression(expr);
152            let key = expression_to_string(&simplified);
153            if !seen.contains(&key) {
154                seen.insert(key);
155                unique.push(simplified);
156            }
157        }
158        LicenseExpression::With { left, right } => {
159            let simplified = LicenseExpression::With {
160                left: Box::new(simplify_expression(left)),
161                right: Box::new(simplify_expression(right)),
162            };
163            let key = expression_to_string(&simplified);
164            if !seen.contains(&key) {
165                seen.insert(key);
166                unique.push(simplified);
167            }
168        }
169        LicenseExpression::License(key) => {
170            if !seen.contains(key) {
171                seen.insert(key.clone());
172                unique.push(LicenseExpression::License(key.clone()));
173            }
174        }
175        LicenseExpression::LicenseRef(key) => {
176            if !seen.contains(key) {
177                seen.insert(key.clone());
178                unique.push(LicenseExpression::LicenseRef(key.clone()));
179            }
180        }
181    }
182}
183
184fn build_expression_from_list(unique: &[LicenseExpression], is_and: bool) -> LicenseExpression {
185    match unique.len() {
186        0 => panic!("build_expression_from_list called with empty list"),
187        1 => unique[0].clone(),
188        _ => {
189            let mut iter = unique.iter();
190            let mut result = iter.next().unwrap().clone();
191            for expr in iter {
192                result = if is_and {
193                    LicenseExpression::And {
194                        left: Box::new(result),
195                        right: Box::new(expr.clone()),
196                    }
197                } else {
198                    LicenseExpression::Or {
199                        left: Box::new(result),
200                        right: Box::new(expr.clone()),
201                    }
202                };
203            }
204            result
205        }
206    }
207}
208
209fn get_flat_args(expr: &LicenseExpression) -> Vec<LicenseExpression> {
210    match expr {
211        LicenseExpression::And { left, right } => {
212            let mut args = Vec::new();
213            collect_flat_and_args(left, &mut args);
214            collect_flat_and_args(right, &mut args);
215            args
216        }
217        LicenseExpression::Or { left, right } => {
218            let mut args = Vec::new();
219            collect_flat_or_args(left, &mut args);
220            collect_flat_or_args(right, &mut args);
221            args
222        }
223        _ => vec![expr.clone()],
224    }
225}
226
227fn collect_flat_and_args(expr: &LicenseExpression, args: &mut Vec<LicenseExpression>) {
228    match expr {
229        LicenseExpression::And { left, right } => {
230            collect_flat_and_args(left, args);
231            collect_flat_and_args(right, args);
232        }
233        _ => args.push(expr.clone()),
234    }
235}
236
237fn collect_flat_or_args(expr: &LicenseExpression, args: &mut Vec<LicenseExpression>) {
238    match expr {
239        LicenseExpression::Or { left, right } => {
240            collect_flat_or_args(left, args);
241            collect_flat_or_args(right, args);
242        }
243        _ => args.push(expr.clone()),
244    }
245}
246
247fn decompose_expr(expr: &LicenseExpression) -> Vec<LicenseExpression> {
248    match expr {
249        LicenseExpression::With { left, right } => {
250            let mut parts = decompose_expr(left);
251            parts.extend(decompose_expr(right));
252            parts
253        }
254        _ => vec![expr.clone()],
255    }
256}
257
258fn expressions_equal(a: &LicenseExpression, b: &LicenseExpression) -> bool {
259    match (a, b) {
260        (LicenseExpression::License(ka), LicenseExpression::License(kb)) => ka == kb,
261        (LicenseExpression::LicenseRef(ka), LicenseExpression::LicenseRef(kb)) => ka == kb,
262        (
263            LicenseExpression::With {
264                left: l1,
265                right: r1,
266            },
267            LicenseExpression::With {
268                left: l2,
269                right: r2,
270            },
271        ) => expressions_equal(l1, l2) && expressions_equal(r1, r2),
272        (LicenseExpression::And { .. }, LicenseExpression::And { .. }) => {
273            let args_a = get_flat_args(a);
274            let args_b = get_flat_args(b);
275            args_a.len() == args_b.len()
276                && args_b
277                    .iter()
278                    .all(|b_arg| args_a.iter().any(|a_arg| expressions_equal(a_arg, b_arg)))
279        }
280        (LicenseExpression::Or { .. }, LicenseExpression::Or { .. }) => {
281            let args_a = get_flat_args(a);
282            let args_b = get_flat_args(b);
283            args_a.len() == args_b.len()
284                && args_b
285                    .iter()
286                    .all(|b_arg| args_a.iter().any(|a_arg| expressions_equal(a_arg, b_arg)))
287        }
288        _ => false,
289    }
290}
291
292fn expr_in_args(expr: &LicenseExpression, args: &[LicenseExpression]) -> bool {
293    if args.iter().any(|a| expressions_equal(a, expr)) {
294        return true;
295    }
296    let decomposed = decompose_expr(expr);
297    if decomposed.len() == 1 {
298        return false;
299    }
300    decomposed
301        .iter()
302        .any(|d| args.iter().any(|a| expressions_equal(a, d)))
303}
304
305pub fn licensing_contains(container: &str, contained: &str) -> bool {
306    let container = container.trim();
307    let contained = contained.trim();
308    if container.is_empty() || contained.is_empty() {
309        return false;
310    }
311
312    if container == contained {
313        return true;
314    }
315
316    let Ok(parsed_container) = super::parse::parse_expression(container) else {
317        return false;
318    };
319    let Ok(parsed_contained) = super::parse::parse_expression(contained) else {
320        return false;
321    };
322
323    let simplified_container = simplify_expression(&parsed_container);
324    let simplified_contained = simplify_expression(&parsed_contained);
325
326    match (&simplified_container, &simplified_contained) {
327        (LicenseExpression::And { .. }, LicenseExpression::And { .. })
328        | (LicenseExpression::Or { .. }, LicenseExpression::Or { .. }) => {
329            let container_args = get_flat_args(&simplified_container);
330            let contained_args = get_flat_args(&simplified_contained);
331            contained_args
332                .iter()
333                .all(|c| container_args.iter().any(|ca| expressions_equal(ca, c)))
334        }
335        (
336            LicenseExpression::And { .. } | LicenseExpression::Or { .. },
337            LicenseExpression::License(_) | LicenseExpression::LicenseRef(_),
338        ) => {
339            let container_args = get_flat_args(&simplified_container);
340            expr_in_args(&simplified_contained, &container_args)
341        }
342        (LicenseExpression::And { .. } | LicenseExpression::Or { .. }, _) => {
343            let container_args = get_flat_args(&simplified_container);
344            container_args
345                .iter()
346                .any(|ca| expressions_equal(ca, &simplified_contained))
347        }
348        (
349            LicenseExpression::With { .. },
350            LicenseExpression::License(_) | LicenseExpression::LicenseRef(_),
351        ) => {
352            let decomposed = decompose_expr(&simplified_container);
353            decomposed
354                .iter()
355                .any(|d| expressions_equal(d, &simplified_contained))
356        }
357        (
358            LicenseExpression::License(_) | LicenseExpression::LicenseRef(_),
359            LicenseExpression::And { .. }
360            | LicenseExpression::Or { .. }
361            | LicenseExpression::With { .. },
362        ) => false,
363        (LicenseExpression::License(k1), LicenseExpression::License(k2)) => k1 == k2,
364        (LicenseExpression::LicenseRef(k1), LicenseExpression::LicenseRef(k2)) => k1 == k2,
365        _ => false,
366    }
367}
368
369/// # Returns
370/// String representation of the expression
371///
372/// # Parentheses
373/// Parentheses are added when needed to preserve semantic meaning based on
374/// operator precedence (WITH > AND > OR). This matches the Python
375/// license-expression library behavior.
376/// Convert a license expression to its string representation.
377#[derive(Clone, Copy)]
378enum BooleanOperator {
379    And,
380    Or,
381}
382
383pub fn expression_to_string(expr: &LicenseExpression) -> String {
384    match expr {
385        LicenseExpression::License(key) => key.clone(),
386        LicenseExpression::LicenseRef(key) => key.clone(),
387        LicenseExpression::And { .. } => render_flat_boolean_chain(expr, BooleanOperator::And),
388        LicenseExpression::Or { .. } => render_flat_boolean_chain(expr, BooleanOperator::Or),
389        LicenseExpression::With { left, right } => {
390            let left_str = expression_to_string(left);
391            let right_str = expression_to_string(right);
392            format!("{} WITH {}", left_str, right_str)
393        }
394    }
395}
396
397fn render_flat_boolean_chain(expr: &LicenseExpression, operator: BooleanOperator) -> String {
398    let mut parts = Vec::new();
399    collect_boolean_chain(expr, operator, &mut parts);
400
401    let separator = match operator {
402        BooleanOperator::And => " AND ",
403        BooleanOperator::Or => " OR ",
404    };
405
406    parts
407        .into_iter()
408        .map(|part| render_boolean_operand(part, operator))
409        .collect::<Vec<_>>()
410        .join(separator)
411}
412
413fn collect_boolean_chain<'a>(
414    expr: &'a LicenseExpression,
415    operator: BooleanOperator,
416    parts: &mut Vec<&'a LicenseExpression>,
417) {
418    match (operator, expr) {
419        (BooleanOperator::And, LicenseExpression::And { left, right })
420        | (BooleanOperator::Or, LicenseExpression::Or { left, right }) => {
421            collect_boolean_chain(left, operator, parts);
422            collect_boolean_chain(right, operator, parts);
423        }
424        _ => parts.push(expr),
425    }
426}
427
428fn render_boolean_operand(expr: &LicenseExpression, parent_operator: BooleanOperator) -> String {
429    match expr {
430        LicenseExpression::License(key) => key.clone(),
431        LicenseExpression::LicenseRef(key) => key.clone(),
432        LicenseExpression::And { .. } => match parent_operator {
433            BooleanOperator::And => expression_to_string(expr),
434            BooleanOperator::Or => format!("({})", expression_to_string(expr)),
435        },
436        LicenseExpression::Or { .. } => match parent_operator {
437            BooleanOperator::Or => expression_to_string(expr),
438            BooleanOperator::And => format!("({})", expression_to_string(expr)),
439        },
440        LicenseExpression::With { left, right } => {
441            let left_str = expression_to_string(left);
442            let right_str = expression_to_string(right);
443            format!("{} WITH {}", left_str, right_str)
444        }
445    }
446}
447
448fn combine_expressions_with(
449    expressions: &[&str],
450    unique: bool,
451    combiner: fn(Vec<LicenseExpression>) -> Option<LicenseExpression>,
452) -> Result<String, ParseError> {
453    if expressions.is_empty() {
454        return Ok(String::new());
455    }
456    if expressions.len() == 1 {
457        let parsed = super::parse::parse_expression(expressions[0])?;
458        return Ok(expression_to_string(&if unique {
459            simplify_expression(&parsed)
460        } else {
461            parsed
462        }));
463    }
464
465    let parsed_exprs: Vec<LicenseExpression> = expressions
466        .iter()
467        .map(|e| super::parse::parse_expression(e))
468        .collect::<Result<Vec<_>, _>>()?;
469
470    let combined = combiner(parsed_exprs);
471
472    match combined {
473        Some(expr) => {
474            let final_expr = if unique {
475                simplify_expression(&expr)
476            } else {
477                expr
478            };
479            Ok(expression_to_string(&final_expr))
480        }
481        None => Ok(String::new()),
482    }
483}
484
485/// Combine multiple license expressions with `AND`.
486///
487/// This function parses each expression string, combines them with `AND`, and
488/// optionally deduplicates license keys.
489pub fn combine_expressions_and(expressions: &[&str], unique: bool) -> Result<String, ParseError> {
490    combine_expressions_with(expressions, unique, LicenseExpression::and)
491}
492
493/// Combine multiple license expressions with `OR`.
494///
495/// This function parses each expression string, combines them with `OR`, and
496/// optionally deduplicates license keys.
497#[allow(dead_code)]
498pub fn combine_expressions_or(expressions: &[&str], unique: bool) -> Result<String, ParseError> {
499    combine_expressions_with(expressions, unique, LicenseExpression::or)
500}
501
502#[cfg(test)]
503mod tests {
504    use super::*;
505
506    #[test]
507    fn test_simplify_expression_no_change() {
508        let expr = super::super::parse::parse_expression("MIT AND Apache-2.0").unwrap();
509        let simplified = simplify_expression(&expr);
510        assert_eq!(expression_to_string(&simplified), "mit AND apache-2.0");
511    }
512
513    #[test]
514    fn test_simplify_expression_with_duplicates() {
515        let expr = super::super::parse::parse_expression("MIT OR MIT").unwrap();
516        let simplified = simplify_expression(&expr);
517        assert_eq!(expression_to_string(&simplified), "mit");
518    }
519
520    #[test]
521    fn test_simplify_and_duplicates() {
522        let expr = super::super::parse::parse_expression("crapl-0.1 AND crapl-0.1").unwrap();
523        let simplified = simplify_expression(&expr);
524        assert_eq!(expression_to_string(&simplified), "crapl-0.1");
525    }
526
527    #[test]
528    fn test_simplify_or_duplicates() {
529        let expr = super::super::parse::parse_expression("mit OR mit").unwrap();
530        let simplified = simplify_expression(&expr);
531        assert_eq!(expression_to_string(&simplified), "mit");
532    }
533
534    #[test]
535    fn test_simplify_preserves_different_licenses() {
536        let expr = super::super::parse::parse_expression("mit AND apache-2.0").unwrap();
537        let simplified = simplify_expression(&expr);
538        assert_eq!(expression_to_string(&simplified), "mit AND apache-2.0");
539    }
540
541    #[test]
542    fn test_simplify_complex_duplicates() {
543        let expr = super::super::parse::parse_expression(
544            "gpl-2.0-plus AND gpl-2.0-plus AND lgpl-2.0-plus",
545        )
546        .unwrap();
547        let simplified = simplify_expression(&expr);
548        assert_eq!(
549            expression_to_string(&simplified),
550            "gpl-2.0-plus AND lgpl-2.0-plus"
551        );
552    }
553
554    #[test]
555    fn test_simplify_three_duplicates() {
556        let expr =
557            super::super::parse::parse_expression("fsf-free AND fsf-free AND fsf-free").unwrap();
558        let simplified = simplify_expression(&expr);
559        assert_eq!(expression_to_string(&simplified), "fsf-free");
560    }
561
562    #[test]
563    fn test_simplify_with_expression_dedup() {
564        let expr = super::super::parse::parse_expression(
565            "gpl-2.0 WITH classpath-exception-2.0 AND gpl-2.0 WITH classpath-exception-2.0",
566        )
567        .unwrap();
568        let simplified = simplify_expression(&expr);
569        assert_eq!(
570            expression_to_string(&simplified),
571            "gpl-2.0 WITH classpath-exception-2.0"
572        );
573    }
574
575    #[test]
576    fn test_simplify_nested_duplicates() {
577        let expr =
578            super::super::parse::parse_expression("(mit AND apache-2.0) OR (mit AND apache-2.0)")
579                .unwrap();
580        let simplified = simplify_expression(&expr);
581        assert_eq!(expression_to_string(&simplified), "mit AND apache-2.0");
582    }
583
584    #[test]
585    fn test_simplify_preserves_order() {
586        let expr =
587            super::super::parse::parse_expression("apache-2.0 AND mit AND apache-2.0").unwrap();
588        let simplified = simplify_expression(&expr);
589        assert_eq!(expression_to_string(&simplified), "apache-2.0 AND mit");
590    }
591
592    #[test]
593    fn test_simplify_mit_and_mit_and_apache() {
594        let expr = super::super::parse::parse_expression("mit AND mit AND apache-2.0").unwrap();
595        let simplified = simplify_expression(&expr);
596        assert_eq!(expression_to_string(&simplified), "mit AND apache-2.0");
597    }
598
599    #[test]
600    fn test_simplify_and_absorption() {
601        let expr = super::super::parse::parse_expression("mit AND (mit OR apache-2.0)").unwrap();
602        let simplified = simplify_expression(&expr);
603
604        assert_eq!(expression_to_string(&simplified), "mit");
605    }
606
607    #[test]
608    fn test_simplify_or_absorption() {
609        let expr = super::super::parse::parse_expression("mit OR (mit AND apache-2.0)").unwrap();
610        let simplified = simplify_expression(&expr);
611
612        assert_eq!(expression_to_string(&simplified), "mit");
613    }
614
615    #[test]
616    fn test_simplify_or_subsumption() {
617        let expr = super::super::parse::parse_expression(
618            "(mit AND apache-2.0) OR (mit AND apache-2.0 AND bsd-new)",
619        )
620        .unwrap();
621        let simplified = simplify_expression(&expr);
622
623        assert_eq!(expression_to_string(&simplified), "mit AND apache-2.0");
624    }
625
626    #[test]
627    fn test_simplify_and_subsumption() {
628        let expr = super::super::parse::parse_expression(
629            "(mit OR apache-2.0) AND (mit OR apache-2.0 OR bsd-new)",
630        )
631        .unwrap();
632        let simplified = simplify_expression(&expr);
633
634        assert_eq!(expression_to_string(&simplified), "mit OR apache-2.0");
635    }
636
637    #[test]
638    fn test_simplify_and_keeps_gpl_or_later_with_only() {
639        let expr =
640            super::super::parse::parse_expression("gpl-2.0-or-later AND gpl-2.0-only").unwrap();
641        let simplified = simplify_expression(&expr);
642
643        assert_eq!(
644            expression_to_string(&simplified),
645            "gpl-2.0-or-later AND gpl-2.0-only"
646        );
647    }
648
649    #[test]
650    fn test_expression_to_string_simple() {
651        let expr = LicenseExpression::License("mit".to_string());
652        assert_eq!(expression_to_string(&expr), "mit");
653    }
654
655    #[test]
656    fn test_expression_to_string_and() {
657        let expr = LicenseExpression::And {
658            left: Box::new(LicenseExpression::License("mit".to_string())),
659            right: Box::new(LicenseExpression::License("apache-2.0".to_string())),
660        };
661        assert_eq!(expression_to_string(&expr), "mit AND apache-2.0");
662    }
663
664    #[test]
665    fn test_expression_to_string_or() {
666        let expr = LicenseExpression::Or {
667            left: Box::new(LicenseExpression::License("mit".to_string())),
668            right: Box::new(LicenseExpression::License("apache-2.0".to_string())),
669        };
670        assert_eq!(expression_to_string(&expr), "mit OR apache-2.0");
671    }
672
673    #[test]
674    fn test_expression_to_string_with() {
675        let expr = LicenseExpression::With {
676            left: Box::new(LicenseExpression::License("gpl-2.0".to_string())),
677            right: Box::new(LicenseExpression::License(
678                "classpath-exception-2.0".to_string(),
679            )),
680        };
681        assert_eq!(
682            expression_to_string(&expr),
683            "gpl-2.0 WITH classpath-exception-2.0"
684        );
685    }
686
687    #[test]
688    fn test_expression_to_string_licenseref() {
689        let expr = LicenseExpression::LicenseRef("licenseref-scancode-custom".to_string());
690        assert_eq!(expression_to_string(&expr), "licenseref-scancode-custom");
691    }
692
693    #[test]
694    fn test_expression_to_string_or_inside_and() {
695        let or_expr = LicenseExpression::Or {
696            left: Box::new(LicenseExpression::License("mit".to_string())),
697            right: Box::new(LicenseExpression::License("apache-2.0".to_string())),
698        };
699        let and_expr = LicenseExpression::And {
700            left: Box::new(or_expr),
701            right: Box::new(LicenseExpression::License("gpl-2.0".to_string())),
702        };
703        assert_eq!(
704            expression_to_string(&and_expr),
705            "(mit OR apache-2.0) AND gpl-2.0"
706        );
707    }
708
709    #[test]
710    fn test_expression_to_string_and_inside_or() {
711        let and_expr = LicenseExpression::And {
712            left: Box::new(LicenseExpression::License("mit".to_string())),
713            right: Box::new(LicenseExpression::License("apache-2.0".to_string())),
714        };
715        let or_expr = LicenseExpression::Or {
716            left: Box::new(and_expr),
717            right: Box::new(LicenseExpression::License("gpl-2.0".to_string())),
718        };
719        assert_eq!(
720            expression_to_string(&or_expr),
721            "(mit AND apache-2.0) OR gpl-2.0"
722        );
723    }
724
725    #[test]
726    fn test_expression_to_string_with_inside_or() {
727        let with_expr = LicenseExpression::With {
728            left: Box::new(LicenseExpression::License("gpl-2.0".to_string())),
729            right: Box::new(LicenseExpression::License(
730                "classpath-exception-2.0".to_string(),
731            )),
732        };
733        let or_expr = LicenseExpression::Or {
734            left: Box::new(with_expr),
735            right: Box::new(LicenseExpression::License("mit".to_string())),
736        };
737        assert_eq!(
738            expression_to_string(&or_expr),
739            "gpl-2.0 WITH classpath-exception-2.0 OR mit"
740        );
741    }
742
743    #[test]
744    fn test_expression_to_string_with_inside_and() {
745        let with_expr = LicenseExpression::With {
746            left: Box::new(LicenseExpression::License("gpl-2.0".to_string())),
747            right: Box::new(LicenseExpression::License(
748                "classpath-exception-2.0".to_string(),
749            )),
750        };
751        let and_expr = LicenseExpression::And {
752            left: Box::new(with_expr),
753            right: Box::new(LicenseExpression::License("mit".to_string())),
754        };
755        assert_eq!(
756            expression_to_string(&and_expr),
757            "gpl-2.0 WITH classpath-exception-2.0 AND mit"
758        );
759    }
760
761    #[test]
762    fn test_expression_to_string_nested_or_flattens_same_operator_grouping() {
763        let or_expr = LicenseExpression::Or {
764            left: Box::new(LicenseExpression::Or {
765                left: Box::new(LicenseExpression::License("mit".to_string())),
766                right: Box::new(LicenseExpression::License("apache-2.0".to_string())),
767            }),
768            right: Box::new(LicenseExpression::License("gpl-2.0".to_string())),
769        };
770        assert_eq!(
771            expression_to_string(&or_expr),
772            "mit OR apache-2.0 OR gpl-2.0"
773        );
774    }
775
776    #[test]
777    fn test_expression_to_string_nested_and_flattens_same_operator_grouping() {
778        let and_expr = LicenseExpression::And {
779            left: Box::new(LicenseExpression::And {
780                left: Box::new(LicenseExpression::License("mit".to_string())),
781                right: Box::new(LicenseExpression::License("apache-2.0".to_string())),
782            }),
783            right: Box::new(LicenseExpression::License("gpl-2.0".to_string())),
784        };
785        assert_eq!(
786            expression_to_string(&and_expr),
787            "mit AND apache-2.0 AND gpl-2.0"
788        );
789    }
790
791    #[test]
792    fn test_expression_to_string_roundtrip_or_and() {
793        let input = "(mit OR apache-2.0) AND gpl-2.0";
794        let expr = super::super::parse::parse_expression(input).unwrap();
795        let output = expression_to_string(&expr);
796        assert_eq!(output, "(mit OR apache-2.0) AND gpl-2.0");
797    }
798
799    #[test]
800    fn test_expression_to_string_roundtrip_or_with() {
801        let input = "gpl-2.0 WITH classpath-exception-2.0 OR mit";
802        let expr = super::super::parse::parse_expression(input).unwrap();
803        let output = expression_to_string(&expr);
804        assert_eq!(output, "gpl-2.0 WITH classpath-exception-2.0 OR mit");
805    }
806
807    #[test]
808    fn test_combine_expressions_empty() {
809        let result = combine_expressions_and(&[], true).unwrap();
810        assert_eq!(result, "");
811    }
812
813    #[test]
814    fn test_combine_expressions_single() {
815        let result = combine_expressions_and(&["mit"], true).unwrap();
816        assert_eq!(result, "mit");
817    }
818
819    #[test]
820    fn test_combine_expressions_two_and() {
821        let result = combine_expressions_and(&["mit", "gpl-2.0-plus"], true).unwrap();
822        assert_eq!(result, "mit AND gpl-2.0-plus");
823    }
824
825    #[test]
826    fn test_combine_expressions_two_or() {
827        let result = combine_expressions_or(&["mit", "apache-2.0"], true).unwrap();
828        assert_eq!(result, "mit OR apache-2.0");
829    }
830
831    #[test]
832    fn test_combine_expressions_multiple_and() {
833        let result = combine_expressions_and(&["mit", "apache-2.0", "gpl-2.0-plus"], true).unwrap();
834        assert_eq!(result, "mit AND apache-2.0 AND gpl-2.0-plus");
835    }
836
837    #[test]
838    fn test_combine_expressions_with_duplicates_unique() {
839        let result = combine_expressions_or(&["mit", "mit", "apache-2.0"], true).unwrap();
840        let expr = super::super::parse::parse_expression(&result).unwrap();
841        let keys = expr.license_keys();
842        assert_eq!(keys.len(), 2);
843        assert!(keys.contains(&"mit".to_string()));
844        assert!(keys.contains(&"apache-2.0".to_string()));
845    }
846
847    #[test]
848    fn test_combine_expressions_with_duplicates_not_unique() {
849        let result = combine_expressions_or(&["mit", "mit", "apache-2.0"], false).unwrap();
850        let expr = super::super::parse::parse_expression(&result).unwrap();
851        assert_eq!(result, "mit OR mit OR apache-2.0");
852        let keys = expr.license_keys();
853        assert_eq!(keys.len(), 2);
854    }
855
856    #[test]
857    fn test_combine_expressions_complex_with_simplification() {
858        let result = combine_expressions_and(&["mit OR apache-2.0", "gpl-2.0-plus"], true).unwrap();
859        assert_eq!(result, "(mit OR apache-2.0) AND gpl-2.0-plus");
860        let expr = super::super::parse::parse_expression(&result).unwrap();
861        assert!(matches!(expr, LicenseExpression::And { .. }));
862        let keys = expr.license_keys();
863        assert_eq!(keys.len(), 3);
864    }
865
866    #[test]
867    fn test_combine_expressions_parse_error() {
868        let result = combine_expressions_and(&["mit", "@invalid@"], true);
869        assert!(result.is_err());
870    }
871
872    #[test]
873    fn test_combine_expressions_with_existing_and() {
874        let result = combine_expressions_and(&["mit AND apache-2.0", "gpl-2.0"], true).unwrap();
875        assert!(result.contains("mit"));
876        assert!(result.contains("apache-2.0"));
877        assert!(result.contains("gpl-2.0"));
878    }
879
880    #[test]
881    fn test_combine_expressions_with_existing_or() {
882        let result = combine_expressions_or(&["mit OR apache-2.0", "gpl-2.0"], true).unwrap();
883        assert!(result.contains("mit"));
884        assert!(result.contains("apache-2.0"));
885        assert!(result.contains("gpl-2.0"));
886    }
887
888    #[test]
889    fn test_expression_to_string_with_no_outer_parens() {
890        let with_expr = LicenseExpression::With {
891            left: Box::new(LicenseExpression::License("gpl-2.0-plus".to_string())),
892            right: Box::new(LicenseExpression::License(
893                "classpath-exception-2.0".to_string(),
894            )),
895        };
896        assert_eq!(
897            expression_to_string(&with_expr),
898            "gpl-2.0-plus WITH classpath-exception-2.0"
899        );
900    }
901
902    #[test]
903    fn test_expression_to_string_with_as_right_operand_of_or() {
904        let with_expr = LicenseExpression::With {
905            left: Box::new(LicenseExpression::License("gpl-2.0".to_string())),
906            right: Box::new(LicenseExpression::License(
907                "classpath-exception-2.0".to_string(),
908            )),
909        };
910        let or_expr = LicenseExpression::Or {
911            left: Box::new(LicenseExpression::License("mit".to_string())),
912            right: Box::new(with_expr),
913        };
914        assert_eq!(
915            expression_to_string(&or_expr),
916            "mit OR gpl-2.0 WITH classpath-exception-2.0"
917        );
918    }
919
920    #[test]
921    fn test_expression_to_string_with_as_right_operand_of_and() {
922        let with_expr = LicenseExpression::With {
923            left: Box::new(LicenseExpression::License("gpl-2.0".to_string())),
924            right: Box::new(LicenseExpression::License(
925                "classpath-exception-2.0".to_string(),
926            )),
927        };
928        let and_expr = LicenseExpression::And {
929            left: Box::new(LicenseExpression::License("mit".to_string())),
930            right: Box::new(with_expr),
931        };
932        assert_eq!(
933            expression_to_string(&and_expr),
934            "mit AND gpl-2.0 WITH classpath-exception-2.0"
935        );
936    }
937
938    #[test]
939    fn test_expression_to_string_complex_precedence() {
940        let input = "mit OR apache-2.0 AND gpl-2.0";
941        let expr = super::super::parse::parse_expression(input).unwrap();
942        assert_eq!(
943            expression_to_string(&expr),
944            "mit OR (apache-2.0 AND gpl-2.0)"
945        );
946    }
947
948    #[test]
949    fn test_expression_to_string_with_no_outer_parens_in_complex_and() {
950        // WITH has higher precedence than AND
951        // Parsed as: (bsd-new AND mit) AND (gpl-3.0-plus WITH autoconf-simple-exception)
952        let input = "bsd-new AND mit AND gpl-3.0-plus WITH autoconf-simple-exception";
953        let expr = super::super::parse::parse_expression(input).unwrap();
954        assert_eq!(
955            expression_to_string(&expr),
956            "bsd-new AND mit AND gpl-3.0-plus WITH autoconf-simple-exception"
957        );
958    }
959
960    #[test]
961    fn test_combine_expressions_and_flattens_reported_redundant_parentheses() {
962        let result = combine_expressions_and(
963            &[
964                "Apache-2.0",
965                "BSD-3-Clause",
966                "GPL-2.0-only",
967                "LicenseRef-scancode-oracle-openjdk-exception-2.0",
968                "APSL-1.0",
969                "APSL-2.0",
970            ],
971            true,
972        )
973        .unwrap();
974
975        assert_eq!(
976            result,
977            "apache-2.0 AND bsd-3-clause AND gpl-2.0-only AND licenseref-scancode-oracle-openjdk-exception-2.0 AND apsl-1.0 AND apsl-2.0"
978        );
979    }
980}
981
982#[cfg(test)]
983mod contains_tests {
984    use super::*;
985
986    #[test]
987    fn test_basic_containment() {
988        assert!(licensing_contains("mit", "mit"));
989        assert!(!licensing_contains("mit", "apache"));
990    }
991
992    #[test]
993    fn test_or_containment() {
994        assert!(licensing_contains("mit OR apache", "mit"));
995        assert!(licensing_contains("mit OR apache", "apache"));
996        assert!(!licensing_contains("mit OR apache", "gpl"));
997    }
998
999    #[test]
1000    fn test_and_containment() {
1001        assert!(licensing_contains("mit AND apache", "mit"));
1002        assert!(licensing_contains("mit AND apache", "apache"));
1003        assert!(!licensing_contains("mit", "mit AND apache"));
1004    }
1005
1006    #[test]
1007    fn test_expression_subset() {
1008        assert!(licensing_contains(
1009            "mit AND apache AND bsd",
1010            "mit AND apache"
1011        ));
1012        assert!(!licensing_contains(
1013            "mit AND apache",
1014            "mit AND apache AND bsd"
1015        ));
1016        assert!(licensing_contains("mit OR apache OR bsd", "mit OR apache"));
1017        assert!(!licensing_contains("mit OR apache", "mit OR apache OR bsd"));
1018    }
1019
1020    #[test]
1021    fn test_order_independence() {
1022        assert!(licensing_contains("mit AND apache", "apache AND mit"));
1023        assert!(licensing_contains("mit OR apache", "apache OR mit"));
1024    }
1025
1026    #[test]
1027    fn test_plus_suffix_no_containment() {
1028        assert!(!licensing_contains("gpl-2.0-plus", "gpl-2.0"));
1029        assert!(!licensing_contains("gpl-2.0", "gpl-2.0-plus"));
1030    }
1031
1032    #[test]
1033    fn test_with_decomposition() {
1034        assert!(licensing_contains(
1035            "gpl-2.0 WITH classpath-exception",
1036            "gpl-2.0"
1037        ));
1038        assert!(licensing_contains(
1039            "gpl-2.0 WITH classpath-exception",
1040            "classpath-exception"
1041        ));
1042        assert!(!licensing_contains(
1043            "gpl-2.0",
1044            "gpl-2.0 WITH classpath-exception"
1045        ));
1046    }
1047
1048    #[test]
1049    fn test_mixed_operators() {
1050        assert!(!licensing_contains("mit OR apache", "mit AND apache"));
1051        assert!(!licensing_contains("mit AND apache", "mit OR apache"));
1052    }
1053
1054    #[test]
1055    fn test_nested_expressions() {
1056        assert!(!licensing_contains("(mit OR apache) AND bsd", "mit"));
1057        assert!(licensing_contains(
1058            "(mit OR apache) AND bsd",
1059            "mit OR apache"
1060        ));
1061        assert!(licensing_contains("(mit OR apache) AND bsd", "bsd"));
1062    }
1063
1064    #[test]
1065    fn test_empty_expressions() {
1066        assert!(!licensing_contains("", "mit"));
1067        assert!(!licensing_contains("mit", ""));
1068        assert!(!licensing_contains("", ""));
1069        assert!(!licensing_contains("   ", "mit"));
1070    }
1071
1072    #[test]
1073    fn test_invalid_expressions() {
1074        assert!(!licensing_contains("mit AND", "mit"));
1075        assert!(!licensing_contains("mit", "AND apache"));
1076    }
1077}