-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1062 from godot-rust/feature/strings-from-cstr
`GString`, `StringName`: add conversions from bytes and C-strings
- Loading branch information
Showing
10 changed files
with
419 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright (c) godot-rust; Bromeon and contributors. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
use std::error::Error; | ||
use std::fmt; | ||
|
||
/// Error related to string encoding/decoding. | ||
#[derive(Debug)] | ||
pub struct StringError { | ||
message: String, | ||
source: Option<Box<(dyn Error + 'static)>>, | ||
} | ||
|
||
impl fmt::Display for StringError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
if let Some(source) = self.source() { | ||
write!(f, "{}: {}", self.message, source) | ||
} else { | ||
write!(f, "{}", self.message) | ||
} | ||
} | ||
} | ||
|
||
impl Error for StringError { | ||
fn source(&self) -> Option<&(dyn Error + 'static)> { | ||
self.source.as_deref() | ||
} | ||
} | ||
|
||
impl StringError { | ||
pub(crate) fn new(message: impl Into<String>) -> Self { | ||
Self { | ||
message: message.into(), | ||
source: None, | ||
} | ||
} | ||
|
||
pub(crate) fn with_source( | ||
message: impl Into<String>, | ||
source: impl Into<Box<(dyn Error + 'static)>>, | ||
) -> Self { | ||
Self { | ||
message: message.into(), | ||
source: Some(source.into()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.