calyx_utils/
pos_string.rs

1use std::{fmt::Display, path::PathBuf};
2
3use crate::{GPosIdx, WithPos};
4
5/// A positioned string.
6#[derive(Clone, Debug, Default)]
7pub struct PosString {
8    data: String,
9    span: GPosIdx,
10}
11
12impl From<String> for PosString {
13    fn from(data: String) -> PosString {
14        PosString {
15            data,
16            span: GPosIdx::UNKNOWN,
17        }
18    }
19}
20
21impl From<PosString> for String {
22    fn from(value: PosString) -> String {
23        value.data
24    }
25}
26
27impl From<PosString> for PathBuf {
28    fn from(value: PosString) -> Self {
29        value.data.into()
30    }
31}
32
33impl Display for PosString {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        self.data.fmt(f)
36    }
37}
38
39impl AsRef<str> for PosString {
40    fn as_ref(&self) -> &str {
41        &self.data
42    }
43}
44
45impl AsRef<std::path::Path> for PosString {
46    fn as_ref(&self) -> &std::path::Path {
47        self.data.as_ref()
48    }
49}
50
51impl WithPos for PosString {
52    fn copy_span(&self) -> GPosIdx {
53        self.span
54    }
55}
56
57impl PosString {
58    /// Construct a nw PosString from a String and a span.
59    pub fn new(data: String, span: GPosIdx) -> Self {
60        Self { data, span }
61    }
62
63    /// Add a span to an existing PosString.
64    pub fn with_span(mut self, span: GPosIdx) -> Self {
65        self.span = span;
66        self
67    }
68}