summaryrefslogtreecommitdiffstats
path: root/vendor/rustversion/src/error.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/rustversion/src/error.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/rustversion/src/error.rs')
-rw-r--r--vendor/rustversion/src/error.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/vendor/rustversion/src/error.rs b/vendor/rustversion/src/error.rs
new file mode 100644
index 000000000..a99625b4a
--- /dev/null
+++ b/vendor/rustversion/src/error.rs
@@ -0,0 +1,56 @@
+use proc_macro::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
+use std::fmt::Display;
+use std::iter::FromIterator;
+
+pub type Result<T, E = Error> = std::result::Result<T, E>;
+
+pub struct Error {
+ begin: Span,
+ end: Span,
+ msg: String,
+}
+
+impl Error {
+ pub fn new(span: Span, msg: impl Display) -> Self {
+ Self::new2(span, span, msg)
+ }
+
+ pub fn new2(begin: Span, end: Span, msg: impl Display) -> Self {
+ Error {
+ begin,
+ end,
+ msg: msg.to_string(),
+ }
+ }
+
+ pub fn group(group: Group, msg: impl Display) -> Self {
+ let mut iter = group.stream().into_iter();
+ let delimiter = group.span();
+ let begin = iter.next().map_or(delimiter, |t| t.span());
+ let end = iter.last().map_or(begin, |t| t.span());
+ Self::new2(begin, end, msg)
+ }
+
+ pub fn into_compile_error(self) -> TokenStream {
+ // compile_error! { $msg }
+ TokenStream::from_iter(vec![
+ TokenTree::Ident(Ident::new("compile_error", self.begin)),
+ TokenTree::Punct({
+ let mut punct = Punct::new('!', Spacing::Alone);
+ punct.set_span(self.begin);
+ punct
+ }),
+ TokenTree::Group({
+ let mut group = Group::new(Delimiter::Brace, {
+ TokenStream::from_iter(vec![TokenTree::Literal({
+ let mut string = Literal::string(&self.msg);
+ string.set_span(self.end);
+ string
+ })])
+ });
+ group.set_span(self.end);
+ group
+ }),
+ ])
+ }
+}