Skip to content

Commit

Permalink
feat: add some zh doc
Browse files Browse the repository at this point in the history
  • Loading branch information
richerfu committed Jun 22, 2024
1 parent b888009 commit 7ee53ea
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
38 changes: 19 additions & 19 deletions src/docs/basic/simple-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@
editLink: true
---

# Simple Project
# 一个简单的项目🌰

We can use tool to initial our HarmonyOS's project, and it's also possible for us to initial project with standard rust project.
我们可以使用脚手架工具来初始化我们的鸿蒙 native 模块项目,当然也可以通过 Rust 标准的项目来创建鸿蒙的 Native 模块。

Now, we will try to use standard rust project to build prebuild binary.
现在我们尝试使用一个标准的 Rust 项目来开发鸿蒙 native 模块。

## Init
## 初始化

Using `cargo` to initial a binary project.
使用 `cargo` 工具来初始化一个 `lib` 类型的包

```shell
cargo new hello --lib
```

## Add dependencies
## 添加依赖

We need to add some dependencies, which provide useful functions.
我们需要添加一些依赖,这些依赖将会为我们提供很多有用的能力来让我们能够快速的创建鸿蒙 native 模块。

```shell
cargo add napi-ohos napi-derive-ohos

cargo add napi-build-ohos --build
```

## Setup binary type
## 设置包构建产物

There are many binary type for rust project, but now we use `cdylib` as final target. We need to add some config with `Cargo.toml`
Rust 的标准项目中支持了非常多的构建产物类型。对于鸿蒙 native 模块来说,我们只需要使用`cdylib`的类型即可。现在我们将`Cargo.toml`文件新增如下内容。

```toml
# Cargo.toml
Expand All @@ -37,15 +37,15 @@ There are many binary type for rust project, but now we use `cdylib` as final ta
crate-type=["cdylib"]
```

## Setup build script
## 初始化构建脚本

`build.rs` is a special file, that will execute before the project starts to bundle. We can use it to add some special logic to help us build the project.
`build.rs` 是 cargo 管理的 Rust 项目中的一个特殊文件,它将会在项目的真实构建之前被执行。我们可以使用这个文件来实现一些逻辑以满足我们的构建需求和场景。

::: tip 🔆
More info about build script , you can find it with [official book](https://doc.rust-lang.org/cargo/reference/build-scripts.html)
想要了解更多关于 `build.rs` 的信息,你可以在 [官方文档](https://doc.rust-lang.org/cargo/reference/build-scripts.html) 中查看。
:::

At first, create a file `build.rs` in project's root folder, then we need to run `setup`, which is provided by `napi-build-ohos`, and the content is below here:
首先,创建一个名为`build.rs`的文件,文件路径位于项目的根目录。然后我们在该文件中新增如下内容:

```rust
// build.rs
Expand All @@ -54,7 +54,7 @@ fn main () {
}
```

Our project may be like this:
现在我们的项目整体结构看起来应该如下所示:

```txt
.
Expand All @@ -65,9 +65,9 @@ Our project may be like this:
└── lib.rs
```

## Add napi method
## 实现 native 方法

Now we can write some napi method. For example, we can add a `add` method
现在我们可以来实现一些 native 方法了。比如我们现在实现一个用于计算两数只和的方法,其代码则如下所示:

```rust
use napi_derive_ohos::napi;
Expand All @@ -78,14 +78,14 @@ pub fn add(a: u32,b: u32) -> u32 {
}
```

## Build
## 构建

Just run `ohrs` to build our project.
只需要通过`ohrs`一个命令就可以完成构建了。、

```shell
ohrs build
```

Then we can get the final prebuild binary and `.d.ts` file in `hello/dist`.
然后我们可以看到项目根目录出现了`dist`文件夹,其中包含了各个目标架构下的最终产物以及类型声明文件`index.d.ts`

![Dist](assets/dist_tree.png)
24 changes: 22 additions & 2 deletions src/docs/usage/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,27 @@
editLink: true
---

# Basic usage
# 基本用法

In basic usage, we can see [napi-rs](https://napi.rs/)'s document to learn it. All the functions should be aligned with it.
在基本的使用上,我们完全对于 [napi-rs](https://napi.rs/),因此你可以直接参考 napi-rs 的官方文档来学习使用。并且 napi-rs 以及 ohos-rs 项目中也包括了非常多的示例代码以及用于单元测试的代码,这些代码我认为可能对你学习基于 napi-rs 开发 Node 模块或者基于 ohos-rs 开发鸿蒙模块都有着非常好的借鉴作用。


## 特别注意

虽然 ohos-rs 基本上对齐了 napi-rs 但是因为鸿蒙的特异性,我们还是会存在一些差异。在日常开发中,我们需要额外注意。

### 不支持 symbol

因为鸿蒙中不支持使用 symbol,因此 ohos-rs 将 symbol 相关的支持能力都进行了隐藏/移除。在使用的时候,如果没有找到 symbol 相关的能力是符合预期的。

### 功能集有限

目前鸿蒙只支持标准的 N-API **1-8** 的能力集,因此在配置 feature 的时候不能开启 N-API9 以及 experimental 相关的功能。

### 额外的变更

todo

### 不需要调用的能力

todo

0 comments on commit 7ee53ea

Please sign in to comment.