Skip to content

Commit

Permalink
fix: Ensure that the pascal_case_name is evaluated at compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Westerlind committed Nov 23, 2021
1 parent 19b85cf commit 7337535
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/lib.rs
Expand Up @@ -19,18 +19,16 @@ able to parse any URI, such as `urn:isbn:0451450523`.
#[macro_use]
extern crate bitflags;

use serde::{Deserialize, Serialize};
use std::fmt::Debug;
pub use url::Url;

use std::collections::HashMap;
use std::{collections::HashMap, fmt::Debug};

use serde::de;
use serde::de::Error as Error_;
use serde::{de, de::Error as Error_, Deserialize, Serialize};
use serde_json::Value;
pub use url::Url;

const fn fmt_pascal_case_const(name: &str) -> ([u8; 128], usize) {
let mut buf = [0; 128];
// Large enough to contain any enumeration name defined in this crate
type PascalCaseBuf = [u8; 32];
const fn fmt_pascal_case_const(name: &str) -> (PascalCaseBuf, usize) {
let mut buf = [0; 32];
let mut buf_i = 0;
let mut name_i = 0;
let name = name.as_bytes();
Expand Down Expand Up @@ -90,9 +88,13 @@ macro_rules! lsp_enum {
impl std::convert::TryFrom<&str> for $typ {
type Error = &'static str;
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
match () {
$(
_ if { let (buf, len) = crate::fmt_pascal_case_const(stringify!($name)); &buf[..len] == value.as_bytes() } => Ok(Self::$name),
_ if {
const X: (crate::PascalCaseBuf, usize) = crate::fmt_pascal_case_const(stringify!($name));
let (buf, len) = X;
&buf[..len] == value.as_bytes()
} => Ok(Self::$name),
)*
_ => Err("unknown enum variant"),
}
Expand Down Expand Up @@ -706,10 +708,10 @@ pub struct ConfigurationItem {
}

mod url_map {
use super::*;

use std::fmt;

use super::*;

pub fn deserialize<'de, D>(
deserializer: D,
) -> Result<Option<HashMap<Url, Vec<TextEdit>>>, D::Error>
Expand Down Expand Up @@ -2453,9 +2455,10 @@ impl SymbolTag {

#[cfg(test)]
mod tests {
use super::*;
use serde::{Deserialize, Serialize};

use super::*;

pub(crate) fn test_serialization<SER>(ms: &SER, expected: &str)
where
SER: Serialize + for<'de> Deserialize<'de> + PartialEq + std::fmt::Debug,
Expand Down

0 comments on commit 7337535

Please sign in to comment.