4afb7f60创建于 2024年9月30日历史提交
/*
 * The MIT License (MIT)
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */

use napi_derive::napi;

/// default enum values are continuos i32s start from 0
#[napi]
pub enum Kind {
  /// Barks
  Dog,
  /// Kills birds
  Cat,
  /// Tasty
  Duck,
}

#[napi(string_enum)]
pub enum Status {
  Pristine,
  Loading,
  Ready,
}

#[napi]
fn enum_to_status() -> Status {
  Status::Loading
}

#[napi(string_enum = "lowercase")]
pub enum StringEnum {
  VariantOne,
  VariantTwo,
  VariantThree,
}

#[napi]
fn enum_to_string_enum() -> StringEnum {
  StringEnum::VariantOne
}

/// You could break the step and for an new continuous value.
#[napi]
pub enum CustomNumEnum {
  One = 1,
  Two,
  Three = 3,
  Four,
  #[doc(hidden)]
  Six = 6,
  Eight = 8,
  Nine, // would be 9
  Ten,  // 10
}

#[napi]
fn enum_to_i32(e: CustomNumEnum) -> i32 {
  e as i32
}

#[napi(skip_typescript)]
pub enum SkippedEnums {
  One = 1,
  Two,
  Tree,
}

#[napi(string_enum)]
pub enum CustomStringEnum {
  #[napi(value = "my-custom-value")]
  Foo,
  Bar,
  Baz,
}
#[napi]
fn enum_to_custom_string_enum() -> CustomStringEnum {
  CustomStringEnum::Foo
}