ctxt.rs 865 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. use std::cell::RefCell;
  2. pub struct Ctxt {
  3. errors: RefCell<Option<Vec<syn::Error>>>,
  4. }
  5. impl Ctxt {
  6. pub fn new() -> Self {
  7. Self {
  8. errors: RefCell::new(Some(Vec::new())),
  9. }
  10. }
  11. pub fn syn_error(&self, err: syn::Error) {
  12. self.errors.borrow_mut().as_mut().unwrap().push(err)
  13. }
  14. pub fn check(self) -> syn::Result<()> {
  15. let mut errors = self.errors.take().unwrap().into_iter();
  16. let mut combined = match errors.next() {
  17. Some(first) => first,
  18. None => return Ok(()),
  19. };
  20. for rest in errors {
  21. combined.combine(rest);
  22. }
  23. Err(combined)
  24. }
  25. }
  26. impl Drop for Ctxt {
  27. fn drop(&mut self) {
  28. if !std::thread::panicking() && self.errors.borrow().is_some() {
  29. panic!("forgot to check for errors");
  30. }
  31. }
  32. }