feat: add dependency
This commit is contained in:
17
javascript-engine/external/iana-time-zone/haiku/Cargo.toml
vendored
Normal file
17
javascript-engine/external/iana-time-zone/haiku/Cargo.toml
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "iana-time-zone-haiku"
|
||||
description = "iana-time-zone support crate for Haiku OS"
|
||||
version = "0.1.1"
|
||||
authors = ["René Kijewski <crates.io@k6i.de>"]
|
||||
repository = "https://github.com/strawlab/iana-time-zone"
|
||||
license = "MIT OR Apache-2.0"
|
||||
keywords = ["IANA", "time"]
|
||||
categories = ["date-and-time", "internationalization", "os"]
|
||||
readme = "README.md"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
cxx = "1.0.34"
|
||||
|
||||
[build-dependencies]
|
||||
cxx-build = "1.0.34"
|
||||
1
javascript-engine/external/iana-time-zone/haiku/LICENSE-APACHE
vendored
Symbolic link
1
javascript-engine/external/iana-time-zone/haiku/LICENSE-APACHE
vendored
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-APACHE
|
||||
1
javascript-engine/external/iana-time-zone/haiku/LICENSE-MIT
vendored
Symbolic link
1
javascript-engine/external/iana-time-zone/haiku/LICENSE-MIT
vendored
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-MIT
|
||||
8
javascript-engine/external/iana-time-zone/haiku/README.md
vendored
Normal file
8
javascript-engine/external/iana-time-zone/haiku/README.md
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
# iana-time-zone-haiku
|
||||
|
||||
[](https://crates.io/crates/iana-time-zone-haiku)
|
||||
[](https://docs.rs/iana-time-zone/)
|
||||
[](https://crates.io/crates/iana-time-zone-haiku)
|
||||
[](https://github.com/strawlab/iana-time-zone/actions?query=branch%3Amain)
|
||||
|
||||
[iana-time-zone](https://github.com/strawlab/iana-time-zone) support crate for Haiku OS.
|
||||
20
javascript-engine/external/iana-time-zone/haiku/build.rs
vendored
Normal file
20
javascript-engine/external/iana-time-zone/haiku/build.rs
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
use std::env;
|
||||
|
||||
fn main() {
|
||||
cxx_build::bridge("src/lib.rs")
|
||||
.file("src/implementation.cc")
|
||||
.flag_if_supported("-std=c++11")
|
||||
.compile("tz_haiku");
|
||||
|
||||
println!("cargo:rerun-if-changed=src/lib.rs");
|
||||
println!("cargo:rerun-if-changed=src/implementation.cc");
|
||||
println!("cargo:rerun-if-changed=src/interface.h");
|
||||
|
||||
let target = env::var_os("TARGET").expect("cargo should set TARGET env var");
|
||||
let target = target
|
||||
.to_str()
|
||||
.expect("TARGET env var should be valid UTF-8");
|
||||
if target.contains("haiku") {
|
||||
println!("cargo:rustc-link-lib=be");
|
||||
}
|
||||
}
|
||||
66
javascript-engine/external/iana-time-zone/haiku/src/implementation.cc
vendored
Normal file
66
javascript-engine/external/iana-time-zone/haiku/src/implementation.cc
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "iana-time-zone-haiku/src/interface.h"
|
||||
#include "iana-time-zone-haiku/src/lib.rs.h"
|
||||
|
||||
#ifdef __HAIKU__
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include <Errors.h>
|
||||
#include <LocaleRoster.h>
|
||||
#include <String.h>
|
||||
#include <TimeZone.h>
|
||||
|
||||
namespace iana_time_zone_haiku {
|
||||
size_t get_tz(rust::Slice<uint8_t> buf) {
|
||||
try {
|
||||
static_assert(sizeof(char) == sizeof(uint8_t), "Illegal char size");
|
||||
|
||||
if (buf.empty()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// `BLocaleRoster::Default()` returns a reference to a statically allocated object.
|
||||
// https://github.com/haiku/haiku/blob/8f16317/src/kits/locale/LocaleRoster.cpp#L143-L147
|
||||
BLocaleRoster *locale_roster(BLocaleRoster::Default());
|
||||
if (!locale_roster) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
BTimeZone tz(NULL, NULL);
|
||||
if (locale_roster->GetDefaultTimeZone(&tz) != B_OK) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
BString bname(tz.ID());
|
||||
int32_t ilength(bname.Length());
|
||||
if (ilength <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t length(ilength);
|
||||
if (length > buf.size()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// BString::String() returns a borrowed string.
|
||||
// https://www.haiku-os.org/docs/api/classBString.html#ae4fe78b06c8e3310093b80305e14ba87
|
||||
const char *sname(bname.String());
|
||||
if (!sname) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::memcpy(buf.data(), sname, length);
|
||||
return length;
|
||||
} catch (...) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
} // namespace iana_time_zone_haiku
|
||||
|
||||
#else
|
||||
|
||||
namespace iana_time_zone_haiku {
|
||||
size_t get_tz(rust::Slice<uint8_t>) { return 0; }
|
||||
} // namespace iana_time_zone_haiku
|
||||
|
||||
#endif
|
||||
9
javascript-engine/external/iana-time-zone/haiku/src/interface.h
vendored
Normal file
9
javascript-engine/external/iana-time-zone/haiku/src/interface.h
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "rust/cxx.h"
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
namespace iana_time_zone_haiku {
|
||||
size_t get_tz(rust::Slice<uint8_t> buf);
|
||||
}
|
||||
68
javascript-engine/external/iana-time-zone/haiku/src/lib.rs
vendored
Normal file
68
javascript-engine/external/iana-time-zone/haiku/src/lib.rs
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
#![warn(clippy::all)]
|
||||
#![warn(clippy::cargo)]
|
||||
#![warn(clippy::undocumented_unsafe_blocks)]
|
||||
#![allow(unknown_lints)]
|
||||
#![warn(missing_copy_implementations)]
|
||||
#![warn(missing_debug_implementations)]
|
||||
#![warn(missing_docs)]
|
||||
#![warn(rust_2018_idioms)]
|
||||
#![warn(trivial_casts, trivial_numeric_casts)]
|
||||
#![warn(unsafe_op_in_unsafe_fn)]
|
||||
#![warn(unused_qualifications)]
|
||||
#![warn(variant_size_differences)]
|
||||
|
||||
//! # iana-time-zone-haiku
|
||||
//!
|
||||
//! [](https://crates.io/crates/iana-time-zone-haiku)
|
||||
//! [](https://docs.rs/iana-time-zone/)
|
||||
//! [](https://crates.io/crates/iana-time-zone-haiku)
|
||||
//! [](https://github.com/strawlab/iana-time-zone/actions?query=branch%3Amain)
|
||||
//!
|
||||
//! [iana-time-zone](https://github.com/strawlab/iana-time-zone) support crate for Haiku OS.
|
||||
|
||||
#[cxx::bridge(namespace = "iana_time_zone_haiku")]
|
||||
mod ffi {
|
||||
// SAFETY: in here "unsafe" is simply part of the syntax
|
||||
unsafe extern "C++" {
|
||||
include!("iana-time-zone-haiku/src/interface.h");
|
||||
|
||||
fn get_tz(buf: &mut [u8]) -> usize;
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the current IANA time zone as a string.
|
||||
///
|
||||
/// On Haiku platforms this function will return [`Some`] with the timezone string
|
||||
/// or [`None`] if an error occurs. On all other platforms, [`None`] is returned.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// let timezone = iana_time_zone_haiku::get_timezone();
|
||||
/// ```
|
||||
pub fn get_timezone() -> Option<String> {
|
||||
// The longest name in the IANA time zone database is 25 ASCII characters long.
|
||||
let mut buf = [0u8; 32];
|
||||
let len = ffi::get_tz(&mut buf);
|
||||
// The name should not be empty, or excessively long.
|
||||
match buf.get(..len)? {
|
||||
b"" => None,
|
||||
s => Some(std::str::from_utf8(s).ok()?.to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
#[cfg(not(target_os = "haiku"))]
|
||||
fn test_fallback_on_non_haiku_platforms() {
|
||||
assert!(super::get_timezone().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(target_os = "haiku")]
|
||||
fn test_retrieve_time_zone_on_haiku_platforms() {
|
||||
let timezone = super::get_timezone().unwrap();
|
||||
assert!(!timezone.is_empty());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user