feat: implement StringLiteral macro
This commit is contained in:
11
string-literal/Cargo.toml
Normal file
11
string-literal/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "string-literal"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
license.workspace = true
|
||||
|
||||
[features]
|
||||
macros = ["dep:string-literal-macros"]
|
||||
|
||||
[dependencies]
|
||||
string-literal-macros = { optional = true, path = "../string-literal-macros" }
|
||||
24
string-literal/src/lib.rs
Normal file
24
string-literal/src/lib.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use std::fmt::{Debug, Display};
|
||||
|
||||
#[cfg(feature = "macros")]
|
||||
pub use string_literal_macros::StringLiteral;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct WrongLiteralError<S> {
|
||||
pub actual: String,
|
||||
pub expected: S,
|
||||
}
|
||||
|
||||
impl<S> Display for WrongLiteralError<S>
|
||||
where
|
||||
S: AsRef<str>,
|
||||
{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let Self { actual, expected } = self;
|
||||
let expected_str = expected.as_ref();
|
||||
|
||||
write!(f, "expected {expected_str:?} but got {actual:?}")
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> std::error::Error for WrongLiteralError<S> where WrongLiteralError<S>: Debug + Display {}
|
||||
Reference in New Issue
Block a user