tauri_plugin_prevent_default/
script.rs

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