ucglib/build/opcode/
display.rs

1// Copyright 2019 Jeremy Wall
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14use std::fmt;
15
16use super::Composite;
17use super::Primitive;
18use super::Value;
19
20use Composite::{List, Tuple};
21use Primitive::{Bool, Empty, Float, Int, Str};
22use Value::{C, F, M, P, S, T};
23
24impl fmt::Display for Value {
25    fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
26        match self {
27            P(p) => write!(w, "{}", p),
28            C(List(ref els, _)) => {
29                write!(w, "[")?;
30                for e in els {
31                    write!(w, "{},", e)?;
32                }
33                write!(w, "]")
34            }
35            C(Tuple(ref flds, _)) => {
36                write!(w, "{{")?;
37                for (k, v) in flds {
38                    write!(w, "\"{}\"={},\n", k, v)?;
39                }
40                write!(w, "}}")
41            }
42            F(_) => write!(w, "<Func>"),
43            M(_) => write!(w, "<Module>"),
44            T(_) => write!(w, "<Expression>"),
45            S(v) => write!(w, "{}", v),
46        }
47    }
48}
49
50impl fmt::Display for Primitive {
51    fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
52        match self {
53            Bool(v) => write!(w, "{}", v),
54            Int(v) => write!(w, "{}", v),
55            Float(v) => write!(w, "{}", v),
56            Str(v) => write!(w, "\"{}\"", v.replace("\"", "\\\"")),
57            Empty => write!(w, "NULL"),
58        }
59    }
60}