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

API docs improvements #942

Merged
merged 27 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8607a77
API docs for `JsValue`
dherman Nov 13, 2022
ce33685
Align the wording style of doc summaries for all entires in `neon::ty…
dherman Nov 14, 2022
977f1cd
Remove redundant markdown.
dherman Nov 14, 2022
fd50eea
`JsArray` example and method docs.
dherman Nov 19, 2022
942dc88
Add explicit `JsArray` type annotation to code example
dherman Nov 19, 2022
21df710
`JsFunction` API docs
dherman Nov 19, 2022
8aca955
API docs for `JsUndefined`
dherman Nov 22, 2022
68356d8
API docs for:
dherman Nov 22, 2022
f125256
`JsError` example
dherman Nov 22, 2022
13fe178
`JsDate` API docs
dherman Nov 23, 2022
f774d76
Use easy-cast library to clean up the numeric conversion logic in the…
dherman Nov 24, 2022
3bcac26
Merge branch 'main' into apidoc-examples
dherman Dec 23, 2022
94ead0d
Fix failing doc-tests:
dherman Dec 23, 2022
c43c319
Example for `JsFuture`
dherman Jan 7, 2023
5190416
Examples for `JsPromise`
dherman Jan 8, 2023
e93a6ba
Delete trailing whitespace
dherman Jan 8, 2023
c063e50
Examples for `JsString`
dherman Jan 9, 2023
a531021
Use easy-cast for the Promise example
dherman Jan 9, 2023
342d2be
- Link `Deferred` to examples in `JsPromise` and `JsFuture`
dherman Jan 9, 2023
6686822
copy edit: "type of JS smart pointer" -> "JS smart pointer"
dherman Jan 23, 2023
7904f1b
Simplification for Date example code
dherman Jan 23, 2023
cdd8cc8
Simplify example with `call_method_with`
dherman Jan 23, 2023
1f332d6
Simplify another example with `call_method_with`
dherman Jan 23, 2023
3945cba
Add "see also" reference from `exec` to `call_with`.
dherman Jan 23, 2023
e0055bd
Clarifying copy
dherman Jan 27, 2023
4e35241
Explain the role of Deferred in the Promise docs
dherman Jan 27, 2023
498606a
bugfix in doc comment code
dherman Jan 27, 2023
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/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
# The following aliases simplify linting the entire workspace
neon-check = " check --all --all-targets --features napi-experimental,futures,external-buffers"
neon-clippy = "clippy --all --all-targets --features napi-experimental,futures,external-buffers -- -A clippy::missing_safety_doc"
neon-test = " test --all --features=doc-comment,napi-experimental,futures,external-buffers"
neon-test = " test --all --features=doc-dependencies,doc-comment,napi-experimental,futures,external-buffers"
neon-doc = " rustdoc -p neon --features=doc-dependencies,napi-experimental,futures,external-buffers -- --cfg docsrs"
17 changes: 17 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion crates/neon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ semver = "1"
psd = "0.3.1" # used for a doc example
anyhow = "1.0.58" # used for a doc example
widestring = "1.0.2" # used for a doc example
linkify = "0.9.0" # used for a doc example

[target.'cfg(not(target = "windows"))'.dev-dependencies]
# Avoid `clang` as a dependency on windows
Expand All @@ -28,6 +29,7 @@ smallvec = "1.4.2"
once_cell = "1.10.0"
neon-macros = { version = "=1.0.0-alpha.2", path = "../neon-macros" }
aquamarine = { version = "0.1.11", optional = true }
easy-cast = { version = "0.5.1", optional = true }
doc-comment = { version = "0.3.3", optional = true }

[dependencies.tokio]
Expand Down Expand Up @@ -80,7 +82,7 @@ task-api = []
proc-macros = []

# Enables the optional dependencies that are only used for generating the API docs.
doc-dependencies = ["doc-comment", "aquamarine"]
doc-dependencies = ["doc-comment", "aquamarine", "easy-cast"]

[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "docsrs"]
Expand Down
9 changes: 6 additions & 3 deletions crates/neon/src/types_impl/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ mod private {
}
}

/// A smart pointer for Rust data managed by the JavaScript engine.
/// A type of JavaScript smart pointer object that owns Rust data.
///
/// The type `JsBox<T>` provides shared ownership of a value of type `T`,
/// allocated in the heap. The data is owned by the JavaScript engine and the
Expand Down Expand Up @@ -281,8 +281,11 @@ impl<T: Send + 'static> Deref for JsBox<T> {
}
}

/// Finalize is executed on the main JavaScript thread and executed immediately
/// before garbage collection.
/// A trait for finalizing values owned by the main JavaScript thread.
///
/// [`Finalize::finalize`] is executed on the main JavaScript thread
/// immediately before garbage collection.
///
/// Values contained by a `JsBox` must implement `Finalize`.
///
/// ## Examples
Expand Down
18 changes: 13 additions & 5 deletions crates/neon/src/types_impl/buffer/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ macro_rules! doc_comment {
{$comment:expr, $decl:item} => { $decl };
}

/// The Node [`Buffer`](https://nodejs.org/api/buffer.html) type.
/// The type of Node
/// [`Buffer`](https://nodejs.org/api/buffer.html)
/// objects.
///
/// # Example
///
Expand All @@ -50,6 +52,8 @@ pub struct JsBuffer(raw::Local);

impl JsBuffer {
/// Constructs a new `Buffer` object, safely zero-filled.
///
/// **See also:** [`Context::buffer`]
pub fn new<'a, C: Context<'a>>(cx: &mut C, len: usize) -> JsResult<'a, Self> {
let result = unsafe { sys::buffer::new(cx.env().to_raw(), len) };

Expand Down Expand Up @@ -206,7 +210,9 @@ impl TypedArray for JsBuffer {
}
}

/// The standard JS [`ArrayBuffer`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) type.
/// The type of JavaScript
/// [`ArrayBuffer`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
/// objects.
///
/// # Example
///
Expand All @@ -231,6 +237,8 @@ pub struct JsArrayBuffer(raw::Local);

impl JsArrayBuffer {
/// Constructs a new `JsArrayBuffer` object, safely zero-filled.
///
/// **See also:** [`Context::array_buffer`]
pub fn new<'a, C: Context<'a>>(cx: &mut C, len: usize) -> JsResult<'a, Self> {
let result = unsafe { sys::arraybuffer::new(cx.env().to_raw(), len) };

Expand Down Expand Up @@ -420,7 +428,7 @@ pub trait Binary: private::Sealed + Copy {
const TYPE_TAG: TypedArrayType;
}

/// The family of JS [typed array][typed-arrays] types.
/// The family of JavaScript [typed array][typed-arrays] types.
///
/// ## Typed Arrays
///
Expand Down Expand Up @@ -814,11 +822,11 @@ macro_rules! impl_typed_array {

doc_comment! {
concat!(
"The standard JS [`",
"The type of JavaScript [`",
stringify!($typ),
"`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/",
stringify!($typ),
") type.
") objects.
# Example
Expand Down
67 changes: 56 additions & 11 deletions crates/neon/src/types_impl/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,45 @@ use crate::{
sys::{self, raw},
};

