Debian Codegraph @

README.md

raw symbols SCIP
1 # as_variant
2
3 [![docs.rs](https://img.shields.io/crates/v/as_variant?color=blue&label=docs&style=flat-square)](https://docs.rs/as_variant)
4
5 A simple Rust macro to convert enums with newtype variants to `Option`s.
6
7 Basic example:
8
9 ```rust
10 use std::ops::Deref;
11
12 use as_variant::as_variant;
13
14 enum Value {
15 Integer(i64),
16 String(String),
17 Array(Vec<Value>),
18 }
19
20 impl Value {
21 pub fn as_integer(&self) -> Option<i64> {
22 as_variant!(self, Self::Integer).copied()
23 }
24
25 pub fn as_str(&self) -> Option<&str> {
26 as_variant!(self, Self::String).map(Deref::deref)
27 }
28
29 pub fn as_array(&self) -> Option<&[Value]> {
30 as_variant!(self, Self::Array).map(Deref::deref)
31 }
32
33 pub fn into_string(self) -> Option<String> {
34 as_variant!(self, Self::String)
35 }
36
37 pub fn into_array(self) -> Option<Vec<Value>> {
38 as_variant!(self, Self::Array)
39 }
40 }
41 ```