shex_validation/
shex_config.rs1use iri_s::IriS;
2use serde::{Deserialize, Serialize};
3use srdf::RdfDataConfig;
4use std::io::Read;
5use std::path::Path;
6use thiserror::Error;
7
8use crate::ShExFormat;
9
10#[derive(Deserialize, Serialize, Debug, PartialEq, Clone, Default)]
12pub struct ShExConfigMain {
13 pub show_time: Option<bool>,
15
16 pub shex: Option<ShExConfig>,
18}
19
20impl ShExConfigMain {
21 pub fn from_path<P: AsRef<Path>>(path: P) -> Result<ShExConfigMain, ShExConfigError> {
23 let path_name = path.as_ref().display().to_string();
24 let mut f = std::fs::File::open(path).map_err(|e| ShExConfigError::FromPathError {
25 path: path_name.clone(),
26 error: e.to_string(),
27 })?;
28 let mut s = String::new();
29 f.read_to_string(&mut s)
30 .map_err(|e| ShExConfigError::FromPathError {
31 path: path_name.clone(),
32 error: e.to_string(),
33 })?;
34 let config: ShExConfigMain =
35 toml::from_str(s.as_str()).map_err(|e| ShExConfigError::TomlError {
36 path: path_name.clone(),
37 error: e.to_string(),
38 })?;
39 Ok(config)
40 }
41
42 pub fn shex_config(&self) -> ShExConfig {
43 match &self.shex {
44 None => ShExConfig::default(),
45 Some(sc) => sc.clone(),
46 }
47 }
48
49 pub fn show_imports(&self) -> bool {
50 match &self.shex {
51 None => false,
52 Some(sc) => sc.show_imports.unwrap_or(false),
53 }
54 }
55
56 pub fn show_extends(&self) -> bool {
57 match &self.shex {
58 None => false,
59 Some(sc) => sc.show_extends.unwrap_or(false),
60 }
61 }
62
63 pub fn show_shapes(&self) -> bool {
64 match &self.shex {
65 None => false,
66 Some(sc) => sc.show_shapes.unwrap_or(false),
67 }
68 }
69
70 pub fn set_show_extends(&mut self, flag: bool) {
71 match &mut self.shex {
72 None => self.shex = Some(ShExConfig::default().with_show_extends(flag)),
73 Some(sc) => sc.clone().set_show_extends(flag),
74 }
75 }
76}
77
78#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
80pub struct ShExConfig {
81 pub show_extends: Option<bool>,
83
84 pub show_imports: Option<bool>,
86
87 pub show_shapes: Option<bool>,
89
90 pub show_dependencies: Option<bool>,
92
93 pub show_ir: Option<bool>,
95
96 pub shex_format: Option<ShExFormat>,
98
99 pub check_well_formed: Option<bool>,
101
102 pub rdf_config_shex: Option<RdfDataConfig>,
104
105 pub base: Option<IriS>,
107}
108
109impl Default for ShExConfig {
110 fn default() -> Self {
111 Self {
112 show_extends: Some(true),
113 show_imports: Some(true),
114 show_shapes: Some(true),
115 show_dependencies: Some(true),
116 show_ir: Some(true),
117 check_well_formed: Some(true),
118 rdf_config_shex: Some(RdfDataConfig::default()),
119 shex_format: Some(ShExFormat::ShExC),
120 base: Some(IriS::new_unchecked("base://")),
121 }
122 }
123}
124
125impl ShExConfig {
126 pub fn rdf_config(&self) -> RdfDataConfig {
127 match &self.rdf_config_shex {
128 None => RdfDataConfig::default(),
129 Some(c) => c.clone(),
130 }
131 }
132
133 pub fn check_well_formed(&self) -> bool {
134 self.check_well_formed.unwrap_or(true)
135 }
136
137 pub fn with_show_extends(mut self, flag: bool) -> Self {
138 self.show_extends = Some(flag);
139 self
140 }
141
142 pub fn set_show_extends(mut self, flag: bool) {
143 self.show_extends = Some(flag);
144 }
145
146 pub fn with_show_imports(mut self, flag: bool) -> Self {
147 self.show_imports = Some(flag);
148 self
149 }
150
151 pub fn with_show_shapes(mut self, flag: bool) -> Self {
152 self.show_shapes = Some(flag);
153 self
154 }
155
156 pub fn with_show_dependencies(mut self, flag: bool) -> Self {
157 self.show_dependencies = Some(flag);
158 self
159 }
160}
161
162#[derive(Error, Debug, Clone)]
163pub enum ShExConfigError {
164 #[error("Error reading config file from path {path}: {error}")]
165 FromPathError { path: String, error: String },
166
167 #[error("Error reading config file from path {path}: {error}")]
168 TomlError { path: String, error: String },
169}