Skip to content

Commit

Permalink
Diagnostic custom kind change
Browse files Browse the repository at this point in the history
  • Loading branch information
hydroper committed Apr 26, 2024
1 parent 84b9114 commit cf62c1a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "as3_parser"
version = "0.5.20"
version = "0.5.22"
edition = "2021"
authors = ["hydroper <[email protected]>"]
repository = "https://github.com/hydroper/as3parser"
Expand Down
18 changes: 10 additions & 8 deletions crates/parser/diagnostics/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::any::Any;

use maplit::hashmap;
use crate::ns::*;

Expand All @@ -14,7 +16,7 @@ pub struct Diagnostic {
pub(crate) is_warning: bool,
pub(crate) is_verify_error: bool,
pub(crate) arguments: Vec<DiagnosticArgument>,
pub(crate) custom_id: RefCell<Option<String>>,
pub(crate) custom_kind: RefCell<Option<Rc<dyn Any>>>,
}

impl Eq for Diagnostic {}
Expand Down Expand Up @@ -46,7 +48,7 @@ impl Diagnostic {
is_verify_error: false,
is_warning: false,
arguments,
custom_id: RefCell::new(None),
custom_kind: RefCell::new(None),
}
}

Expand All @@ -57,7 +59,7 @@ impl Diagnostic {
is_verify_error: true,
is_warning: false,
arguments,
custom_id: RefCell::new(None),
custom_kind: RefCell::new(None),
}
}

Expand All @@ -68,7 +70,7 @@ impl Diagnostic {
is_verify_error: false,
is_warning: true,
arguments,
custom_id: RefCell::new(None),
custom_kind: RefCell::new(None),
}
}

Expand Down Expand Up @@ -104,12 +106,12 @@ impl Diagnostic {
self.kind.id()
}

pub fn custom_id(&self) -> Option<String> {
self.custom_id.borrow().clone()
pub fn custom_kind(&self) -> Option<Rc<dyn Any>> {
self.custom_kind.borrow().clone()
}

pub fn set_custom_id(&self, id: Option<&str>) {
self.custom_id.replace(id.map(|id| id.to_owned()));
pub fn set_custom_kind(&self, id: Option<Rc<dyn Any>>) {
self.custom_kind.replace(id);
}

/// Formats the diagnostic by overriding the message text.
Expand Down
18 changes: 12 additions & 6 deletions docs/diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,22 @@ The `diagnostic_arguments![]` literal takes elements in one of the forms:
* `Token(token)`
* `String(string)`

## Custom IDs
## Custom kinds

If you need to support your own messages, set the `custom_id` field:
If you need to support your own messages, set the `custom_kind` field:

```rust
diagnostic.set_custom_id(Some("myMessage"));
diagnostic.set_custom_kind(Some(Rc::new(kind)));
```

Finish formatting your own message by attaching additional information with:
Where `kind` is an enumeration's variant. You may then later cast the custom kind from `Rc<dyn Any>` to your enumeration through `.downcast::<MyEnum>()`.

```
If you want, for simple debugging purposes, you may finish formatting your own message with location information by using:

```rust
diagnostic.format_with_message("My message", Some(custom_id_number))
```
```

For real use cases, calling `.format_with_message()` is not preferred if your application is not solely in English, since it will add categories such as `Warning`.

Moreover, argument variants in `as3_parser::ns::Diagnostic` may not be enough. For example, a compiler may want to store a symbol argument; in this case, an extra layer over `as3_parser::ns::Diagnostic` must be provided, supporting more variants.

0 comments on commit cf62c1a

Please sign in to comment.