sdml_errors/lib.rs
1/*!
2This crate provides the common error and diagnostics for SDML.
3
4For passing errors the typical [`Error`] and [`Result`] types are provided.
5
6For fine-grained reporting of model issues the [`diagnostics`] module allow for describing
7and emitting structured values.
8 */
9
10#![warn(
11 unknown_lints,
12 // ---------- Stylistic
13 absolute_paths_not_starting_with_crate,
14 elided_lifetimes_in_paths,
15 explicit_outlives_requirements,
16 macro_use_extern_crate,
17 nonstandard_style, /* group */
18 noop_method_call,
19 rust_2018_idioms,
20 single_use_lifetimes,
21 trivial_casts,
22 trivial_numeric_casts,
23 // ---------- Future
24 future_incompatible, /* group */
25 rust_2021_compatibility, /* group */
26 // ---------- Public
27 missing_debug_implementations,
28 // missing_docs,
29 unreachable_pub,
30 // ---------- Unsafe
31 unsafe_code,
32 unsafe_op_in_unsafe_fn,
33 // ---------- Unused
34 unused, /* group */
35)]
36#![deny(
37 // ---------- Public
38 exported_private_dependencies,
39 // ---------- Deprecated
40 anonymous_parameters,
41 bare_trait_objects,
42 ellipsis_inclusive_range_patterns,
43 // ---------- Unsafe
44 deref_nullptr,
45 drop_bounds,
46 dyn_drop,
47)]
48
49use codespan_reporting::files::SimpleFiles;
50use std::fmt::Display;
51
52// ------------------------------------------------------------------------------------------------
53// Public Types
54// ------------------------------------------------------------------------------------------------
55
56///
57/// An opaque identifier used to index the source associated with a loaded module.
58///
59pub type FileId = usize;
60
61///
62/// A type that holds the source code loaded prior to parsing.
63///
64#[derive(Clone, Debug)]
65pub struct Source(String);
66
67///
68/// The mapping of module names to corresponding source code.
69///
70pub type SourceFiles = SimpleFiles<String, Source>;
71
72///
73/// A span, in bytes, start..end for some context.
74///
75pub type Span = std::ops::Range<usize>;
76
77// ------------------------------------------------------------------------------------------------
78// Implementations
79// ------------------------------------------------------------------------------------------------
80
81impl Display for Source {
82 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83 write!(f, "{}", self.0)
84 }
85}
86
87impl From<String> for Source {
88 fn from(value: String) -> Self {
89 Self(value)
90 }
91}
92
93impl AsRef<str> for Source {
94 fn as_ref(&self) -> &str {
95 self.as_str()
96 }
97}
98
99impl AsRef<[u8]> for Source {
100 fn as_ref(&self) -> &[u8] {
101 self.as_bytes()
102 }
103}
104
105impl Source {
106 fn as_str(&self) -> &str {
107 self.0.as_str()
108 }
109
110 fn as_bytes(&self) -> &[u8] {
111 self.0.as_bytes()
112 }
113}
114
115// ------------------------------------------------------------------------------------------------
116// Modules
117// ------------------------------------------------------------------------------------------------
118
119#[macro_use]
120mod i18n;
121
122pub mod errors;
123pub use errors::Error;
124
125pub mod diagnostics;
126pub use diagnostics::{Diagnostic, Reporter};