shape/
helpers.rs

1use super::Shape;
2
3/// [`Ref<T>`] is a placeholder for whichever reference counting wrapper type we
4/// want to define here ([`std::rc::Rc`] also works).
5///
6/// Besides allowing for cheap cloning and sharing of references to [`Shape`]
7/// subtrees, reference counting also paves the way for structural sharing of
8/// canonical shapes, which could have profound performance benefits.
9///
10/// Finally, Rust will complain about the [`Shape`] type referring to itself
11/// without indirection unless we introduce a wrapper type like [`Ref<T>`] to
12/// provide the indirection.
13pub(crate) type Ref<T> = std::sync::Arc<T>;
14
15/// Since we're using [`std::sync::Arc`] for reference counting, and [`Shape`]
16/// is an immutable structure, we can safely implement [`Send`] and [`Sync`] for
17/// [`Shape`].
18unsafe impl Sync for Shape {}
19unsafe impl Send for Shape {}
20
21/// Quote a string with double quotes and appropriate JSON escaping.
22pub(super) fn quote_string(s: &str) -> String {
23    serde_json_bytes::Value::String(s.into()).to_string()
24}
25
26pub(super) fn quote_non_identifier(s: &str) -> String {
27    if s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
28        s.to_string()
29    } else {
30        quote_string(s)
31    }
32}
33
34/// A [start..end) range of byte offsets within some source document.
35pub type OffsetRange = Option<std::ops::Range<usize>>;