Skip to main content

strop_grammar/query/
codec.rs

1//! Compilation is deterministic; native cancellation and program caches never
2//! cross the recording boundary. Replay reconstructs the checked query itself.
3use super::CompiledQuery;
4use serde::{Deserialize, Serialize};
5
6#[derive(Serialize)]
7struct QueryRef<'a> {
8    source: &'a str,
9    whole_word: bool,
10}
11#[derive(Deserialize)]
12struct QueryRecord {
13    source: String,
14    whole_word: bool,
15}
16impl Serialize for CompiledQuery {
17    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
18        QueryRef {
19            source: self.source(),
20            whole_word: self.whole_word(),
21        }
22        .serialize(serializer)
23    }
24}
25impl<'de> Deserialize<'de> for CompiledQuery {
26    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
27        let record = QueryRecord::deserialize(deserializer)?;
28        Self::compile(&record.source, record.whole_word).map_err(serde::de::Error::custom)
29    }
30}