-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Move PrimitiveType into dedicated module
- Loading branch information
1 parent
9a4f79a
commit 97b45d8
Showing
5 changed files
with
85 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use std::{convert::TryFrom, fmt}; | ||
|
||
/// For faster error handling in "type" keyword validator we have this enum, to match | ||
/// with it instead of a string. | ||
#[derive(Debug, Clone)] | ||
pub enum PrimitiveType { | ||
Array, | ||
Boolean, | ||
Integer, | ||
Null, | ||
Number, | ||
Object, | ||
String, | ||
} | ||
|
||
impl fmt::Display for PrimitiveType { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
PrimitiveType::Array => write!(f, "array"), | ||
PrimitiveType::Boolean => write!(f, "boolean"), | ||
PrimitiveType::Integer => write!(f, "integer"), | ||
PrimitiveType::Null => write!(f, "null"), | ||
PrimitiveType::Number => write!(f, "number"), | ||
PrimitiveType::Object => write!(f, "object"), | ||
PrimitiveType::String => write!(f, "string"), | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<&str> for PrimitiveType { | ||
type Error = (); | ||
|
||
fn try_from(value: &str) -> Result<Self, Self::Error> { | ||
match value { | ||
"array" => Ok(PrimitiveType::Array), | ||
"boolean" => Ok(PrimitiveType::Boolean), | ||
"integer" => Ok(PrimitiveType::Integer), | ||
"null" => Ok(PrimitiveType::Null), | ||
"number" => Ok(PrimitiveType::Number), | ||
"object" => Ok(PrimitiveType::Object), | ||
"string" => Ok(PrimitiveType::String), | ||
_ => Err(()), | ||
} | ||
} | ||
} |