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