Skip to content

Commit a9db545

Browse files
committed
Derive Facet for the types in graphannis-core types module
1 parent 591516a commit a9db545

5 files changed

Lines changed: 31 additions & 9 deletions

File tree

core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ binary-layout = "4.0.1"
1515
bincode = "1.2"
1616
clru = "0.6.1"
1717
facet = "0.28.0"
18+
facet-reflect = "0.28.0"
1819
itertools = "0.10"
1920
lazy_static = "1.4"
2021
log = "0.4"

core/src/errors.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ use std::{
33
sync::PoisonError,
44
};
55

6+
use facet_reflect::ReflectError;
67
use thiserror::Error;
78

89
use crate::types::AnnoKey;
910

10-
#[derive(Error, Debug)]
11+
#[derive(Error, Debug, strum_macros::IntoStaticStr)]
1112
#[non_exhaustive]
1213
pub enum GraphAnnisCoreError {
1314
#[error("invalid component type {0}")]
@@ -74,6 +75,8 @@ pub enum GraphAnnisCoreError {
7475
#[error(transparent)]
7576
TomlSerializer(#[from] toml::ser::Error),
7677
#[error(transparent)]
78+
Reflection(#[from] ReflectError),
79+
#[error(transparent)]
7780
Other(#[from] Box<dyn std::error::Error + Send + Sync>),
7881
}
7982

core/src/types.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use facet::Facet;
2+
use facet_reflect::Partial;
23
use num_traits::{Bounded, FromPrimitive, Num, ToPrimitive};
34
use serde::Serialize;
45
use serde::de::DeserializeOwned;
@@ -7,8 +8,6 @@ use std::fmt;
78
use std::ops::AddAssign;
89

910
use std::{convert::TryInto, str::FromStr};
10-
use strum::IntoEnumIterator;
11-
use strum_macros::{EnumIter, EnumString};
1211

1312
use super::serializer::{FixedSizeKeySerializer, KeySerializer};
1413
use crate::serializer::KeyVec;
@@ -34,7 +33,9 @@ pub struct AnnoKey {
3433
}
3534

3635
/// An annotation with a qualified name and a value.
37-
#[derive(Serialize, Deserialize, Default, Eq, PartialEq, PartialOrd, Ord, Clone, Debug, Hash)]
36+
#[derive(
37+
Facet, Serialize, Deserialize, Default, Eq, PartialEq, PartialOrd, Ord, Clone, Debug, Hash,
38+
)]
3839
pub struct Annotation {
3940
/// Qualified name or unique "key" for the annotation
4041
pub key: AnnoKey,
@@ -43,7 +44,9 @@ pub struct Annotation {
4344
}
4445

4546
/// Directed edge between a source and target node which are identified by their ID.
46-
#[derive(Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord, Clone, Debug, Hash, Default)]
47+
#[derive(
48+
Facet, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord, Clone, Debug, Hash, Default,
49+
)]
4750
#[repr(C)]
4851
pub struct Edge {
4952
pub source: NodeID,
@@ -138,7 +141,8 @@ pub trait ComponentType:
138141
}
139142

140143
/// A simplified implementation of a `ComponentType` that only has one type of edges.
141-
#[derive(Clone, Eq, PartialEq, PartialOrd, Ord, EnumString, EnumIter, Debug)]
144+
#[derive(Facet, Clone, Eq, PartialEq, PartialOrd, Ord, Debug)]
145+
#[repr(u16)]
142146
pub enum DefaultComponentType {
143147
Edge,
144148
}
@@ -163,7 +167,7 @@ impl fmt::Display for DefaultComponentType {
163167

164168
pub struct DefaultGraphIndex;
165169

166-
#[derive(Serialize, Deserialize)]
170+
#[derive(Facet, Serialize, Deserialize)]
167171
pub struct DefaultGlobalStatistics;
168172

169173
impl ComponentType for DefaultComponentType {
@@ -176,15 +180,27 @@ impl ComponentType for DefaultComponentType {
176180
Ok(DefaultGraphIndex {})
177181
}
178182
fn all_component_types() -> Vec<Self> {
179-
DefaultComponentType::iter().collect()
183+
vec![DefaultComponentType::Edge]
180184
}
181185
fn calculate_global_statistics(_graph: &mut Graph<Self>) -> StdResult<(), ComponentTypeError> {
182186
Ok(())
183187
}
184188
}
185189

190+
impl FromStr for DefaultComponentType {
191+
type Err = GraphAnnisCoreError;
192+
193+
fn from_str(s: &str) -> StdResult<Self, Self::Err> {
194+
let result = Partial::alloc_shape(DefaultComponentType::SHAPE)?
195+
.select_variant_named(s)?
196+
.build()?
197+
.materialize()?;
198+
Ok(result)
199+
}
200+
}
201+
186202
/// Identifies an edge component of the graph.
187-
#[derive(Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord, Hash, Clone, Debug)]
203+
#[derive(Facet, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord, Hash, Clone, Debug)]
188204
pub struct Component<CT: ComponentType> {
189205
/// Type of the component
190206
ctype: u16,

graphannis/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ regex = "1"
2424
[dependencies]
2525
boolean_expression = "0.4"
2626
csv = "1"
27+
facet = "0.28.0"
2728
fs2 = "0.4"
2829
graphannis-core = { path = "../core/", version = "^3" }
2930
itertools = "0.10"

graphannis/src/annis/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{fmt::Display, sync::PoisonError};
22

33
use crate::annis::types::LineColumnRange;
4+
45
use graphannis_core::{
56
errors::{ComponentTypeError, GraphAnnisCoreError},
67
types::NodeID,

0 commit comments

Comments
 (0)