1#![deny(unsafe_code)]
7#![warn(missing_docs)]
8
9mod expr;
10mod item;
11mod stmt;
12mod types;
13mod version;
14
15pub use expr::Expr;
16pub use item::{Function, Module, Parameter, PuirItem, TypeDef, Visibility};
17pub use stmt::Stmt;
18pub use types::{PuirType, TypeEvidence, TypeInferenceBundle, TypeInferenceReport};
19pub use version::{check_puir_schema, PUIR_SCHEMA_VERSION};
20
21use indexmap::IndexMap;
22use parallax_core::{ErrorCode, ParallaxError};
23use serde::{Deserialize, Serialize};
24
25#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
27pub struct SourceSpan {
28 pub file: String,
30 pub line: u32,
32 pub column: u32,
34 pub end_line: Option<u32>,
36 pub end_column: Option<u32>,
38}
39
40impl SourceSpan {
41 pub fn new(file: impl Into<String>, line: u32, column: u32) -> Self {
43 Self {
44 file: file.into(),
45 line,
46 column,
47 end_line: None,
48 end_column: None,
49 }
50 }
51}
52
53#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
55#[serde(rename_all = "snake_case")]
56pub enum Confidence {
57 Exact,
59 High,
61 Medium,
63 Low,
65 Unsupported,
67}
68
69impl Confidence {
70 pub fn score(self) -> f64 {
72 match self {
73 Self::Exact => 1.0,
74 Self::High => 0.95,
75 Self::Medium => 0.75,
76 Self::Low => 0.45,
77 Self::Unsupported => 0.0,
78 }
79 }
80
81 pub fn label(self) -> &'static str {
83 match self {
84 Self::Exact => "EXACT",
85 Self::High => "HIGH",
86 Self::Medium => "MEDIUM",
87 Self::Low => "LOW",
88 Self::Unsupported => "UNSUPPORTED",
89 }
90 }
91}
92
93#[derive(Clone, Debug, Serialize, Deserialize)]
95pub struct PuirProgram {
96 pub version: u32,
98 pub modules: IndexMap<String, Module>,
100 pub metadata: IndexMap<String, serde_json::Value>,
102}
103
104impl PuirProgram {
105 pub fn new() -> Self {
107 Self {
108 version: PUIR_SCHEMA_VERSION,
109 modules: IndexMap::new(),
110 metadata: IndexMap::new(),
111 }
112 }
113
114 pub fn validate(&self) -> Result<(), ParallaxError> {
116 check_puir_schema(self.version)
117 }
118}
119
120impl Default for PuirProgram {
121 fn default() -> Self {
122 Self::new()
123 }
124}
125
126#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
128#[serde(transparent)]
129pub struct NodeId(pub u64);
130
131impl NodeId {
132 pub const fn new(id: u64) -> Self {
134 Self(id)
135 }
136}
137
138#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
140pub struct Effects {
141 pub io: bool,
143 pub async_: bool,
145 pub throws: bool,
147 pub env: bool,
149 pub fs: bool,
151 pub network: bool,
153}
154
155pub fn schema_error(msg: impl Into<String>) -> ParallaxError {
157 ParallaxError::new(ErrorCode::InvalidArgument, msg)
158 .with_source("parallax-puir")
159 .with_operation("validate")
160}