Skip to main content

surrealguard_syntax/
source.rs

1//! Source identity primitives.
2
3use std::fmt;
4
5use serde::{Deserialize, Serialize};
6
7/// Stable identity for a source analyzed by SurrealGuard.
8///
9/// This may be a real file URI, an embedded-query virtual URI, or any adapter-owned
10/// identifier. Spans should never travel without a source id.
11#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
12pub struct SourceId(String);
13
14impl SourceId {
15    /// Wraps an identifier string as a source id.
16    pub fn new(id: impl Into<String>) -> Self {
17        Self(id.into())
18    }
19
20    /// The underlying identifier string.
21    pub fn as_str(&self) -> &str {
22        &self.0
23    }
24}
25
26impl fmt::Display for SourceId {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        f.write_str(&self.0)
29    }
30}
31
32impl From<&str> for SourceId {
33    fn from(value: &str) -> Self {
34        Self::new(value)
35    }
36}
37
38impl From<String> for SourceId {
39    fn from(value: String) -> Self {
40        Self::new(value)
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn source_id_preserves_input_text() {
50        let source = SourceId::new("file:///schema.surql");
51
52        assert_eq!(source.as_str(), "file:///schema.surql");
53        assert_eq!(source.to_string(), "file:///schema.surql");
54    }
55}