tauri_plugin_prevent_default/
script.rs1use std::fmt;
2use std::ops::{Add, AddAssign};
3use std::sync::Arc;
4
5#[derive(Debug)]
7pub struct Script(Arc<str>);
8
9impl Script {
10 pub fn new(script: impl AsRef<str>) -> Self {
11 Self(Arc::from(script.as_ref()))
12 }
13
14 #[must_use]
15 pub fn join(&self, script: impl AsRef<str>) -> Self {
16 let script = script.as_ref();
17 let capacity = self.0.len().saturating_add(script.len());
18 let mut buf = String::with_capacity(capacity);
19 buf.push_str(&self.0);
20 buf.push('\n');
21 buf.push_str(script);
22 Self::from(buf)
23 }
24}
25
26impl AsRef<str> for Script {
27 fn as_ref(&self) -> &str {
28 &self.0
29 }
30}
31
32impl Clone for Script {
33 fn clone(&self) -> Self {
34 Self(Arc::clone(&self.0))
35 }
36}
37
38impl From<&str> for Script {
39 fn from(value: &str) -> Self {
40 Script(Arc::from(value))
41 }
42}
43
44impl From<String> for Script {
45 fn from(value: String) -> Self {
46 Script(Arc::from(value))
47 }
48}
49
50impl From<&Script> for String {
51 fn from(value: &Script) -> Self {
52 String::from(value.0.as_ref())
53 }
54}
55
56impl From<Script> for String {
57 fn from(value: Script) -> Self {
58 <Self as From<&Script>>::from(&value)
59 }
60}
61
62impl From<&Script> for Vec<u8> {
63 fn from(value: &Script) -> Self {
64 value.0.as_bytes().to_vec()
65 }
66}
67
68impl From<Script> for Vec<u8> {
69 fn from(value: Script) -> Self {
70 <Self as From<&Script>>::from(&value)
71 }
72}
73
74impl From<&Script> for Vec<u16> {
75 fn from(value: &Script) -> Self {
76 value
77 .0
78 .as_bytes()
79 .iter()
80 .copied()
81 .map(u16::from)
82 .collect()
83 }
84}
85
86impl From<Script> for Vec<u16> {
87 fn from(value: Script) -> Self {
88 <Self as From<&Script>>::from(&value)
89 }
90}
91
92impl<T: AsRef<str>> Add<T> for Script {
93 type Output = Script;
94
95 fn add(self, rhs: T) -> Self::Output {
96 self.join(rhs)
97 }
98}
99
100impl<T: AsRef<str>> AddAssign<T> for Script {
101 fn add_assign(&mut self, other: T) {
102 *self = self.join(other);
103 }
104}
105
106impl fmt::Display for Script {
107 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108 write!(f, "{}", self.0)
109 }
110}