diff --git a/packages/cli/src/gen/doc2/builder.rs b/packages/cli/src/gen/definitions/builder.rs similarity index 78% rename from packages/cli/src/gen/doc2/builder.rs rename to packages/cli/src/gen/definitions/builder.rs index dd5beee..ac80313 100644 --- a/packages/cli/src/gen/doc2/builder.rs +++ b/packages/cli/src/gen/definitions/builder.rs @@ -1,20 +1,20 @@ use anyhow::{bail, Result}; -use super::{item::DocItem, kind::DocItemKind}; +use super::{item::DefinitionsItem, kind::DefinitionsItemKind}; #[derive(Debug, Default, Clone)] -pub struct DocItemBuilder { +pub struct DefinitionsItemBuilder { exported: bool, - kind: Option, + kind: Option, name: Option, meta: Option, value: Option, - children: Vec, + children: Vec, arg_types: Vec, } #[allow(dead_code)] -impl DocItemBuilder { +impl DefinitionsItemBuilder { pub fn new() -> Self { Self { ..Default::default() @@ -27,7 +27,7 @@ impl DocItemBuilder { self } - pub fn with_kind(mut self, kind: DocItemKind) -> Self { + pub fn with_kind(mut self, kind: DefinitionsItemKind) -> Self { self.kind = Some(kind); self } @@ -47,12 +47,12 @@ impl DocItemBuilder { self } - pub fn with_child(mut self, child: DocItem) -> Self { + pub fn with_child(mut self, child: DefinitionsItem) -> Self { self.children.push(child); self } - pub fn with_children(mut self, children: &[DocItem]) -> Self { + pub fn with_children(mut self, children: &[DefinitionsItem]) -> Self { self.children.extend_from_slice(children); self } @@ -69,11 +69,11 @@ impl DocItemBuilder { self } - pub fn build(self) -> Result { + pub fn build(self) -> Result { if let Some(kind) = self.kind { let mut children = self.children; children.sort(); - Ok(DocItem { + Ok(DefinitionsItem { exported: self.exported, kind, name: self.name, diff --git a/packages/cli/src/gen/doc2/item.rs b/packages/cli/src/gen/definitions/item.rs similarity index 88% rename from packages/cli/src/gen/doc2/item.rs rename to packages/cli/src/gen/definitions/item.rs index 78dda6f..2ca5473 100644 --- a/packages/cli/src/gen/doc2/item.rs +++ b/packages/cli/src/gen/definitions/item.rs @@ -2,14 +2,14 @@ use std::cmp::Ordering; use serde::{Deserialize, Serialize}; -use super::kind::DocItemKind; +use super::kind::DefinitionsItemKind; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] -pub struct DocItem { +pub struct DefinitionsItem { #[serde(skip_serializing_if = "skip_serialize_is_false")] pub(super) exported: bool, - pub(super) kind: DocItemKind, + pub(super) kind: DefinitionsItemKind, #[serde(skip_serializing_if = "Option::is_none")] pub(super) name: Option, #[serde(skip_serializing_if = "Option::is_none")] @@ -17,7 +17,7 @@ pub struct DocItem { #[serde(skip_serializing_if = "Option::is_none")] pub(super) value: Option, #[serde(skip_serializing_if = "Vec::is_empty")] - pub(super) children: Vec, + pub(super) children: Vec, #[serde(skip_serializing_if = "Vec::is_empty")] pub(super) arg_types: Vec, } @@ -27,7 +27,7 @@ fn skip_serialize_is_false(b: &bool) -> bool { !b } -impl PartialOrd for DocItem { +impl PartialOrd for DefinitionsItem { fn partial_cmp(&self, other: &Self) -> Option { match self.kind.partial_cmp(&other.kind).unwrap() { Ordering::Equal => {} @@ -52,14 +52,14 @@ impl PartialOrd for DocItem { } } -impl Ord for DocItem { +impl Ord for DefinitionsItem { fn cmp(&self, other: &Self) -> Ordering { self.partial_cmp(other).unwrap() } } #[allow(dead_code)] -impl DocItem { +impl DefinitionsItem { pub fn is_exported(&self) -> bool { self.exported } @@ -88,7 +88,7 @@ impl DocItem { self.kind.is_tag() } - pub fn kind(&self) -> DocItemKind { + pub fn kind(&self) -> DefinitionsItemKind { self.kind } @@ -104,7 +104,7 @@ impl DocItem { self.value.as_deref() } - pub fn children(&self) -> &[DocItem] { + pub fn children(&self) -> &[DefinitionsItem] { &self.children } diff --git a/packages/cli/src/gen/doc2/kind.rs b/packages/cli/src/gen/definitions/kind.rs similarity index 72% rename from packages/cli/src/gen/doc2/kind.rs rename to packages/cli/src/gen/definitions/kind.rs index 59a690e..3bc2015 100644 --- a/packages/cli/src/gen/doc2/kind.rs +++ b/packages/cli/src/gen/definitions/kind.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[serde(rename_all = "PascalCase")] -pub enum DocItemKind { +pub enum DefinitionsItemKind { Root, Table, Property, @@ -14,33 +14,33 @@ pub enum DocItemKind { } #[allow(dead_code)] -impl DocItemKind { +impl DefinitionsItemKind { pub fn is_root(self) -> bool { - self == DocItemKind::Root + self == DefinitionsItemKind::Root } pub fn is_table(self) -> bool { - self == DocItemKind::Table + self == DefinitionsItemKind::Table } pub fn is_property(self) -> bool { - self == DocItemKind::Property + self == DefinitionsItemKind::Property } pub fn is_function(self) -> bool { - self == DocItemKind::Function + self == DefinitionsItemKind::Function } pub fn is_description(self) -> bool { - self == DocItemKind::Description + self == DefinitionsItemKind::Description } pub fn is_tag(self) -> bool { - self == DocItemKind::Tag + self == DefinitionsItemKind::Tag } } -impl fmt::Display for DocItemKind { +impl fmt::Display for DefinitionsItemKind { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, diff --git a/packages/cli/src/gen/definitions/mod.rs b/packages/cli/src/gen/definitions/mod.rs new file mode 100644 index 0000000..020458f --- /dev/null +++ b/packages/cli/src/gen/definitions/mod.rs @@ -0,0 +1,13 @@ +mod builder; +mod item; +mod kind; +mod moonwave; +mod parser; +mod tag; +mod tree; + +pub use item::DefinitionsItem; +pub use kind::DefinitionsItemKind; +pub use parser::PIPE_SEPARATOR; +pub use tag::DefinitionsItemTag; +pub use tree::DefinitionsTree; diff --git a/packages/cli/src/gen/doc2/moonwave.rs b/packages/cli/src/gen/definitions/moonwave.rs similarity index 85% rename from packages/cli/src/gen/doc2/moonwave.rs rename to packages/cli/src/gen/definitions/moonwave.rs index 69e8d8a..9d5870d 100644 --- a/packages/cli/src/gen/doc2/moonwave.rs +++ b/packages/cli/src/gen/definitions/moonwave.rs @@ -1,12 +1,12 @@ use regex::Regex; -use super::{builder::DocItemBuilder, item::DocItem, kind::DocItemKind}; +use super::{builder::DefinitionsItemBuilder, item::DefinitionsItem, kind::DefinitionsItemKind}; fn should_separate_tag_meta(tag_kind: &str) -> bool { matches!(tag_kind.trim().to_ascii_lowercase().as_ref(), "param") } -fn parse_moonwave_style_tag(line: &str) -> Option { +fn parse_moonwave_style_tag(line: &str) -> Option { let tag_regex = Regex::new(r#"^@(\S+)\s*(.*)$"#).unwrap(); if tag_regex.is_match(line) { let captures = tag_regex.captures(line).unwrap(); @@ -22,8 +22,8 @@ fn parse_moonwave_style_tag(line: &str) -> Option { if tag_kind.is_empty() { None } else { - let mut builder = DocItemBuilder::new() - .with_kind(DocItemKind::Tag) + let mut builder = DefinitionsItemBuilder::new() + .with_kind(DefinitionsItemKind::Tag) .with_name(tag_kind); if !tag_name.is_empty() { builder = builder.with_meta(tag_name); @@ -38,7 +38,7 @@ fn parse_moonwave_style_tag(line: &str) -> Option { } } -pub(super) fn parse_moonwave_style_comment(comment: &str) -> Vec { +pub(super) fn parse_moonwave_style_comment(comment: &str) -> Vec { let lines = comment.lines().map(str::trim).collect::>(); let indent_len = lines.iter().fold(usize::MAX, |acc, line| { let first = line.chars().enumerate().find_map(|(idx, ch)| { @@ -70,8 +70,8 @@ pub(super) fn parse_moonwave_style_comment(comment: &str) -> Vec { } if !doc_lines.is_empty() { doc_items.push( - DocItemBuilder::new() - .with_kind(DocItemKind::Description) + DefinitionsItemBuilder::new() + .with_kind(DefinitionsItemKind::Description) .with_value(doc_lines.join("\n").trim()) .build() .unwrap(), diff --git a/packages/cli/src/gen/doc2/parser.rs b/packages/cli/src/gen/definitions/parser.rs similarity index 94% rename from packages/cli/src/gen/doc2/parser.rs rename to packages/cli/src/gen/definitions/parser.rs index e907366..0b36a55 100644 --- a/packages/cli/src/gen/doc2/parser.rs +++ b/packages/cli/src/gen/definitions/parser.rs @@ -9,23 +9,23 @@ use full_moon::{ use regex::Regex; use super::{ - builder::DocItemBuilder, item::DocItem, kind::DocItemKind, + builder::DefinitionsItemBuilder, item::DefinitionsItem, kind::DefinitionsItemKind, moonwave::parse_moonwave_style_comment, }; pub const PIPE_SEPARATOR: &str = " | "; #[derive(Debug, Clone)] -struct DocVisitorItem { +struct DefinitionsParserItem { name: String, comment: Option, type_info: TypeInfo, } -impl DocVisitorItem { - fn into_doc_item(self, type_definition_declares: &Vec) -> DocItem { - let mut builder = DocItemBuilder::new() - .with_kind(DocItemKind::from(&self.type_info)) +impl DefinitionsParserItem { + fn into_doc_item(self, type_definition_declares: &Vec) -> DefinitionsItem { + let mut builder = DefinitionsItemBuilder::new() + .with_kind(DefinitionsItemKind::from(&self.type_info)) .with_name(&self.name); if type_definition_declares.contains(&self.name) { builder = builder.as_exported(); @@ -54,11 +54,11 @@ impl DocVisitorItem { } } -impl From<&TypeInfo> for DocItemKind { +impl From<&TypeInfo> for DefinitionsItemKind { fn from(value: &TypeInfo) -> Self { match value { - TypeInfo::Array { .. } | TypeInfo::Table { .. } => DocItemKind::Table, - TypeInfo::Basic(_) | TypeInfo::String(_) => DocItemKind::Property, + TypeInfo::Array { .. } | TypeInfo::Table { .. } => DefinitionsItemKind::Table, + TypeInfo::Basic(_) | TypeInfo::String(_) => DefinitionsItemKind::Property, TypeInfo::Optional { base, .. } => Self::from(base.as_ref()), TypeInfo::Tuple { types, .. } => { let mut kinds = types.iter().map(Self::from).collect::>(); @@ -82,7 +82,7 @@ impl From<&TypeInfo> for DocItemKind { ) } } - typ if type_info_is_fn(typ) => DocItemKind::Function, + typ if type_info_is_fn(typ) => DefinitionsItemKind::Function, typ => unimplemented!( "Missing support for TypeInfo in type definitions parser:\n{}", typ.to_string() @@ -111,7 +111,7 @@ fn parse_type_definitions_declares(contents: &str) -> (String, Vec) { (resulting_contents, found_declares) } -pub fn parse_type_definitions_into_doc_items(contents: S) -> Result> +pub fn parse_type_definitions_into_doc_items(contents: S) -> Result> where S: AsRef, { @@ -128,7 +128,7 @@ where Stmt::TypeDeclaration(typ) => Some((typ, typ.type_token())), _ => None, } { - found_top_level_items.push(DocVisitorItem { + found_top_level_items.push(DefinitionsParserItem { name: declaration.type_name().token().to_string(), comment: find_token_moonwave_comment(token_reference), type_info: declaration.type_definition().clone(), diff --git a/packages/cli/src/gen/doc2/tag.rs b/packages/cli/src/gen/definitions/tag.rs similarity index 92% rename from packages/cli/src/gen/doc2/tag.rs rename to packages/cli/src/gen/definitions/tag.rs index d3c1aec..c4da915 100644 --- a/packages/cli/src/gen/doc2/tag.rs +++ b/packages/cli/src/gen/definitions/tag.rs @@ -1,11 +1,11 @@ use anyhow::{bail, Context, Result}; use serde::{Deserialize, Serialize}; -use super::item::DocItem; +use super::item::DefinitionsItem; #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "PascalCase")] -pub enum DocsItemTag { +pub enum DefinitionsItemTag { Class(String), Within(String), Param((String, String)), @@ -16,7 +16,7 @@ pub enum DocsItemTag { } #[allow(dead_code)] -impl DocsItemTag { +impl DefinitionsItemTag { pub fn is_class(&self) -> bool { matches!(self, Self::Class(_)) } @@ -46,9 +46,9 @@ impl DocsItemTag { } } -impl TryFrom<&DocItem> for DocsItemTag { +impl TryFrom<&DefinitionsItem> for DefinitionsItemTag { type Error = anyhow::Error; - fn try_from(value: &DocItem) -> Result { + fn try_from(value: &DefinitionsItem) -> Result { if let Some(name) = value.get_name() { Ok(match name.trim().to_ascii_lowercase().as_ref() { "class" => Self::Class( diff --git a/packages/cli/src/gen/doc2/tree.rs b/packages/cli/src/gen/definitions/tree.rs similarity index 68% rename from packages/cli/src/gen/doc2/tree.rs rename to packages/cli/src/gen/definitions/tree.rs index 7c90d0c..d4a5f82 100644 --- a/packages/cli/src/gen/doc2/tree.rs +++ b/packages/cli/src/gen/definitions/tree.rs @@ -4,20 +4,20 @@ use anyhow::{Context, Result}; use serde::{Deserialize, Serialize}; use super::{ - builder::DocItemBuilder, item::DocItem, kind::DocItemKind, + builder::DefinitionsItemBuilder, item::DefinitionsItem, kind::DefinitionsItemKind, parser::parse_type_definitions_into_doc_items, }; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct DocTree(DocItem); +pub struct DefinitionsTree(DefinitionsItem); #[allow(dead_code)] -impl DocTree { +impl DefinitionsTree { pub fn from_type_definitions>(type_definitions_contents: S) -> Result { let top_level_items = parse_type_definitions_into_doc_items(type_definitions_contents) .context("Failed to visit type definitions AST")?; - let root = DocItemBuilder::new() - .with_kind(DocItemKind::Root) + let root = DefinitionsItemBuilder::new() + .with_kind(DefinitionsItemKind::Root) .with_name("<<>>") .with_children(&top_level_items) .build()?; @@ -29,19 +29,19 @@ impl DocTree { true } - pub fn into_inner(self) -> DocItem { + pub fn into_inner(self) -> DefinitionsItem { self.0 } } -impl Deref for DocTree { - type Target = DocItem; +impl Deref for DefinitionsTree { + type Target = DefinitionsItem; fn deref(&self) -> &Self::Target { &self.0 } } -impl DerefMut for DocTree { +impl DerefMut for DefinitionsTree { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } diff --git a/packages/cli/src/gen/doc2/mod.rs b/packages/cli/src/gen/doc2/mod.rs deleted file mode 100644 index 2d443bf..0000000 --- a/packages/cli/src/gen/doc2/mod.rs +++ /dev/null @@ -1,13 +0,0 @@ -mod builder; -mod item; -mod kind; -mod moonwave; -mod parser; -mod tag; -mod tree; - -pub use item::DocItem; -pub use kind::DocItemKind; -pub use parser::PIPE_SEPARATOR; -pub use tag::DocsItemTag; -pub use tree::DocTree; diff --git a/packages/cli/src/gen/mod.rs b/packages/cli/src/gen/mod.rs index 2ddaa33..c41d332 100644 --- a/packages/cli/src/gen/mod.rs +++ b/packages/cli/src/gen/mod.rs @@ -3,7 +3,7 @@ mod docs_file; mod selene_defs; mod wiki_dir; -pub mod doc2; +pub mod definitions; pub use docs_file::generate_from_type_definitions as generate_docs_json_from_definitions; pub use selene_defs::generate_from_type_definitions as generate_selene_defs_from_definitions; diff --git a/packages/cli/src/gen/selene_defs.rs b/packages/cli/src/gen/selene_defs.rs index 3041908..1ebf0ef 100644 --- a/packages/cli/src/gen/selene_defs.rs +++ b/packages/cli/src/gen/selene_defs.rs @@ -1,12 +1,12 @@ use anyhow::{Context, Result}; use serde_yaml::{Mapping as YamlMapping, Sequence as YamlSequence, Value as YamlValue}; -use crate::gen::doc2::DocsItemTag; +use crate::gen::definitions::DefinitionsItemTag; -use super::doc2::{DocItem, DocItemKind, DocTree, PIPE_SEPARATOR}; +use super::definitions::{DefinitionsItem, DefinitionsItemKind, DefinitionsTree, PIPE_SEPARATOR}; pub fn generate_from_type_definitions(contents: &str) -> Result { - let tree = DocTree::from_type_definitions(contents)?; + let tree = DefinitionsTree::from_type_definitions(contents)?; let mut globals = YamlMapping::new(); let top_level_exported_items = tree.children().iter().filter(|top_level| { top_level.is_exported() @@ -17,7 +17,7 @@ pub fn generate_from_type_definitions(contents: &str) -> Result { }); for top_level_item in top_level_exported_items { match top_level_item.kind() { - DocItemKind::Table => { + DefinitionsItemKind::Table => { let top_level_name = top_level_item .get_name() .context("Missing name for top-level doc item")? @@ -37,7 +37,7 @@ pub fn generate_from_type_definitions(contents: &str) -> Result { ); } } - DocItemKind::Function => { + DefinitionsItemKind::Function => { globals.insert( YamlValue::String( top_level_item @@ -63,14 +63,14 @@ pub fn generate_from_type_definitions(contents: &str) -> Result { )) } -fn doc_item_to_selene_yaml_mapping(item: &DocItem) -> Result { +fn doc_item_to_selene_yaml_mapping(item: &DefinitionsItem) -> Result { let mut mapping = YamlMapping::new(); if item.is_property() || item.is_table() { let property_access_tag = item .children() .iter() .find_map(|child| { - if let Ok(tag) = DocsItemTag::try_from(child) { + if let Ok(tag) = DefinitionsItemTag::try_from(child) { if tag.is_read_only() || tag.is_read_write() { Some(tag) } else { @@ -90,8 +90,8 @@ fn doc_item_to_selene_yaml_mapping(item: &DocItem) -> Result { YamlValue::String("property".to_string()), YamlValue::String( match property_access_tag { - DocsItemTag::ReadOnly => "read-only", - DocsItemTag::ReadWrite => "new-fields", + DefinitionsItemTag::ReadOnly => "read-only", + DefinitionsItemTag::ReadWrite => "new-fields", _ => unreachable!(), } .to_string(), @@ -99,7 +99,7 @@ fn doc_item_to_selene_yaml_mapping(item: &DocItem) -> Result { ); } else if item.is_function() { let is_must_use = item.children().iter().any(|child| { - if let Ok(tag) = DocsItemTag::try_from(child) { + if let Ok(tag) = DefinitionsItemTag::try_from(child) { tag.is_must_use() } else { false