miniextendr_api/from_r/
strings.rs1use crate::SEXP;
26use crate::from_r::{
27 SexpError, TryFromSexp, is_null_or_plain_na, is_null_or_plain_na_unchecked, scalar_charsxp,
28 scalar_charsxp_unchecked, try_charsxp_to_str, try_charsxp_to_str_unchecked,
29};
30
31impl TryFromSexp for &'static str {
51 type Error = SexpError;
52 const CHARACTER_ONLY: bool = true;
53
54 #[inline]
55 fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
56 let charsxp = scalar_charsxp(sexp)?;
57
58 if charsxp == SEXP::na_string() {
60 return Ok("");
61 }
62 if charsxp == SEXP::blank_string() {
63 return Ok("");
64 }
65
66 unsafe { try_charsxp_to_str(charsxp) }
68 }
69
70 unsafe fn __mx_try_from_rooted(root: crate::Root<'_>) -> Result<Self, Self::Error> {
71 let charsxp = scalar_charsxp(root.get())?;
72 if charsxp == SEXP::na_string() || charsxp == SEXP::blank_string() {
73 return Ok("");
74 }
75 let charsxp = unsafe { root.protect_child(charsxp) }.get();
76 unsafe { try_charsxp_to_str(charsxp) }
77 }
78
79 #[inline]
80 unsafe fn try_from_sexp_unchecked(sexp: SEXP) -> Result<Self, Self::Error> {
81 let charsxp = unsafe { scalar_charsxp_unchecked(sexp)? };
82
83 if charsxp == SEXP::na_string() {
85 return Ok("");
86 }
87 if charsxp == SEXP::blank_string() {
88 return Ok("");
89 }
90
91 unsafe { try_charsxp_to_str_unchecked(charsxp) }
93 }
94}
95
96impl TryFromSexp for Option<&'static str> {
98 type Error = SexpError;
99 const CHARACTER_ONLY: bool = true;
100
101 #[inline]
102 fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
103 if is_null_or_plain_na(sexp) {
104 return Ok(None);
105 }
106
107 let charsxp = scalar_charsxp(sexp)?;
108 if charsxp == SEXP::na_string() {
109 return Ok(None);
110 }
111 if charsxp == SEXP::blank_string() {
112 return Ok(Some(""));
113 }
114
115 unsafe { try_charsxp_to_str(charsxp) }.map(Some)
116 }
117
118 unsafe fn __mx_try_from_rooted(root: crate::Root<'_>) -> Result<Self, Self::Error> {
119 if is_null_or_plain_na(root.get()) {
120 return Ok(None);
121 }
122 let charsxp = scalar_charsxp(root.get())?;
123 if charsxp == SEXP::na_string() {
124 return Ok(None);
125 }
126 if charsxp == SEXP::blank_string() {
127 return Ok(Some(""));
128 }
129 let charsxp = unsafe { root.protect_child(charsxp) }.get();
130 unsafe { try_charsxp_to_str(charsxp) }.map(Some)
131 }
132
133 #[inline]
134 unsafe fn try_from_sexp_unchecked(sexp: SEXP) -> Result<Self, Self::Error> {
135 if unsafe { is_null_or_plain_na_unchecked(sexp) } {
136 return Ok(None);
137 }
138
139 let charsxp = unsafe { scalar_charsxp_unchecked(sexp)? };
140 if charsxp == SEXP::na_string() {
141 return Ok(None);
142 }
143 if charsxp == SEXP::blank_string() {
144 return Ok(Some(""));
145 }
146
147 unsafe { try_charsxp_to_str_unchecked(charsxp) }.map(Some)
148 }
149}
150
151impl TryFromSexp for char {
156 type Error = SexpError;
157 const CHARACTER_ONLY: bool = true;
158
159 #[inline]
160 fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
161 let s: &str = TryFromSexp::try_from_sexp(sexp)?;
162 let mut chars = s.chars();
163 match (chars.next(), chars.next()) {
164 (Some(c), None) => Ok(c),
165 (None, _) => Err(SexpError::InvalidValue(
166 "empty string cannot be converted to char".to_string(),
167 )),
168 (Some(_), Some(_)) => Err(SexpError::InvalidValue(
169 "string has more than one character".to_string(),
170 )),
171 }
172 }
173
174 #[inline]
175 unsafe fn try_from_sexp_unchecked(sexp: SEXP) -> Result<Self, Self::Error> {
176 let s: &str = unsafe { TryFromSexp::try_from_sexp_unchecked(sexp)? };
177 let mut chars = s.chars();
178 match (chars.next(), chars.next()) {
179 (Some(c), None) => Ok(c),
180 (None, _) => Err(SexpError::InvalidValue(
181 "empty string cannot be converted to char".to_string(),
182 )),
183 (Some(_), Some(_)) => Err(SexpError::InvalidValue(
184 "string has more than one character".to_string(),
185 )),
186 }
187 }
188}
189
190impl TryFromSexp for String {
203 type Error = SexpError;
204 const CHARACTER_ONLY: bool = true;
205
206 #[inline]
207 fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
208 let charsxp = scalar_charsxp(sexp)?;
209
210 if charsxp == SEXP::na_string() {
211 return Ok(String::new());
212 }
213
214 unsafe { try_charsxp_to_str(charsxp) }.map(str::to_owned)
215 }
216
217 #[inline]
218 unsafe fn try_from_sexp_unchecked(sexp: SEXP) -> Result<Self, Self::Error> {
219 let charsxp = unsafe { scalar_charsxp_unchecked(sexp)? };
220
221 if charsxp == SEXP::na_string() {
222 return Ok(String::new());
223 }
224
225 unsafe { try_charsxp_to_str_unchecked(charsxp) }.map(str::to_owned)
226 }
227}
228
229impl TryFromSexp for Option<String> {
241 type Error = SexpError;
242 const CHARACTER_ONLY: bool = true;
243
244 #[inline]
245 fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
246 if is_null_or_plain_na(sexp) {
247 return Ok(None);
248 }
249
250 let charsxp = scalar_charsxp(sexp)?;
251
252 if charsxp == SEXP::na_string() {
254 return Ok(None);
255 }
256
257 unsafe { try_charsxp_to_str(charsxp) }.map(|s| Some(s.to_owned()))
258 }
259
260 #[inline]
261 unsafe fn try_from_sexp_unchecked(sexp: SEXP) -> Result<Self, Self::Error> {
262 Self::try_from_sexp(sexp)
264 }
265}
266macro_rules! impl_owned_str_from_r {
273 ($owner:ident) => {
274 impl TryFromSexp for $owner<str> {
275 type Error = SexpError;
276 const CHARACTER_ONLY: bool = true;
277 fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
278 String::try_from_sexp(sexp).map(Self::from)
279 }
280 unsafe fn try_from_sexp_unchecked(sexp: SEXP) -> Result<Self, Self::Error> {
281 unsafe { String::try_from_sexp_unchecked(sexp) }.map(Self::from)
282 }
283 }
284 impl TryFromSexp for Option<$owner<str>> {
285 type Error = SexpError;
286 const CHARACTER_ONLY: bool = true;
287 fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
288 Option::<String>::try_from_sexp(sexp).map(|v| v.map($owner::from))
289 }
290 unsafe fn try_from_sexp_unchecked(sexp: SEXP) -> Result<Self, Self::Error> {
291 unsafe { Option::<String>::try_from_sexp_unchecked(sexp) }
292 .map(|v| v.map($owner::from))
293 }
294 }
295 impl TryFromSexp for Vec<$owner<str>> {
296 type Error = SexpError;
297 const CHARACTER_ONLY: bool = true;
298 fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
299 Vec::<String>::try_from_sexp(sexp)
300 .map(|v| v.into_iter().map($owner::from).collect())
301 }
302 unsafe fn try_from_sexp_unchecked(sexp: SEXP) -> Result<Self, Self::Error> {
303 unsafe { Vec::<String>::try_from_sexp_unchecked(sexp) }
304 .map(|v| v.into_iter().map($owner::from).collect())
305 }
306 }
307 impl TryFromSexp for Vec<Option<$owner<str>>> {
308 type Error = SexpError;
309 const CHARACTER_ONLY: bool = true;
310 fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
311 Vec::<Option<String>>::try_from_sexp(sexp)
312 .map(|v| v.into_iter().map(|s| s.map($owner::from)).collect())
313 }
314 unsafe fn try_from_sexp_unchecked(sexp: SEXP) -> Result<Self, Self::Error> {
315 unsafe { Vec::<Option<String>>::try_from_sexp_unchecked(sexp) }
316 .map(|v| v.into_iter().map(|s| s.map($owner::from)).collect())
317 }
318 }
319 };
320}
321use std::rc::Rc;
322use std::sync::Arc;
323impl TryFromSexp for Box<str> {
type Error = SexpError;
const CHARACTER_ONLY: bool = true;
fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
String::try_from_sexp(sexp).map(Self::from)
}
unsafe fn try_from_sexp_unchecked(sexp: SEXP)
-> Result<Self, Self::Error> {
unsafe { String::try_from_sexp_unchecked(sexp) }.map(Self::from)
}
}
impl TryFromSexp for Option<Box<str>> {
type Error = SexpError;
const CHARACTER_ONLY: bool = true;
fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
Option::<String>::try_from_sexp(sexp).map(|v| v.map(Box::from))
}
unsafe fn try_from_sexp_unchecked(sexp: SEXP)
-> Result<Self, Self::Error> {
unsafe {
Option::<String>::try_from_sexp_unchecked(sexp)
}.map(|v| v.map(Box::from))
}
}
impl TryFromSexp for Vec<Box<str>> {
type Error = SexpError;
const CHARACTER_ONLY: bool = true;
fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
Vec::<String>::try_from_sexp(sexp).map(|v|
v.into_iter().map(Box::from).collect())
}
unsafe fn try_from_sexp_unchecked(sexp: SEXP)
-> Result<Self, Self::Error> {
unsafe {
Vec::<String>::try_from_sexp_unchecked(sexp)
}.map(|v| v.into_iter().map(Box::from).collect())
}
}
impl TryFromSexp for Vec<Option<Box<str>>> {
type Error = SexpError;
const CHARACTER_ONLY: bool = true;
fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
Vec::<Option<String>>::try_from_sexp(sexp).map(|v|
v.into_iter().map(|s| s.map(Box::from)).collect())
}
unsafe fn try_from_sexp_unchecked(sexp: SEXP)
-> Result<Self, Self::Error> {
unsafe {
Vec::<Option<String>>::try_from_sexp_unchecked(sexp)
}.map(|v| v.into_iter().map(|s| s.map(Box::from)).collect())
}
}impl_owned_str_from_r!(Box);
324impl TryFromSexp for Rc<str> {
type Error = SexpError;
const CHARACTER_ONLY: bool = true;
fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
String::try_from_sexp(sexp).map(Self::from)
}
unsafe fn try_from_sexp_unchecked(sexp: SEXP)
-> Result<Self, Self::Error> {
unsafe { String::try_from_sexp_unchecked(sexp) }.map(Self::from)
}
}
impl TryFromSexp for Option<Rc<str>> {
type Error = SexpError;
const CHARACTER_ONLY: bool = true;
fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
Option::<String>::try_from_sexp(sexp).map(|v| v.map(Rc::from))
}
unsafe fn try_from_sexp_unchecked(sexp: SEXP)
-> Result<Self, Self::Error> {
unsafe {
Option::<String>::try_from_sexp_unchecked(sexp)
}.map(|v| v.map(Rc::from))
}
}
impl TryFromSexp for Vec<Rc<str>> {
type Error = SexpError;
const CHARACTER_ONLY: bool = true;
fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
Vec::<String>::try_from_sexp(sexp).map(|v|
v.into_iter().map(Rc::from).collect())
}
unsafe fn try_from_sexp_unchecked(sexp: SEXP)
-> Result<Self, Self::Error> {
unsafe {
Vec::<String>::try_from_sexp_unchecked(sexp)
}.map(|v| v.into_iter().map(Rc::from).collect())
}
}
impl TryFromSexp for Vec<Option<Rc<str>>> {
type Error = SexpError;
const CHARACTER_ONLY: bool = true;
fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
Vec::<Option<String>>::try_from_sexp(sexp).map(|v|
v.into_iter().map(|s| s.map(Rc::from)).collect())
}
unsafe fn try_from_sexp_unchecked(sexp: SEXP)
-> Result<Self, Self::Error> {
unsafe {
Vec::<Option<String>>::try_from_sexp_unchecked(sexp)
}.map(|v| v.into_iter().map(|s| s.map(Rc::from)).collect())
}
}impl_owned_str_from_r!(Rc);
325impl TryFromSexp for Arc<str> {
type Error = SexpError;
const CHARACTER_ONLY: bool = true;
fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
String::try_from_sexp(sexp).map(Self::from)
}
unsafe fn try_from_sexp_unchecked(sexp: SEXP)
-> Result<Self, Self::Error> {
unsafe { String::try_from_sexp_unchecked(sexp) }.map(Self::from)
}
}
impl TryFromSexp for Option<Arc<str>> {
type Error = SexpError;
const CHARACTER_ONLY: bool = true;
fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
Option::<String>::try_from_sexp(sexp).map(|v| v.map(Arc::from))
}
unsafe fn try_from_sexp_unchecked(sexp: SEXP)
-> Result<Self, Self::Error> {
unsafe {
Option::<String>::try_from_sexp_unchecked(sexp)
}.map(|v| v.map(Arc::from))
}
}
impl TryFromSexp for Vec<Arc<str>> {
type Error = SexpError;
const CHARACTER_ONLY: bool = true;
fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
Vec::<String>::try_from_sexp(sexp).map(|v|
v.into_iter().map(Arc::from).collect())
}
unsafe fn try_from_sexp_unchecked(sexp: SEXP)
-> Result<Self, Self::Error> {
unsafe {
Vec::<String>::try_from_sexp_unchecked(sexp)
}.map(|v| v.into_iter().map(Arc::from).collect())
}
}
impl TryFromSexp for Vec<Option<Arc<str>>> {
type Error = SexpError;
const CHARACTER_ONLY: bool = true;
fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
Vec::<Option<String>>::try_from_sexp(sexp).map(|v|
v.into_iter().map(|s| s.map(Arc::from)).collect())
}
unsafe fn try_from_sexp_unchecked(sexp: SEXP)
-> Result<Self, Self::Error> {
unsafe {
Vec::<Option<String>>::try_from_sexp_unchecked(sexp)
}.map(|v| v.into_iter().map(|s| s.map(Arc::from)).collect())
}
}impl_owned_str_from_r!(Arc);
326