/// A JavaScript Date object
/// The type of JavaScript
/// [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
/// objects.
///
/// # Example
///
/// The following shows an example of converting Rust
/// [`SystemTime`](std::time::SystemTime) timestamps to JavaScript `Date` objects.
///
/// ```
/// # use neon::prelude::*;
/// use easy_cast::Cast; // for safe numeric conversions
/// use neon::types::JsDate;
/// use std::fs::File;
/// use std::time::SystemTime;
///
/// /// Return the "modified" timestamp for the file at the given path.
/// fn last_modified(path: &str) -> Option<f64> {
/// // A more robust implementation would do more detailed error reporting.
/// File::open(&path).ok()?
/// .metadata().ok()?
/// .modified().ok()?
/// .duration_since(SystemTime::UNIX_EPOCH).ok()?
/// .as_millis().try_cast().ok()
/// }
///
/// fn modified(mut cx: FunctionContext) -> JsResult<JsDate> {
/// let path: Handle<JsString> = cx.argument(0)?;
///
/// let modified = last_modified(&path.value(&mut cx))
/// .and_then(|n| cx.date(n).ok());
///
/// if let Some(date) = modified {
/// Ok(date)
/// } else {
/// cx.throw_error("failed to get timestamp")
/// }
/// }
/// ```
#[cfg_attr(docsrs, doc(cfg(feature = "napi-5")))]
#[derive(Debug)]
#[repr(transparent)]
Expand All @@ -39,7 +77,7 @@ impl Managed for JsDate {
}
}

/// The Error struct for a Date
/// An error produced when constructing a date with an invalid value.
#[derive(Debug)]
#[cfg_attr(docsrs, doc(cfg(feature = "napi-5")))]
pub struct DateError(DateErrorKind);
Expand All @@ -62,7 +100,11 @@ impl Error for DateError {}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(docsrs, doc(cfg(feature = "napi-5")))]
pub enum DateErrorKind {
/// Produced for an initialization value greater than
/// [`JsDate::MAX_VALUE`](JsDate::MAX_VALUE).
Overflow,
/// Produced for an initialization value lesser than
/// [`JsDate::MIN_VALUE`](JsDate::MIN_VALUE).
Underflow,
}

Expand All @@ -83,13 +125,16 @@ impl<'a, T: Value> ResultExt<Handle<'a, T>> for Result<Handle<'a, T>, DateError>
}

