summaryrefslogtreecommitdiff
path: root/src/diag.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-12-15 22:51:55 +0100
committerLaurenz <laurmaedje@gmail.com>2022-12-15 23:11:20 +0100
commitb6202b646a0d5ecced301d9bac8bfcaf977d7ee4 (patch)
tree7d42cb50f9e66153e7e8b2217009684e25f54f42 /src/diag.rs
parentf3980c704544a464f9729cc8bc9f97d3a7454769 (diff)
Reflection for castables
Diffstat (limited to 'src/diag.rs')
-rw-r--r--src/diag.rs34
1 files changed, 24 insertions, 10 deletions
diff --git a/src/diag.rs b/src/diag.rs
index e244ba7c..55f16b5f 100644
--- a/src/diag.rs
+++ b/src/diag.rs
@@ -156,16 +156,6 @@ impl<T> Trace<T> for SourceResult<T> {
/// A result type with a string error message.
pub type StrResult<T> = Result<T, EcoString>;
-/// Transform `expected X, found Y` into `expected X or A, found Y`.
-pub fn with_alternative(msg: EcoString, alt: &str) -> EcoString {
- let mut parts = msg.split(", found ");
- if let (Some(a), Some(b)) = (parts.next(), parts.next()) {
- format_eco!("{} or {}, found {}", a, alt, b)
- } else {
- msg
- }
-}
-
/// Convert a [`StrResult`] to a [`SourceResult`] by adding span information.
pub trait At<T> {
/// Add the span information.
@@ -181,6 +171,30 @@ where
}
}
+/// Format the parts separated with commas and a final "and" or "or".
+pub(crate) fn comma_list<S>(buf: &mut String, parts: &[S], last: &str)
+where
+ S: AsRef<str>,
+{
+ for (i, part) in parts.iter().enumerate() {
+ match i {
+ 0 => {}
+ 1 if parts.len() == 2 => {
+ buf.push(' ');
+ buf.push_str(last);
+ buf.push(' ');
+ }
+ i if i + 1 == parts.len() => {
+ buf.push_str(", ");
+ buf.push_str(last);
+ buf.push(' ');
+ }
+ _ => buf.push_str(", "),
+ }
+ buf.push_str(part.as_ref());
+ }
+}
+
/// A result type with a file-related error.
pub type FileResult<T> = Result<T, FileError>;