Skip to content

Commit dea957d

Browse files
committed
attribute display trait
1 parent 8014f2f commit dea957d

File tree

4 files changed

+59
-25
lines changed

4 files changed

+59
-25
lines changed

crates/ast/src/parser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use fil_utils::{
1010
use itertools::Itertools;
1111
use pest::pratt_parser::{Assoc, Op, PrattParser};
1212
use pest_consume::{match_nodes, Error, Parser};
13+
use std::fmt::Display;
1314
use std::fs;
1415
use std::hash::Hash;
1516
use std::path::Path;

crates/utils/src/attr/attributes.rs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use super::{AttrCtx, AttrStore};
22
use crate::GPosIdx;
3-
use std::{hash::Hash, str::FromStr};
3+
use std::{fmt::Display, hash::Hash};
44

55
/// Stores the attributes of a component
66
#[derive(Clone)]
77
pub struct Attributes<Bool, Num, Float>
88
where
9-
Bool: FromStr + Hash + Eq + Copy,
10-
Num: FromStr + Hash + Eq + Copy,
11-
Float: FromStr + Hash + Eq + Copy,
9+
Bool: Hash + Eq + Copy,
10+
Num: Hash + Eq + Copy,
11+
Float: Hash + Eq + Copy,
1212
{
1313
/// Numerical attributes
1414
num_attrs: AttrStore<Num, u64>,
@@ -20,9 +20,9 @@ where
2020

2121
impl<Bool, Num, Float> AttrCtx<Num, u64> for Attributes<Bool, Num, Float>
2222
where
23-
Bool: FromStr + Hash + Eq + Copy,
24-
Num: FromStr + Hash + Eq + Copy,
25-
Float: FromStr + Hash + Eq + Copy,
23+
Bool: Hash + Eq + Copy,
24+
Num: Hash + Eq + Copy,
25+
Float: Hash + Eq + Copy,
2626
{
2727
fn get(&self, attr: Num) -> Option<&u64> {
2828
self.num_attrs.get(attr)
@@ -43,9 +43,9 @@ where
4343

4444
impl<Bool, Num, Float> AttrCtx<Bool, bool> for Attributes<Bool, Num, Float>
4545
where
46-
Bool: FromStr + Hash + Eq + Copy,
47-
Num: FromStr + Hash + Eq + Copy,
48-
Float: FromStr + Hash + Eq + Copy,
46+
Bool: Hash + Eq + Copy,
47+
Num: Hash + Eq + Copy,
48+
Float: Hash + Eq + Copy,
4949
{
5050
fn get(&self, attr: Bool) -> Option<&bool> {
5151
self.bool_attrs.get(attr)
@@ -66,9 +66,9 @@ where
6666

6767
impl<Bool, Num, Float> AttrCtx<Float, f64> for Attributes<Bool, Num, Float>
6868
where
69-
Bool: FromStr + Hash + Eq + Copy,
70-
Num: FromStr + Hash + Eq + Copy,
71-
Float: FromStr + Hash + Eq + Copy,
69+
Bool: Hash + Eq + Copy,
70+
Num: Hash + Eq + Copy,
71+
Float: Hash + Eq + Copy,
7272
{
7373
fn get(&self, attr: Float) -> Option<&f64> {
7474
self.float_attrs.get(attr)
@@ -89,9 +89,9 @@ where
8989

9090
impl<Bool, Num, Float> Default for Attributes<Bool, Num, Float>
9191
where
92-
Bool: FromStr + Hash + Eq + Copy,
93-
Num: FromStr + Hash + Eq + Copy,
94-
Float: FromStr + Hash + Eq + Copy,
92+
Bool: Hash + Eq + Copy,
93+
Num: Hash + Eq + Copy,
94+
Float: Hash + Eq + Copy,
9595
{
9696
fn default() -> Self {
9797
Self {
@@ -101,3 +101,18 @@ where
101101
}
102102
}
103103
}
104+
105+
impl<Bool, Num, Float> Display for Attributes<Bool, Num, Float>
106+
where
107+
Bool: Display + Hash + Eq + Copy,
108+
Num: Display + Hash + Eq + Copy,
109+
Float: Display + Hash + Eq + Copy,
110+
{
111+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
112+
write!(f, "#[")?;
113+
write!(f, "{}", &self.num_attrs)?;
114+
write!(f, "{}", &self.bool_attrs)?;
115+
write!(f, "{}", &self.float_attrs)?;
116+
write!(f, "]")
117+
}
118+
}

crates/utils/src/attr/store.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
use crate::{attr::AttrCtx, GPosIdx};
2-
use std::collections::HashMap;
2+
use std::{collections::HashMap, fmt::Display, hash::Hash};
33

44
/// A store for attributes
55
#[derive(Clone)]
66
pub struct AttrStore<Attr, Value>
77
where
8-
Attr: Eq + std::hash::Hash + Copy,
8+
Attr: Eq + Hash + Copy,
99
{
1010
attrs: HashMap<Attr, (Value, GPosIdx)>,
1111
}
1212

1313
impl<Attr, Value> AttrStore<Attr, Value>
1414
where
15-
Attr: Eq + std::hash::Hash + Copy,
15+
Attr: Eq + Hash + Copy,
1616
{
1717
/// iterates over the attributes
1818
pub fn iter(&self) -> impl Iterator<Item = (Attr, &Value)> {
@@ -22,7 +22,7 @@ where
2222

2323
impl<Attr, Value> AttrCtx<Attr, Value> for AttrStore<Attr, Value>
2424
where
25-
Attr: Eq + std::hash::Hash + Copy,
25+
Attr: Eq + Hash + Copy,
2626
{
2727
fn get(&self, attr: Attr) -> Option<&Value> {
2828
self.attrs.get(&attr).map(|(value, _)| value)
@@ -43,11 +43,29 @@ where
4343

4444
impl<Attr, Value> Default for AttrStore<Attr, Value>
4545
where
46-
Attr: Eq + std::hash::Hash + Copy,
46+
Attr: Eq + Hash + Copy,
4747
{
4848
fn default() -> Self {
4949
Self {
5050
attrs: HashMap::new(),
5151
}
5252
}
5353
}
54+
55+
impl<Attr, Value> Display for AttrStore<Attr, Value>
56+
where
57+
Attr: Eq + Hash + Copy + Display,
58+
Value: Display,
59+
{
60+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61+
write!(
62+
f,
63+
"{}",
64+
self.attrs
65+
.iter()
66+
.map(|(attr, (value, _))| format!("{}={}", attr, value))
67+
.collect::<Vec<_>>()
68+
.join(", ")
69+
)
70+
}
71+
}

crates/utils/src/macros.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ macro_rules! attr_enum {
3535
)*
3636
};
3737
) => {
38-
#[derive(Clone, Copy, PartialEq, strum_macros::EnumString, Eq, Hash)]
38+
#[derive(Clone, Copy, PartialEq, strum_macros::EnumString, strum_macros::Display, Eq, Hash)]
3939
pub enum $name {
4040
$(
4141
$(#[$pub_meta])*
@@ -44,7 +44,7 @@ macro_rules! attr_enum {
4444
)*
4545
$(
4646
$(#[$priv_meta])*
47-
#[strum(disabled)]
47+
#[strum(disabled,to_string = $priv_str)]
4848
$priv,
4949
)*
5050
}
@@ -79,7 +79,7 @@ macro_rules! attr_enum {
7979
priv {
8080
$(
8181
$(#[$priv_meta:meta])*
82-
$priv:ident,
82+
$priv:ident: $priv_str:literal,
8383
)*
8484
};
8585
) => {
@@ -89,7 +89,7 @@ macro_rules! attr_enum {
8989
priv {
9090
$(
9191
$(#[$priv_meta])*
92-
$priv,
92+
$priv: $priv_str,
9393
)*
9494
};
9595
}

0 commit comments

Comments
 (0)