skia_safe/modules/svg/types/
iri.rs

1use std::fmt;
2
3use crate::{
4    interop::{self, AsStr},
5    prelude::*,
6};
7use skia_bindings as sb;
8
9pub type IriKind = sb::SkSVGIRI_Type;
10pub type IriFuncKind = sb::SkSVGFuncIRI_Type;
11
12pub type Iri = Handle<sb::SkSVGIRI>;
13
14impl NativeDrop for sb::SkSVGIRI {
15    fn drop(&mut self) {
16        unsafe { sb::C_SkSVGIRI_destruct(self) }
17    }
18}
19
20impl Default for Iri {
21    fn default() -> Self {
22        Self::construct(|uninitialized| unsafe { sb::C_SkSVGIRI_Construct(uninitialized) })
23    }
24}
25
26impl fmt::Debug for Iri {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        f.debug_struct("SvgIri")
29            .field("data", &self.data())
30            .finish()
31    }
32}
33
34impl Iri {
35    pub fn data(&self) -> &str {
36        self.native().fIRI.as_str()
37    }
38
39    pub fn new<T: AsRef<str>>(value: T, kind: IriKind) -> Self {
40        Self::construct(|uninitialized| unsafe {
41            let iri = interop::String::from_str(value.as_ref());
42
43            sb::C_SkSVGIRI_Construct1(uninitialized, kind, iri.native())
44        })
45    }
46}
47
48pub type IriFunc = Handle<sb::SkSVGFuncIRI>;
49
50impl NativeDrop for sb::SkSVGFuncIRI {
51    fn drop(&mut self) {
52        unsafe { sb::C_SkSVGFuncIRI_destruct(self) }
53    }
54}
55
56impl fmt::Debug for IriFunc {
57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58        f.debug_struct("SvgIriFunc")
59            .field("kind", &self.kind())
60            .field("iri", &self.iri())
61            .finish()
62    }
63}
64
65impl IriFunc {
66    pub fn iri(&self) -> Option<&Iri> {
67        let func = self.native();
68
69        if matches!(func.fType, IriFuncKind::IRI) {
70            Some(Iri::from_native_ref(&self.native().fIRI))
71        } else {
72            None
73        }
74    }
75
76    pub fn kind(&self) -> IriFuncKind {
77        self.native().fType
78    }
79
80    pub fn from_iri(value: Iri) -> Self {
81        Self::construct(|uninitialized| unsafe {
82            sb::C_SkSVGFuncIRI_Construct1(uninitialized, value.native())
83        })
84    }
85
86    pub fn none() -> Self {
87        Self::construct(|uninitialized| unsafe { sb::C_SkSVGFuncIRI_Construct(uninitialized) })
88    }
89}