impl JsDate {
/// The smallest possible Date value, defined by ECMAScript. See <https://www.ecma-international.org/ecma-262/5.1/#sec-15.7.3.3>
/// The smallest possible `Date` value,
/// [defined by ECMAScript](https://www.ecma-international.org/ecma-262/5.1/#sec-15.7.3.3).
pub const MIN_VALUE: f64 = -8.64e15;
/// The largest possible Date value, defined by ECMAScript. See <https://www.ecma-international.org/ecma-262/5.1/#sec-15.7.3.2>
/// The largest possible `Date` value,
/// [defined by ECMAScript](https://www.ecma-international.org/ecma-262/5.1/#sec-15.7.3.2).
pub const MAX_VALUE: f64 = 8.64e15;

/// Creates a new Date. It errors when `value` is outside the range of valid JavaScript Date values. When `value`
/// is `NaN`, the operation will succeed but with an invalid Date
/// Creates a new `Date`. It errors when `value` is outside the range of valid JavaScript
/// `Date` values. When `value` is `NaN`, the operation will succeed but with an
/// invalid `Date`.
pub fn new<'a, C: Context<'a>, T: Into<f64>>(
cx: &mut C,
value: T,
Expand All @@ -108,22 +153,22 @@ impl JsDate {
Ok(date)
}

/// Creates a new Date with lossy conversion for out of bounds Date values. Out of bounds
/// values will be treated as NaN
/// Creates a new `Date` with lossy conversion for out of bounds `Date` values.
/// Out of bounds values will be treated as `NaN`.
pub fn new_lossy<'a, C: Context<'a>, V: Into<f64>>(cx: &mut C, value: V) -> Handle<'a, JsDate> {
let env = cx.env().to_raw();
let local = unsafe { sys::date::new_date(env, value.into()) };
Handle::new_internal(JsDate(local))
}

/// Gets the Date's value. An invalid Date will return `std::f64::NaN`
/// Gets the `Date`'s value. An invalid `Date` will return [`std::f64::NAN`].
pub fn value<'a, C: Context<'a>>(&self, cx: &mut C) -> f64 {
let env = cx.env().to_raw();
unsafe { sys::date::value(env, self.to_raw()) }
}

/// Checks if the Date's value is valid. A Date is valid if its value is between
/// `JsDate::MIN_VALUE` and `JsDate::MAX_VALUE` or if it is `NaN`
/// Checks if the `Date`'s value is valid. A `Date` is valid if its value is
/// between [`JsDate::MIN_VALUE`] and [`JsDate::MAX_VALUE`] or if it is `NaN`.
pub fn is_valid<'a, C: Context<'a>>(&self, cx: &mut C) -> bool {
let value = self.value(cx);
(JsDate::MIN_VALUE..=JsDate::MAX_VALUE).contains(&value)
Expand Down
31 changes: 30 additions & 1 deletion crates/neon/src/types_impl/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,30 @@ use crate::{
types::{build, private::ValueInternal, utf8::Utf8, Value},
};

/// A JS `Error` object.
/// The type of JavaScript
/// [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)
/// objects.
///
/// # Example
///
/// ```
/// # use neon::prelude::*;
/// # fn test(mut cx: FunctionContext) -> JsResult<JsUndefined> {
/// // Create a type error:
/// let err = cx.type_error("expected a number, found a string")?;
///
/// // Add some custom diagnostic properties to the error:
/// let expected = cx.string("number");
/// err.set(&mut cx, "expected", expected)?;
///
/// let found = cx.string("string");
/// err.set(&mut cx, "found", found)?;
///
/// // Throw the error:
/// cx.throw(err)?;
/// # Ok(cx.undefined())
/// # }
/// ```
#[repr(transparent)]
#[derive(Debug)]
pub struct JsError(raw::Local);
Expand Down Expand Up @@ -50,6 +73,8 @@ impl Object for JsError {}

impl JsError {
/// Creates a direct instance of the [`Error`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error) class.
///
/// **See also:** [`Context::error`]
pub fn error<'a, C: Context<'a>, S: AsRef<str>>(
cx: &mut C,
msg: S,
Expand All @@ -62,6 +87,8 @@ impl JsError {
}

/// Creates an instance of the [`TypeError`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/TypeError) class.
///
/// **See also:** [`Context::type_error`]
pub fn type_error<'a, C: Context<'a>, S: AsRef<str>>(
cx: &mut C,
msg: S,
Expand All @@ -74,6 +101,8 @@ impl JsError {
}

/// Creates an instance of the [`RangeError`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RangeError) class.
///
/// **See also:** [`Context::range_error`]
pub fn range_error<'a, C: Context<'a>, S: AsRef<str>>(
cx: &mut C,
msg: S,
Expand Down
Loading