1use std::collections::BTreeMap;
5use std::fmt;
6use std::path::PathBuf;
7
8use flow_types::{LibraryLayout, Placement, Segments};
9
10#[derive(Clone, Debug, PartialEq, Eq)]
14pub struct Contract {
15 root: PathBuf,
16 modules: BTreeMap<Segments, String>,
17}
18
19impl Contract {
20 pub(crate) fn new(root: PathBuf, modules: BTreeMap<Segments, String>) -> Self {
21 Self { root, modules }
22 }
23
24 #[must_use]
26 pub fn modules(&self) -> &BTreeMap<Segments, String> {
27 &self.modules
28 }
29
30 #[must_use]
33 pub fn module(&self, path: &str) -> Option<&str> {
34 self.modules.get(path).map(String::as_str)
35 }
36
37 pub fn committed(&self, module: &Segments) -> std::io::Result<String> {
45 let file = LibraryLayout::module_file(module, Placement::Contract);
46 std::fs::read_to_string(self.root.join(LibraryLayout::file(file.as_str())))
47 }
48}
49
50#[derive(Clone, Debug, PartialEq, Eq)]
53pub struct Location {
54 file: String,
55 line: u32,
56}
57
58impl Location {
59 pub(crate) fn new(file: String, line: u32) -> Self {
60 Self { file, line }
61 }
62
63 #[must_use]
65 pub fn file(&self) -> &str {
66 &self.file
67 }
68
69 #[must_use]
71 pub fn line(&self) -> u32 {
72 self.line
73 }
74}
75
76impl fmt::Display for Location {
77 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
78 write!(formatter, "{}:{}:1", self.file, self.line)
79 }
80}
81
82#[derive(Clone, Debug, PartialEq, Eq)]
84#[non_exhaustive]
85pub enum ContractError {
86 StdImportCollision {
88 at: Location,
90 name: String,
92 },
93 DuplicateTool {
95 at: Location,
97 name: String,
99 },
100 NotAModulePath {
102 at: Location,
104 module: String,
106 segment: String,
108 },
109 Spelled {
113 at: Location,
115 module: String,
117 },
118 ForeignType {
121 at: Location,
123 name: String,
125 module: String,
127 },
128 UnaddressedType {
132 at: Location,
134 name: String,
136 dependency: String,
138 how: String,
140 },
141 SharedFlowName {
143 at: Location,
145 name: String,
147 first: String,
149 second: String,
151 },
152 SeveralCrates {
154 first: String,
156 second: String,
158 },
159}
160
161impl fmt::Display for ContractError {
162 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
163 match self {
164 Self::StdImportCollision { at, name } => write!(
165 formatter,
166 "{at}: Tool `{name}` conflicts with import `std@v1/{name}`; give the Tool a different public name"
167 ),
168 Self::DuplicateTool { at, name } => {
169 write!(formatter, "{at}: duplicate Tool name {name}")
170 }
171 Self::NotAModulePath {
172 at,
173 module,
174 segment,
175 } => write!(
176 formatter,
177 "{at}: the module `{segment}` of `{module}` is not a Flow lowercase name, so it has no contract module path"
178 ),
179 Self::Spelled { at, module } => write!(
180 formatter,
181 "{at}: `{module}` is no module path: `lib` alone is the root module's file, contract/lib.flow, and a first segment `contract` is the contract's directory; rename the Rust module"
182 ),
183 Self::ForeignType { at, name, module } => write!(
184 formatter,
185 "{at}: {name} is defined in {module}, of a crate this library does not depend on"
186 ),
187 Self::UnaddressedType {
188 at,
189 name,
190 dependency,
191 how,
192 } => write!(
193 formatter,
194 "{at}: {name} comes from {how}, so it has no Flow address and no contract can import it; depend on {dependency} by a path in this repository or a git URL at a tag"
195 ),
196 Self::SharedFlowName {
197 at,
198 name,
199 first,
200 second,
201 } => write!(
202 formatter,
203 "{at}: distinct Rust types share the Flow name {name} in one module: {first} and {second}"
204 ),
205 Self::SeveralCrates { first, second } => write!(
206 formatter,
207 "a contract is one library's, but its Tools are defined in the crates {first} and {second}"
208 ),
209 }
210 }
211}
212
213impl std::error::Error for ContractError {}
214
215#[cfg(test)]
216#[cfg_attr(coverage, coverage(off))]
217mod tests {
218 use super::{ContractError, Location};
219
220 #[test]
221 fn a_location_names_its_file_and_line_as_an_editor_reads_them() {
222 let at = Location::new("src/lib.rs".into(), 12);
223 assert_eq!((at.file(), at.line()), ("src/lib.rs", 12));
224 assert_eq!(at.to_string(), "src/lib.rs:12:1");
225 }
226
227 #[test]
228 fn tools_of_two_crates_are_refused_naming_both() {
229 let refused = ContractError::SeveralCrates {
230 first: "flow_lib_git".into(),
231 second: "flow_lib_http".into(),
232 };
233 assert_eq!(
234 refused.to_string(),
235 "a contract is one library's, but its Tools are defined in the crates flow_lib_git and flow_lib_http"
236 );
237 }
238}