shex_ast/ast/
shape_decl.rs1use super::shape_expr::ShapeExpr;
2use crate::ast::deserialize_string_or_struct;
3use crate::ast::serialize_string_or_struct;
4use crate::Annotation;
5use crate::ShapeExprLabel;
6use prefixmap::Deref;
7use prefixmap::DerefError;
8use serde::{Deserialize, Serialize};
9
10#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
11pub struct ShapeDecl {
12 #[serde(rename = "type")]
13 pub type_: String,
14
15 pub id: ShapeExprLabel,
16
17 #[serde(rename = "abstract", default = "default_abstract")]
18 pub is_abstract: bool,
19
20 #[serde(
21 rename = "shapeExpr",
22 serialize_with = "serialize_string_or_struct",
23 deserialize_with = "deserialize_string_or_struct"
24 )]
25 pub shape_expr: ShapeExpr,
26}
27
28fn default_abstract() -> bool {
29 false
30}
31
32impl ShapeDecl {
33 pub fn new(label: ShapeExprLabel, shape_expr: ShapeExpr, is_abstract: bool) -> Self {
34 ShapeDecl {
35 type_: "ShapeDecl".to_string(),
36 is_abstract,
37 id: label,
38 shape_expr,
39 }
40 }
41
42 pub fn with_is_abstract(mut self, is_abstract: bool) -> Self {
43 self.is_abstract = is_abstract;
44 self
45 }
46
47 pub fn add_annotation(&mut self, annotation: Annotation) {
48 self.shape_expr.add_annotation(annotation);
49 }
50}
51
52impl Deref for ShapeDecl {
53 fn deref(
54 &self,
55 base: &Option<iri_s::IriS>,
56 prefixmap: &Option<prefixmap::PrefixMap>,
57 ) -> Result<Self, DerefError>
58 where
59 Self: Sized,
60 {
61 let id = self.id.deref(base, prefixmap)?;
62 let shape_expr = self.shape_expr.deref(base, prefixmap)?;
63 Ok(ShapeDecl {
64 type_: self.type_.clone(),
65 is_abstract: self.is_abstract,
66 id,
67 shape_expr,
68 })
69 }
70}