Skip to main content

databoard/
remapping_helper.rs

1// Copyright © 2025 Stephan Kunz
2//! Implements [`databoard`][`Remappings`] and helper functions handling the remapping rules.
3
4// region:		--- helpers
5/// Returns `true` if a key is a valid databoard key, otherwise `false`
6#[must_use]
7fn is_valid_db_key(key: &str) -> bool {
8	!key.contains('"') && !key.contains('\'') && !key.contains(':') && !key.contains('{') && !key.contains('}')
9}
10
11/// Returns `true` if a key is not a board pointer but a constant assignment , otherwise `false`
12#[must_use]
13pub fn is_const_assignment(key: &str) -> bool {
14	!key.starts_with('{') && !key.ends_with('}') || key.contains('"') || key.contains(':') || key.contains('\'')
15}
16
17/// Checks whether the given key is a pointer into a [`Databoard`](crate::databoard).
18#[must_use]
19pub fn is_board_pointer(key: &str) -> bool {
20	key.starts_with('{') && key.ends_with('}') && is_valid_db_key(&key[1..key.len() - 1])
21}
22
23/// Returns Some(literal) of the [`Databoard`](crate::databoard) pointer if it is one, otherwise `None`.
24#[must_use]
25pub fn strip_board_pointer(key: &str) -> Option<&str> {
26	if is_board_pointer(key) {
27		Some(&key[1..key.len() - 1])
28	} else {
29		None
30	}
31}
32
33/// Returns the literal of the [`Databoard`](crate::databoard) pointer if it is one.
34/// # Errors
35/// - if is not a [`Databoard`](crate::databoard) pointer, the error contains the unchanged key.
36pub fn check_board_pointer(key: &str) -> core::result::Result<&str, &str> {
37	if is_board_pointer(key) {
38		Ok(&key[1..key.len() - 1])
39	} else {
40		Err(key)
41	}
42}
43
44/// Returns the literal of the current/local [`Databoard`](crate::databoard) key if it is one.
45/// # Errors
46/// - if is not a current/local [`Databoard`](crate::databoard) `key`, the error contains the unchanged `key`.
47pub fn check_local_key(key: &str) -> core::result::Result<&str, &str> {
48	if key.starts_with('_') && is_valid_db_key(&key[1..]) {
49		Ok(&key[1..])
50	} else {
51		Err(key)
52	}
53}
54
55/// Checks whether the given key is a pointer into current/local [`Databoard`](crate::databoard).
56#[must_use]
57pub fn is_local_pointer(key: &str) -> bool {
58	key.starts_with("{_") && key.ends_with('}') && is_valid_db_key(&key[2..key.len() - 1])
59}
60
61/// Returns Some(literal) of the current/local [`Databoard`](crate::databoard) pointer if it is one, otherwise `None`.
62/// The leading `_` is removed from the literal.
63#[must_use]
64pub fn strip_local_pointer(key: &str) -> Option<&str> {
65	if is_local_pointer(key) {
66		Some(&key[2..key.len() - 1])
67	} else {
68		None
69	}
70}
71
72/// Returns the literal of the current/local [`Databoard`](crate::databoard) pointer if it is one.
73/// # Errors
74/// - if is not a current/local [`Databoard`](crate::databoard) pointer, the error contains the unchanged key.
75pub fn check_local_pointer(key: &str) -> core::result::Result<&str, &str> {
76	if is_local_pointer(key) {
77		Ok(&key[2..key.len() - 1])
78	} else {
79		Err(key)
80	}
81}
82
83/// Returns the literal of the top level [`Databoard`](crate::databoard) key if it is one.
84/// # Errors
85/// - if is not a top level [`Databoard`](crate::databoard) `key`, the error contains the unchanged `key`.
86pub fn check_top_level_key(key: &str) -> core::result::Result<&str, &str> {
87	if key.starts_with('@') && is_valid_db_key(&key[1..]) {
88		Ok(&key[1..])
89	} else {
90		Err(key)
91	}
92}
93
94/// Checks whether the given key is a pointer into top level [`Databoard`](crate::databoard).
95#[must_use]
96pub fn is_top_level_pointer(key: &str) -> bool {
97	key.starts_with("{@") && key.ends_with('}') && is_valid_db_key(&key[2..key.len() - 1])
98}
99
100/// Returns Some(literal) of the top level [`Databoard`](crate::databoard) pointer if it is one, otherwise `None`.
101/// The leading `@` is removed from the literal.
102#[must_use]
103pub fn strip_top_level_pointer(key: &str) -> Option<&str> {
104	if is_top_level_pointer(key) {
105		Some(&key[2..key.len() - 1])
106	} else {
107		None
108	}
109}
110
111/// Returns the literal of the top level [`Databoard`](crate::databoard) pointer if it is one.
112/// # Errors
113/// - if is not a top level [`Databoard`](crate::databoard) pointer, the error contains the unchanged pointer.
114pub fn check_top_level_pointer(key: &str) -> core::result::Result<&str, &str> {
115	if is_top_level_pointer(key) {
116		Ok(&key[2..key.len() - 1])
117	} else {
118		Err(key)
119	}
120}
121
122// #[cfg(test)]
123// mod tests {
124// 	use super::*;
125
126// 	// check, that the auto traits are available
127// 	const fn is_normal<T: Sized + Send + Sync>() {}
128
129// 	#[test]
130// 	const fn normal_types() {
131// 		is_normal::<RemappingList>();
132// 		is_normal::<RemappingEntry>();
133// 	}
134// }