aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2020-07-24 15:30:12 +0100
committerJonas Schievink <[email protected]>2020-07-24 15:30:12 +0100
commit6f02befee4249618a2a7858d27649fa389888ea8 (patch)
tree57ebf87e049cf1bded4c94338ca86dcd84e77d27 /crates/ra_hir_expand
parentc3defe2532ba6ffd12a13bcbc8fdeda037665efc (diff)
Add a builder for DiagnosticSink
Diffstat (limited to 'crates/ra_hir_expand')
-rw-r--r--crates/ra_hir_expand/src/diagnostics.rs43
1 files changed, 26 insertions, 17 deletions
diff --git a/crates/ra_hir_expand/src/diagnostics.rs b/crates/ra_hir_expand/src/diagnostics.rs
index 545cff9bd..6a5844f31 100644
--- a/crates/ra_hir_expand/src/diagnostics.rs
+++ b/crates/ra_hir_expand/src/diagnostics.rs
@@ -48,23 +48,6 @@ pub struct DiagnosticSink<'a> {
48} 48}
49 49
50impl<'a> DiagnosticSink<'a> { 50impl<'a> DiagnosticSink<'a> {
51 /// FIXME: split `new` and `on` into a separate builder type
52 pub fn new(cb: impl FnMut(&dyn Diagnostic) + 'a) -> DiagnosticSink<'a> {
53 DiagnosticSink { callbacks: Vec::new(), default_callback: Box::new(cb) }
54 }
55
56 pub fn on<D: Diagnostic, F: FnMut(&D) + 'a>(mut self, mut cb: F) -> DiagnosticSink<'a> {
57 let cb = move |diag: &dyn Diagnostic| match diag.downcast_ref::<D>() {
58 Some(d) => {
59 cb(d);
60 Ok(())
61 }
62 None => Err(()),
63 };
64 self.callbacks.push(Box::new(cb));
65 self
66 }
67
68 pub fn push(&mut self, d: impl Diagnostic) { 51 pub fn push(&mut self, d: impl Diagnostic) {
69 let d: &dyn Diagnostic = &d; 52 let d: &dyn Diagnostic = &d;
70 self._push(d); 53 self._push(d);
@@ -80,3 +63,29 @@ impl<'a> DiagnosticSink<'a> {
80 (self.default_callback)(d) 63 (self.default_callback)(d)
81 } 64 }
82} 65}
66
67pub struct DiagnosticSinkBuilder<'a> {
68 callbacks: Vec<Box<dyn FnMut(&dyn Diagnostic) -> Result<(), ()> + 'a>>,
69}
70
71impl<'a> DiagnosticSinkBuilder<'a> {
72 pub fn new() -> Self {
73 Self { callbacks: Vec::new() }
74 }
75
76 pub fn on<D: Diagnostic, F: FnMut(&D) + 'a>(mut self, mut cb: F) -> Self {
77 let cb = move |diag: &dyn Diagnostic| match diag.downcast_ref::<D>() {
78 Some(d) => {
79 cb(d);
80 Ok(())
81 }
82 None => Err(()),
83 };
84 self.callbacks.push(Box::new(cb));
85 self
86 }
87
88 pub fn build<F: FnMut(&dyn Diagnostic) + 'a>(self, default_callback: F) -> DiagnosticSink<'a> {
89 DiagnosticSink { callbacks: self.callbacks, default_callback: Box::new(default_callback) }
90 }
91}