61dec081创建于 2024年9月20日历史提交
/*
 * 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 crate::{bindgen_prelude::*, check_status, sys, ValueType};

impl TypeName for bool {
  fn type_name() -> &'static str {
    "bool"
  }

  fn value_type() -> ValueType {
    ValueType::Boolean
  }
}

impl ValidateNapiValue for bool {}

impl ToNapiValue for bool {
  unsafe fn to_napi_value(env: sys::napi_env, val: bool) -> Result<sys::napi_value> {
    let mut ptr = std::ptr::null_mut();

    check_status!(
      unsafe { sys::napi_get_boolean(env, val, &mut ptr) },
      "Failed to convert rust type `bool` into napi value",
    )?;

    Ok(ptr)
  }
}

impl FromNapiValue for bool {
  unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
    let mut ret = false;

    check_status!(
      unsafe { sys::napi_get_value_bool(env, napi_val, &mut ret) },
      "Failed to convert napi value into rust type `bool`",
    )?;

    Ok(ret)
  }
}