Add comments everywhere

This commit is contained in:
Pierre HUBERT 2023-02-03 09:48:58 +01:00
parent cc05d65eb9
commit 960d4318ec

View File

@ -29,28 +29,41 @@ fn expect_schema_object(s: &Schema) -> &SchemaObject {
} }
} }
/// The type of the schema
#[derive(Debug, Clone, serde::Serialize)] #[derive(Debug, Clone, serde::Serialize)]
#[serde(tag = "type")] #[serde(tag = "type")]
pub enum NodeType { pub enum NodeType {
/// NULL value
Null, Null,
/// BOOLEAN value
Boolean, Boolean,
/// Array value
Array { Array {
/// Schema information about the children of the array
item: Box<TreeNode>, item: Box<TreeNode>,
}, },
/// Object value
Object { Object {
required: Option<Vec<String>>, /// Required filed for the object
required: Option<Vec<String>>,*
/// The children of the object
children: Vec<TreeNode>, children: Vec<TreeNode>,
}, },
/// String value
String, String,
/// Number value
Number, Number,
/// Integer value
Integer, Integer,
} }
impl NodeType { impl NodeType {
/// Specify if the type of the node allow children
pub fn can_have_children(&self) -> bool { pub fn can_have_children(&self) -> bool {
matches!(self, NodeType::Object { .. } | NodeType::Array { .. }) matches!(self, NodeType::Object { .. } | NodeType::Array { .. })
} }
/// Get a short symbol representing the type of value
pub fn symbol(&self) -> &'static str { pub fn symbol(&self) -> &'static str {
match self { match self {
NodeType::Null => "NULL", NodeType::Null => "NULL",
@ -64,20 +77,28 @@ impl NodeType {
} }
} }
/// Parser schema structure node
#[derive(Debug, Clone, serde::Serialize)] #[derive(Debug, Clone, serde::Serialize)]
pub struct TreeNode { pub struct TreeNode {
/// The name of the schema
pub name: String, pub name: String,
/// The type of the schema
#[serde(flatten)] #[serde(flatten)]
pub r#type: NodeType, pub r#type: NodeType,
/// The description provided for the schema, if available
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>, pub description: Option<String>,
/// Examples values for the schema, if available
#[serde(skip_serializing_if = "Vec::is_empty")] #[serde(skip_serializing_if = "Vec::is_empty")]
pub examples: Vec<String>, pub examples: Vec<String>,
/// Discrete values of the schema, if specified
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub r#enum: Option<Vec<String>>, pub r#enum: Option<Vec<String>>,
} }
impl TreeNode { impl TreeNode {
/// Get the name of the schema, alongside with a symbol
/// indicating its type
pub fn print_name(&self) -> String { pub fn print_name(&self) -> String {
format!("{} {}", self.name, self.r#type.symbol()) format!("{} {}", self.name, self.r#type.symbol())
} }