conjure_cp_core/ast/
name.rs1use std::fmt::Display;
2
3use itertools::Itertools as _;
4use polyquine::Quine;
5use serde::{Deserialize, Serialize};
6use ustr::Ustr;
7
8#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize, Quine)]
10#[path_prefix(conjure_cp::ast)]
11pub enum Name {
12 User(Ustr),
14 Machine(i32),
16
17 Represented(
20 Box<(
23 Name,
25 Ustr,
27 Ustr,
29 )>,
30 ),
31
32 WithRepresentation(
34 Box<Name>,
35 Vec<Ustr>,
37 ),
38}
39
40impl Name {
41 pub fn user(name: &str) -> Self {
43 Name::User(Ustr::from(name))
44 }
45}
46
47impl Default for Name {
48 fn default() -> Self {
49 Name::User(Ustr::from(""))
50 }
51}
52
53uniplate::derive_unplateable!(Name);
54
55impl Display for Name {
56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 match self {
58 Name::User(s) => write!(f, "{s}"),
59 Name::Machine(i) => write!(f, "__{i}"),
60 Name::Represented(fields) => {
61 let (name, rule_string, suffix) = fields.as_ref();
62 write!(f, "{name}#{rule_string}_{suffix}")
63 }
64 Name::WithRepresentation(name, items) => {
65 write!(f, "{name}#{}", items.iter().join("#"))
67 }
68 }
69 }
70}
71
72impl From<&str> for Name {
73 fn from(s: &str) -> Self {
74 Name::User(Ustr::from(s))
75 }
76}
77
78impl From<i32> for Name {
79 fn from(i: i32) -> Self {
80 Name::Machine(i)
81 }
82}