1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use super::{Attribute, AttributeLike};
use crate::{Context, Error, StringRef};
use mlir_sys::{mlirStringAttrGet, mlirStringAttrGetValue, MlirAttribute};

/// A string attribute.
#[derive(Clone, Copy)]
pub struct StringAttribute<'c> {
    attribute: Attribute<'c>,
}

impl<'c> StringAttribute<'c> {
    /// Creates a string attribute.
    pub fn new(context: &'c Context, string: &str) -> Self {
        unsafe {
            Self::from_raw(mlirStringAttrGet(
                context.to_raw(),
                StringRef::new(string).to_raw(),
            ))
        }
    }

    /// Returns a value.
    pub fn value(&self) -> &'c str {
        unsafe { StringRef::from_raw(mlirStringAttrGetValue(self.to_raw())) }
            .as_str()
            .unwrap()
    }
}

attribute_traits!(StringAttribute, is_string, "string");

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test::create_test_context;

    #[test]
    fn value() {
        let context = create_test_context();
        let value = StringAttribute::new(&context, "foo").value();

        assert_eq!(value, "foo");
    }
}