1pub mod parse;
2pub use parse::parse_liberty;
3
4use crate::error::LibError;
5
6#[derive(Debug, Clone, PartialEq)]
7pub enum Ast {
8 Group {
9 kind: String,
10 params: Vec<String>,
11 items: Vec<Ast>,
12 },
13 Attr {
14 key: String,
15 value: Value,
16 },
17}
18
19#[derive(Debug, Clone, PartialEq)]
20pub enum Value {
21 Bool(bool),
22 Float(f64),
23 FloatGroup(Vec<f64>),
24 String(String),
25 Ident(String),
26 Vec(Vec<Value>),
27}
28
29impl Ast {
30 pub fn group(kind: impl Into<String>, params: impl Into<Vec<String>>, items: impl Into<Vec<Ast>>) -> Self {
31 Self::Group {
32 kind: kind.into(),
33 params: params.into(),
34 items: items.into(),
35 }
36 }
37
38 pub fn group_single_param(kind: impl Into<String>, param: impl Into<String>, items: impl Into<Vec<Ast>>) -> Self {
39 Self::Group {
40 kind: kind.into(),
41 params: vec![param.into()],
42 items: items.into(),
43 }
44 }
45
46 pub fn attr(key: impl Into<String>, value: impl Into<Value>) -> Self {
47 Self::Attr { key: key.into(), value: value.into() }
48 }
49}
50
51impl Value {
52 pub fn kind_str(&self) -> &'static str {
53 match self {
54 Self::Bool(_) => "bool",
55 Self::Float(_) => "float",
56 Self::FloatGroup(_) => "floats",
57 Self::Ident(_) => "ident",
58 Self::String(_) => "string",
59 Self::Vec(_) => "vector",
60 }
61 }
62
63 pub fn bool(&self) -> Result<bool, LibError> {
64 if let Self::Bool(value) = self {
65 Ok(*value)
66 } else {
67 Err(LibError::ValueKindUnmatch("bool", self.kind_str()))
68 }
69 }
70
71 pub fn float(&self) -> Result<f64, LibError> {
72 if let Self::Float(value) = self {
73 Ok(*value)
74 } else {
75 Err(LibError::ValueKindUnmatch("float", self.kind_str()))
76 }
77 }
78
79 pub fn floats(&self) -> Result<&[f64], LibError> {
80 if let Self::FloatGroup(value) = self {
81 Ok(value)
82 } else {
83 Err(LibError::ValueKindUnmatch("floats", self.kind_str()))
84 }
85 }
86
87 pub fn ident(&self) -> Result<&str, LibError> {
88 if let Self::Ident(value) = self {
89 Ok(value)
90 } else {
91 Err(LibError::ValueKindUnmatch("ident", self.kind_str()))
92 }
93 }
94
95 pub fn string(&self) -> Result<&str, LibError> {
96 if let Self::String(value) = self {
97 Ok(value)
98 } else {
99 Err(LibError::ValueKindUnmatch("string", self.kind_str()))
100 }
101 }
102
103 pub fn vector(&self) -> Result<&[Value], LibError> {
104 if let Self::Vec(value) = self {
105 Ok(value)
106 } else {
107 Err(LibError::ValueKindUnmatch("vector", self.kind_str()))
108 }
109 }
110}
111
112impl Value {
113 pub fn into_bool(self) -> Result<bool, LibError> {
114 if let Self::Bool(value) = self {
115 Ok(value)
116 } else {
117 Err(LibError::ValueKindUnmatch("bool", self.kind_str()))
118 }
119 }
120
121 pub fn into_float(self) -> Result<f64, LibError> {
122 if let Self::Float(value) = self {
123 Ok(value)
124 } else {
125 Err(LibError::ValueKindUnmatch("float", self.kind_str()))
126 }
127 }
128
129 pub fn into_floats(self) -> Result<Vec<f64>, LibError> {
130 if let Self::FloatGroup(value) = self {
131 Ok(value)
132 } else {
133 Err(LibError::ValueKindUnmatch("floats", self.kind_str()))
134 }
135 }
136
137 pub fn into_ident(self) -> Result<String, LibError> {
138 if let Self::Ident(value) = self {
139 Ok(value)
140 } else {
141 Err(LibError::ValueKindUnmatch("ident", self.kind_str()))
142 }
143 }
144
145 pub fn into_string(self) -> Result<String, LibError> {
146 if let Self::String(value) = self {
147 Ok(value)
148 } else {
149 Err(LibError::ValueKindUnmatch("string", self.kind_str()))
150 }
151 }
152
153 pub fn into_vector(self) -> Result<Vec<Value>, LibError> {
154 if let Self::Vec(value) = self {
155 Ok(value)
156 } else {
157 Err(LibError::ValueKindUnmatch("vector", self.kind_str()))
158 }
159 }
160}