Skip to main content

litex/infer/
infer_result.rs

1use crate::prelude::*;
2use std::collections::HashSet;
3
4#[derive(Clone, Debug)]
5pub struct InferResult {
6    /// 自由文本(如 warning)与事实的 `Display` 字符串,按语句顺序。
7    infer_facts: Vec<String>,
8    /// 与 `new_fact` / `push_atomic_fact` 写入的字符串一一对应的结构化事实。
9    facts: Vec<Fact>,
10}
11
12impl InferResult {
13    pub fn new() -> Self {
14        InferResult {
15            infer_facts: vec![],
16            facts: vec![],
17        }
18    }
19
20    /// 将 `fact` 同时记入展示行与 [`Self::inferred_facts`]。
21    pub fn from_fact(fact: &Fact) -> Self {
22        let mut r = Self::new();
23        r.new_fact(fact);
24        r
25    }
26
27    pub fn is_empty(&self) -> bool {
28        self.infer_facts.is_empty() && self.facts.is_empty()
29    }
30
31    /// 用于 CLI / JSON `infer_facts`:按顺序的文本行(含 `new_fact` 写入的事实字符串与 `new_with_msg` 的文本)。
32    pub fn infer_lines(&self) -> &[String] {
33        &self.infer_facts
34    }
35
36    /// Same as [`Self::infer_lines`] but drops repeated strings while keeping first occurrence order (for JSON / CLI).
37    pub fn infer_lines_unique_in_order(&self) -> Vec<String> {
38        let mut seen = HashSet::new();
39        self.infer_facts
40            .iter()
41            .filter(|s| seen.insert((*s).clone()))
42            .cloned()
43            .collect()
44    }
45
46    /// 结构化推断事实(与 [`Self::infer_lines`] 中由事实产生的行对应)。
47    pub fn inferred_facts(&self) -> &[Fact] {
48        &self.facts
49    }
50
51    pub fn join_infer_lines(&self, sep: &str) -> String {
52        self.infer_facts.join(sep)
53    }
54
55    pub fn new_with_msg(&mut self, msg: String) {
56        self.infer_facts.push(msg);
57    }
58
59    pub fn new_fact(&mut self, fact: &Fact) {
60        self.infer_facts.push(fact.to_string());
61        self.facts.push(fact.clone());
62    }
63
64    pub fn push_atomic_fact(&mut self, atomic_fact: &AtomicFact) {
65        self.infer_facts.push(atomic_fact.to_string());
66        self.facts.push(atomic_fact.clone().into());
67    }
68
69    pub fn new_infer_result_inside(&mut self, other_infer_result: InferResult) {
70        self.infer_facts.extend(other_infer_result.infer_facts);
71        self.facts.extend(other_infer_result.facts);
72    }
73}