Skip to content

Commit 1caaaca

Browse files
committed
♻️ Restructure rust API
1 parent 9081c6f commit 1caaaca

File tree

16 files changed

+459
-343
lines changed

16 files changed

+459
-343
lines changed

crates/analyzer/src/analyze.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ pub mod module;
66
pub mod struct_;
77
pub mod type_;
88

9-
pub use self::crate_::{analyze_crate, Crate};
10-
pub use self::enum_::{Enum, Variant};
11-
pub use self::module::Module;
12-
pub use self::struct_::{Field, Struct};
13-
pub use self::type_::TypeSegment;
9+
pub use self::crate_::analyze_crate;
1410

1511
/// Extracts the docstring from an object's attributes
1612
///

crates/analyzer/src/analyze/crate_.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use anyhow::{Context, Result};
33
use serde::{Deserialize, Serialize};
44

5-
use super::{Enum, Module, Struct};
5+
use crate::data_model::{Crate, Enum, Module, Struct};
66

77
pub fn analyze_crate(path: &str) -> Result<AnalysisResult> {
88
// make the path absolute
@@ -154,18 +154,6 @@ pub struct AnalysisResult {
154154
pub enums: Vec<Enum>,
155155
}
156156

157-
#[derive(Debug, Clone, Serialize, Deserialize)]
158-
/// Representation of a crate
159-
///
160-
/// .. req:: Represent a crate
161-
/// :id: RUST006
162-
/// :tags: rust
163-
/// :status: in-progress
164-
pub struct Crate {
165-
pub name: String,
166-
pub version: String,
167-
}
168-
169157
#[derive(Debug, Deserialize)]
170158
struct CargoToml {
171159
package: Package,

crates/analyzer/src/analyze/enum_.rs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,10 @@
11
//! Analyze enums
22
use quote::quote;
3-
use serde::{Deserialize, Serialize};
43
use syn::ItemEnum;
54

6-
use super::{docstring_from_attrs, struct_::Field};
5+
use crate::data_model::{Enum, Field, Variant};
76

8-
#[derive(Debug, Clone, Serialize, Deserialize)]
9-
/// Representation of a Enum
10-
///
11-
/// .. req:: Represent an enum
12-
/// :id: RUST003
13-
/// :tags: rust
14-
/// :status: in-progress
15-
pub struct Enum {
16-
/// The fully qualified name of the enum
17-
pub path: Vec<String>,
18-
/// The docstring of the enum
19-
pub docstring: String,
20-
pub variants: Vec<Variant>,
21-
}
7+
use super::docstring_from_attrs;
228

239
impl Enum {
2410
/// Fully qualified name of the variant
@@ -43,17 +29,6 @@ impl Enum {
4329
}
4430
}
4531

46-
#[derive(Debug, Clone, Serialize, Deserialize)]
47-
/// Representation of a Enum variant
48-
pub struct Variant {
49-
/// The fully qualified name of the variant
50-
pub path: Vec<String>,
51-
/// The docstring of the variant
52-
pub docstring: String,
53-
pub discriminant: Option<String>, // TODO shouldn't just be a string
54-
pub fields: Vec<Field>,
55-
}
56-
5732
impl Variant {
5833
/// Fully qualified name of the variant
5934
pub fn name(&self) -> String {

crates/analyzer/src/analyze/module.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,11 @@
22
use std::path::Path;
33

44
use anyhow::Result;
5-
use serde::{Deserialize, Serialize};
65
use syn::parse_file;
76

8-
use super::{docstring_from_attrs, enum_::Enum, struct_::Struct};
7+
use crate::data_model::{Enum, Module, Struct};
98

10-
#[derive(Debug, Clone, Serialize, Deserialize)]
11-
/// Representation of a module
12-
///
13-
/// .. req:: Represent a module
14-
/// :id: RUST005
15-
/// :tags: rust
16-
/// :status: in-progress
17-
pub struct Module {
18-
/// The path to the module file
19-
pub file: Option<String>,
20-
/// The fully qualified name of the module
21-
pub path: Vec<String>,
22-
pub docstring: String,
23-
/// The public declarations in the module
24-
pub declarations: Vec<String>,
25-
}
9+
use super::docstring_from_attrs;
2610

2711
impl Module {
2812
/// Fully qualified name of the variant

crates/analyzer/src/analyze/struct_.rs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,9 @@
11
//! Analyze structs
2-
use serde::{Deserialize, Serialize};
32
use syn::{ItemStruct, Visibility};
43

5-
use super::{
6-
docstring_from_attrs,
7-
type_::{convert_type, TypeSegment},
8-
};
4+
use crate::data_model::{Field, Struct};
95

10-
#[derive(Debug, Clone, Serialize, Deserialize)]
11-
/// Representation of a Struct
12-
///
13-
/// .. req:: Represent a struct
14-
/// :id: RUST004
15-
/// :tags: rust
16-
/// :status: in-progress
17-
pub struct Struct {
18-
/// The fully qualified name of the struct
19-
pub path: Vec<String>,
20-
/// The docstring of the struct
21-
pub docstring: String,
22-
pub fields: Vec<Field>,
23-
}
6+
use super::{docstring_from_attrs, type_::convert_type};
247

258
impl Struct {
269
/// Fully qualified name of the variant
@@ -50,18 +33,6 @@ impl Struct {
5033
}
5134
}
5235

53-
#[derive(Debug, Clone, Serialize, Deserialize)]
54-
/// Representation of a Struct or Enum field
55-
pub struct Field {
56-
/// The fully qualified name of the field.
57-
///
58-
/// Note, for fields of tuple structs, the final component is the index of the field
59-
pub path: Vec<String>,
60-
/// The docstring of the field
61-
pub docstring: String,
62-
pub type_: Vec<TypeSegment>,
63-
}
64-
6536
impl Field {
6637
/// Extract the relevant information from the AST
6738
pub fn parse(parent: &[&str], position: usize, ast: &syn::Field) -> Self {

crates/analyzer/src/analyze/type_.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
//! Analyze types
22
use quote::quote;
3-
use serde::{Deserialize, Serialize};
43

5-
#[derive(Debug, Clone, Serialize, Deserialize)]
6-
/// A segment of a type signature
7-
///
8-
/// Types are split into segments to allow for identification of referenceable elements
9-
pub enum TypeSegment {
10-
String(String),
11-
Path(String),
12-
}
4+
use crate::data_model::TypeSegment;
135

146
impl From<&str> for TypeSegment {
157
fn from(s: &str) -> Self {

crates/analyzer/src/data_model.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//! Data model for the analyzer
2+
use serde::{Deserialize, Serialize};
3+
4+
#[derive(Debug, Clone, Serialize, Deserialize)]
5+
/// Representation of a crate
6+
///
7+
/// .. req:: Represent a crate
8+
/// :id: RUST003
9+
/// :tags: rust
10+
/// :status: in-progress
11+
pub struct Crate {
12+
pub name: String,
13+
pub version: String,
14+
}
15+
16+
#[derive(Debug, Clone, Serialize, Deserialize)]
17+
/// Representation of a module
18+
///
19+
/// .. req:: Represent a module
20+
/// :id: RUST004
21+
/// :tags: rust
22+
/// :status: in-progress
23+
pub struct Module {
24+
/// The path to the module file
25+
pub file: Option<String>,
26+
/// The fully qualified name of the module
27+
pub path: Vec<String>,
28+
pub docstring: String,
29+
/// The public declarations in the module
30+
pub declarations: Vec<String>,
31+
}
32+
33+
#[derive(Debug, Clone, Serialize, Deserialize)]
34+
/// Representation of a Struct
35+
///
36+
/// .. req:: Represent a struct
37+
/// :id: RUST005
38+
/// :tags: rust
39+
/// :status: in-progress
40+
pub struct Struct {
41+
/// The fully qualified name of the struct
42+
pub path: Vec<String>,
43+
/// The docstring of the struct
44+
pub docstring: String,
45+
pub fields: Vec<Field>,
46+
}
47+
48+
#[derive(Debug, Clone, Serialize, Deserialize)]
49+
/// Representation of a Enum
50+
///
51+
/// .. req:: Represent an enum
52+
/// :id: RUST006
53+
/// :tags: rust
54+
/// :status: in-progress
55+
pub struct Enum {
56+
/// The fully qualified name of the enum
57+
pub path: Vec<String>,
58+
/// The docstring of the enum
59+
pub docstring: String,
60+
pub variants: Vec<Variant>,
61+
}
62+
63+
#[derive(Debug, Clone, Serialize, Deserialize)]
64+
/// Representation of a Enum variant
65+
pub struct Variant {
66+
/// The fully qualified name of the variant
67+
pub path: Vec<String>,
68+
/// The docstring of the variant
69+
pub docstring: String,
70+
pub discriminant: Option<String>, // TODO shouldn't just be a string
71+
pub fields: Vec<Field>,
72+
}
73+
74+
#[derive(Debug, Clone, Serialize, Deserialize)]
75+
/// Representation of a Struct or Enum field
76+
pub struct Field {
77+
/// The fully qualified name of the field.
78+
///
79+
/// Note, for fields of tuple structs, the final component is the index of the field
80+
pub path: Vec<String>,
81+
/// The docstring of the field
82+
pub docstring: String,
83+
pub type_: TypeSignature,
84+
}
85+
86+
#[derive(Debug, Clone, Serialize, Deserialize)]
87+
/// A segment of a type signature
88+
///
89+
/// Types are split into segments to allow for easy identification of referenceable elements
90+
pub enum TypeSegment {
91+
String(String),
92+
Path(String),
93+
}
94+
95+
/// A representation of a type signature
96+
pub type TypeSignature = Vec<TypeSegment>;

crates/analyzer/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
//!
1010
//! We need to be able to analyze a rust package and extract the necessary information from it.
1111
pub mod analyze;
12+
pub mod data_model;

crates/py_binding/src/objects.rs renamed to crates/py_binding/src/data_model.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
//! Mapping of the analyzer data model to pyo3 classes
2+
13
use pyo3::prelude::*;
24

3-
use analyzer::analyze;
5+
use analyzer::data_model;
46

57
#[pyclass]
68
#[derive(Clone)]
@@ -27,8 +29,8 @@ impl Crate {
2729
}
2830
}
2931

30-
impl From<analyze::Crate> for Crate {
31-
fn from(crate_: analyze::Crate) -> Self {
32+
impl From<data_model::Crate> for Crate {
33+
fn from(crate_: data_model::Crate) -> Self {
3234
Crate {
3335
name: crate_.name,
3436
version: crate_.version,
@@ -63,8 +65,8 @@ impl Module {
6365
}
6466
}
6567

66-
impl From<analyze::Module> for Module {
67-
fn from(module: analyze::Module) -> Self {
68+
impl From<data_model::Module> for Module {
69+
fn from(module: data_model::Module) -> Self {
6870
Module {
6971
file: module.file,
7072
path: module.path,
@@ -100,8 +102,8 @@ impl Field {
100102
}
101103
}
102104

103-
impl From<analyze::Field> for Field {
104-
fn from(field: analyze::Field) -> Self {
105+
impl From<data_model::Field> for Field {
106+
fn from(field: data_model::Field) -> Self {
105107
Field {
106108
path: field.path,
107109
docstring: field.docstring,
@@ -137,8 +139,8 @@ impl Struct {
137139
}
138140
}
139141

140-
impl From<analyze::Struct> for Struct {
141-
fn from(module: analyze::Struct) -> Self {
142+
impl From<data_model::Struct> for Struct {
143+
fn from(module: data_model::Struct) -> Self {
142144
Struct {
143145
path: module.path,
144146
docstring: module.docstring,
@@ -174,8 +176,8 @@ impl Enum {
174176
}
175177
}
176178

177-
impl From<analyze::Enum> for Enum {
178-
fn from(module: analyze::Enum) -> Self {
179+
impl From<data_model::Enum> for Enum {
180+
fn from(module: data_model::Enum) -> Self {
179181
Enum {
180182
path: module.path,
181183
docstring: module.docstring,
@@ -212,8 +214,8 @@ impl Variant {
212214
}
213215
}
214216

215-
impl From<analyze::Variant> for Variant {
216-
fn from(var: analyze::Variant) -> Self {
217+
impl From<data_model::Variant> for Variant {
218+
fn from(var: data_model::Variant) -> Self {
217219
Variant {
218220
path: var.path,
219221
docstring: var.docstring,
@@ -244,14 +246,14 @@ impl TypeSegment {
244246
}
245247
}
246248

247-
impl From<analyze::TypeSegment> for TypeSegment {
248-
fn from(field: analyze::TypeSegment) -> Self {
249+
impl From<data_model::TypeSegment> for TypeSegment {
250+
fn from(field: data_model::TypeSegment) -> Self {
249251
match field {
250-
analyze::TypeSegment::Path(content) => TypeSegment {
252+
data_model::TypeSegment::Path(content) => TypeSegment {
251253
content,
252254
is_path: true,
253255
},
254-
analyze::TypeSegment::String(content) => TypeSegment {
256+
data_model::TypeSegment::String(content) => TypeSegment {
255257
content,
256258
is_path: false,
257259
},

0 commit comments

Comments
 (0)