Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc/features support #1511

Merged
merged 18 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions extensions/scarb-doc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ pub struct AdditionalMetadata {
pub fn generate_packages_information(
metadata: &Metadata,
metadata_for_packages: &[PackageMetadata],
) -> Vec<PackageInformation> {
) -> Result<Vec<PackageInformation>, anyhow::Error> {
wawel37 marked this conversation as resolved.
Show resolved Hide resolved
let mut packages_information = vec![];
for package_metadata in metadata_for_packages {
let authors = package_metadata.manifest_metadata.authors.clone();

let project_config = get_project_config(metadata, package_metadata);

let crate_ = generate_language_elements_tree_for_package(
package_metadata.name.clone(),
project_config,
Expand All @@ -48,7 +49,7 @@ pub fn generate_packages_information(
},
});
}
packages_information
Ok(packages_information)
}

fn generate_language_elements_tree_for_package(
Expand Down
13 changes: 10 additions & 3 deletions extensions/scarb-doc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ use scarb_doc::docs_generation::markdown::MarkdownContent;
use scarb_doc::metadata::get_target_dir;

use scarb_metadata::MetadataCommand;
use scarb_ui::args::PackagesFilter;
use scarb_ui::args::{PackagesFilter, ToEnvVars};

use scarb_doc::generate_packages_information;
use scarb_doc::versioned_json_output::VersionedJsonOutput;

use scarb_ui::args::FeaturesSpec;

const OUTPUT_DIR: &str = "doc";

#[derive(Default, Debug, Clone, clap::ValueEnum)]
Expand All @@ -32,19 +34,24 @@ struct Args {
/// Specifies a format of generated files.
#[arg(long, value_enum, default_value_t)]
output_format: OutputFormat,

/// Specifies features to enable.
#[command(flatten)]
pub features: FeaturesSpec,
}

fn main_inner() -> Result<()> {
let args = Args::parse();

// args.features.to_env_vars();
wawel37 marked this conversation as resolved.
Show resolved Hide resolved
let metadata = MetadataCommand::new()
.inherit_stderr()
.envs(args.features.to_env_vars())
.exec()
.context("metadata command failed")?;
let metadata_for_packages = args.packages_filter.match_many(&metadata)?;
let output_dir = get_target_dir(&metadata).join(OUTPUT_DIR);

let packages_information = generate_packages_information(&metadata, &metadata_for_packages);
let packages_information = generate_packages_information(&metadata, &metadata_for_packages)?;

match args.output_format {
OutputFormat::Json => {
Expand Down
1 change: 1 addition & 0 deletions extensions/scarb-doc/src/metadata/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ fn get_crates_config(
&component.package.to_string()
)
});

wawel37 marked this conversation as resolved.
Show resolved Hide resolved
(
SmolStr::from(&component.name),
get_crate_settings_for_package(
Expand Down
89 changes: 89 additions & 0 deletions extensions/scarb-doc/tests/code/code_1.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//! Fibonacci sequence calculator

/// Main function that calculates the 16th Fibonacci number
fn main() -> u32 {
fib(16)
}

/// use into_trait
use core::traits::Into as into_trait;
use core::traits::TryInto;

/// FOO constant with value 42
const FOO: u32 = 42;

/// Calculate the nth Fibonacci number
///
/// # Arguments
/// * `n` - The index of the Fibonacci number to calculate
///
fn fib(mut n: u32) -> u32 {
let mut a: u32 = 0;
let mut b: u32 = 1;
while n != 0 {
n = n - 1;
let temp = b;
b = a + b;
a = temp;
};
a
}

/// Pair type alias for a tuple of two u32 values
type Pair = (u32, u32);

/// Color enum with Red, Green, and Blue variants
enum Color {
/// Red color
Red: (),
/// Green color
Green: (),
/// Blue color
Blue: (),
}

/// Shape trait for objects that have an area
trait Shape<T> {
/// Constant for the shape type
const SHAPE_CONST: felt252;

/// Type alias for a pair of shapes
type ShapePair;

/// Calculate the area of the shape
fn area(self: T) -> u32;
}

/// Circle struct with radius field
#[derive(Drop, Serde, PartialEq)]
struct Circle {
/// Radius of the circle
radius: u32,
}

/// Implementation of the Shape trait for Circle
impl CircleShape of Shape<Circle> {
/// Type alias for a pair of circles
type ShapePair = (Circle, Circle);

/// Shape constant
const SHAPE_CONST: felt252 = 'xyz';

/// Implementation of the area method for Circle
fn area(self: Circle) -> u32 {
3 * self.radius * self.radius
}
}

/// Tests module
mod tests {
/// Imported fib function from the parent module
use super::fib as fib_function;

/// Really
#[test]
/// works.
fn it_works() {
assert(fib_function(16) == 987, 'it works!');
}
}
17 changes: 17 additions & 0 deletions extensions/scarb-doc/tests/code/code_2.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! Sub-package code (without feature)

/// Function that prints "test" to stdout with endline.
/// Can invoke it like that:
/// ```cairo
/// fn main() {
/// test();
/// }
/// ```
fn test() {
println!("test");
}

/// Main function that cairo runs as a binary entrypoint.
fn main() {
println!("hello_world");
}
18 changes: 18 additions & 0 deletions extensions/scarb-doc/tests/code/code_3.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! Sub-package code (with feature)

/// Function that prints "test" to stdout with endline.
/// Can invoke it like that:
/// ```cairo
/// fn main() {
/// test();
/// }
/// ```
#[cfg(feature: 'test_feature')]
wawel37 marked this conversation as resolved.
Show resolved Hide resolved
wawel37 marked this conversation as resolved.
Show resolved Hide resolved
fn test() {
println!("test");
}

/// Main function that cairo runs as a binary entrypoint.
fn main() {
println!("hello_world");
}
100 changes: 100 additions & 0 deletions extensions/scarb-doc/tests/code/code_4.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//! Fibonacci sequence calculator

/// Main function that calculates the 16th Fibonacci number
fn main() -> u32 {
fib(16)
}

/// use into_trait
use core::traits::Into as into_trait;
use core::traits::TryInto;

/// FOO constant with value 42
const FOO: u32 = 42;

/// Function that prints "test" to stdout with endline.
/// Can invoke it like that:
/// ```cairo
/// fn main() {
/// test();
/// }
/// ```
#[cfg(feature: "test_feature")]
wawel37 marked this conversation as resolved.
Show resolved Hide resolved
fn test() {
println!("test");
}
/// Calculate the nth Fibonacci number
///
/// # Arguments
/// * `n` - The index of the Fibonacci number to calculate
///
fn fib(mut n: u32) -> u32 {
let mut a: u32 = 0;
let mut b: u32 = 1;
while n != 0 {
n = n - 1;
let temp = b;
b = a + b;
a = temp;
};
a
}

/// Pair type alias for a tuple of two u32 values
type Pair = (u32, u32);

/// Color enum with Red, Green, and Blue variants
enum Color {
/// Red color
Red: (),
/// Green color
Green: (),
/// Blue color
Blue: (),
}

/// Shape trait for objects that have an area
trait Shape<T> {
/// Constant for the shape type
const SHAPE_CONST: felt252;

/// Type alias for a pair of shapes
type ShapePair;

/// Calculate the area of the shape
fn area(self: T) -> u32;
}

/// Circle struct with radius field
#[derive(Drop, Serde, PartialEq)]
struct Circle {
/// Radius of the circle
radius: u32,
}

/// Implementation of the Shape trait for Circle
impl CircleShape of Shape<Circle> {
/// Type alias for a pair of circles
type ShapePair = (Circle, Circle);

/// Shape constant
const SHAPE_CONST: felt252 = 'xyz';

/// Implementation of the area method for Circle
fn area(self: Circle) -> u32 {
3 * self.radius * self.radius
}
}

/// Tests module
mod tests {
/// Imported fib function from the parent module
use super::fib as fib_function;

/// Really
#[test]
/// works.
fn it_works() {
assert(fib_function(16) == 987, 'it works!');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[book]
authors = ["<unknown>"]
language = "en"
multilingual = false
src = "src"
title = "hello_world_sub_package - Cairo"

[output.html]
no-section-label = true

[output.html.playground]
runnable = false

[output.html.fold]
enable = true
level = 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Summary

- [Modules](./modules.md)

- [hello_world_sub_package](./hello_world_sub_package.md)

- [Free functions](./free_functions.md)

- [test](./hello_world_sub_package-test.md)

- [main](./hello_world_sub_package-main.md)

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Free functions

- [test](./hello_world_sub_package-test.md)

- [main](./hello_world_sub_package-main.md)

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# main

Main function that cairo runs as a binary entrypoint.


Fully qualified path: `hello_world_sub_package::main`

```rust
fn main()
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# test

Sub-package code (without feature)
Function that prints "test" to stdout with endline.
Can invoke it like that:
```cairo
fn main() {
test();
}
```


Fully qualified path: `hello_world_sub_package::test`

```rust
fn test()
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# hello_world_sub_package

Fully qualified path: `hello_world_sub_package`

## Free functions

- [test](./hello_world_sub_package-test.md)

- [main](./hello_world_sub_package-main.md)

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Modules

- [hello_world_sub_package](./hello_world_sub_package.md)

Loading
Loading