-
Notifications
You must be signed in to change notification settings - Fork 40
Set up basic diplomat workflow #163
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b869085
Set up basic crates
Manishearth 1b9fdc8
FFI for Error types
Manishearth 946108c
FFI for calendar
Manishearth 9351c0e
Add options
Manishearth 9b145cc
PlainDate
Manishearth 59e80f9
lint
Manishearth fa5ea62
Merge branch 'main' into diplomat
Manishearth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| [workspace] | ||
| resolver = "2" | ||
| members = ["diplomat-gen", "temporal_capi"] | ||
|
|
||
| [workspace.package] | ||
| edition = "2021" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| [package] | ||
| name = "diplomat-gen" | ||
| edition.workspace = true | ||
| version.workspace = true | ||
| rust-version.workspace = true | ||
| authors.workspace = true | ||
| license.workspace = true | ||
| repository.workspace = true | ||
| readme.workspace = true | ||
| exclude.workspace = true | ||
|
|
||
| [dependencies] | ||
| diplomat-tool = "0.9.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| use std::path::Path; | ||
|
|
||
| fn main() -> std::io::Result<()> { | ||
| let manifest = Path::new(env!("CARGO_MANIFEST_DIR")); | ||
|
|
||
| let capi = manifest.parent().unwrap().join("temporal_capi"); | ||
|
|
||
| let library_config = Default::default(); | ||
|
|
||
| diplomat_tool::gen( | ||
| &capi.join("src/lib.rs"), | ||
| "cpp", | ||
| &{ | ||
| let include = capi.join("bindings").join("cpp"); | ||
| std::fs::remove_dir_all(&include)?; | ||
| std::fs::create_dir(&include)?; | ||
| include | ||
| }, | ||
| &Default::default(), | ||
| library_config, | ||
| false, | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| [package] | ||
| name = "temporal_capi" | ||
| edition.workspace = true | ||
| version.workspace = true | ||
| rust-version.workspace = true | ||
| authors.workspace = true | ||
| license.workspace = true | ||
| repository.workspace = true | ||
| readme.workspace = true | ||
| exclude.workspace = true | ||
|
|
||
| [dependencies] | ||
| diplomat = "0.9.0" | ||
| diplomat-runtime = "0.9.0" | ||
| temporal_rs = { version = "0.0.4", path = ".." } | ||
| icu_calendar = { version = "2.0.0-beta1", default-features = false} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| #ifndef DIPLOMAT_RUNTIME_CPP_H | ||
| #define DIPLOMAT_RUNTIME_CPP_H | ||
|
|
||
| #include <optional> | ||
| #include <string> | ||
| #include <type_traits> | ||
| #include <variant> | ||
|
|
||
| #if __cplusplus >= 202002L | ||
| #include <span> | ||
| #else | ||
| #include <array> | ||
| #endif | ||
|
|
||
| namespace diplomat { | ||
|
|
||
| namespace capi { | ||
| extern "C" { | ||
|
|
||
| static_assert(sizeof(char) == sizeof(uint8_t), "your architecture's `char` is not 8 bits"); | ||
| static_assert(sizeof(char16_t) == sizeof(uint16_t), "your architecture's `char16_t` is not 16 bits"); | ||
| static_assert(sizeof(char32_t) == sizeof(uint32_t), "your architecture's `char32_t` is not 32 bits"); | ||
|
|
||
| typedef struct DiplomatWrite { | ||
| void* context; | ||
| char* buf; | ||
| size_t len; | ||
| size_t cap; | ||
| bool grow_failed; | ||
| void (*flush)(struct DiplomatWrite*); | ||
| bool (*grow)(struct DiplomatWrite*, size_t); | ||
| } DiplomatWrite; | ||
|
|
||
| bool diplomat_is_str(const char* buf, size_t len); | ||
|
|
||
| #define MAKE_SLICES(name, c_ty) \ | ||
| typedef struct Diplomat##name##View { \ | ||
| const c_ty* data; \ | ||
| size_t len; \ | ||
| } Diplomat##name##View; \ | ||
| typedef struct Diplomat##name##ViewMut { \ | ||
| c_ty* data; \ | ||
| size_t len; \ | ||
| } Diplomat##name##ViewMut; \ | ||
| typedef struct Diplomat##name##Array { \ | ||
| const c_ty* data; \ | ||
| size_t len; \ | ||
| } Diplomat##name##Array; | ||
|
|
||
| #define MAKE_SLICES_AND_OPTIONS(name, c_ty) \ | ||
| MAKE_SLICES(name, c_ty) \ | ||
| typedef struct Option##name {union { c_ty ok; }; bool is_ok; } Option##name; | ||
|
|
||
| MAKE_SLICES_AND_OPTIONS(I8, int8_t) | ||
| MAKE_SLICES_AND_OPTIONS(U8, uint8_t) | ||
| MAKE_SLICES_AND_OPTIONS(I16, int16_t) | ||
| MAKE_SLICES_AND_OPTIONS(U16, uint16_t) | ||
| MAKE_SLICES_AND_OPTIONS(I32, int32_t) | ||
| MAKE_SLICES_AND_OPTIONS(U32, uint32_t) | ||
| MAKE_SLICES_AND_OPTIONS(I64, int64_t) | ||
| MAKE_SLICES_AND_OPTIONS(U64, uint64_t) | ||
| MAKE_SLICES_AND_OPTIONS(Isize, intptr_t) | ||
| MAKE_SLICES_AND_OPTIONS(Usize, size_t) | ||
| MAKE_SLICES_AND_OPTIONS(F32, float) | ||
| MAKE_SLICES_AND_OPTIONS(F64, double) | ||
| MAKE_SLICES_AND_OPTIONS(Bool, bool) | ||
| MAKE_SLICES_AND_OPTIONS(Char, char32_t) | ||
| MAKE_SLICES(String, char) | ||
| MAKE_SLICES(String16, char16_t) | ||
| MAKE_SLICES(Strings, DiplomatStringView) | ||
| MAKE_SLICES(Strings16, DiplomatString16View) | ||
|
|
||
| } // extern "C" | ||
| } // namespace capi | ||
|
|
||
| extern "C" inline void _flush(capi::DiplomatWrite* w) { | ||
| std::string* string = reinterpret_cast<std::string*>(w->context); | ||
| string->resize(w->len); | ||
| }; | ||
|
|
||
| extern "C" inline bool _grow(capi::DiplomatWrite* w, uintptr_t requested) { | ||
| std::string* string = reinterpret_cast<std::string*>(w->context); | ||
| string->resize(requested); | ||
| w->cap = string->length(); | ||
| w->buf = &(*string)[0]; | ||
| return true; | ||
| }; | ||
|
|
||
| inline capi::DiplomatWrite WriteFromString(std::string& string) { | ||
| capi::DiplomatWrite w; | ||
| w.context = &string; | ||
| w.buf = &string[0]; | ||
| w.len = string.length(); | ||
| w.cap = string.length(); | ||
| // Will never become true, as _grow is infallible. | ||
| w.grow_failed = false; | ||
| w.flush = _flush; | ||
| w.grow = _grow; | ||
| return w; | ||
| }; | ||
|
|
||
| template<class T> struct Ok { | ||
| T inner; | ||
| Ok(T&& i): inner(std::move(i)) {} | ||
| // We don't want to expose an lvalue-capable constructor in general | ||
| // however there is no problem doing this for trivially copyable types | ||
| template<typename X = T, typename = typename std::enable_if<std::is_trivially_copyable<X>::value>::type> | ||
| Ok(T i): inner(i) {} | ||
| Ok() = default; | ||
| Ok(Ok&&) noexcept = default; | ||
| Ok(const Ok &) = default; | ||
| Ok& operator=(const Ok&) = default; | ||
| Ok& operator=(Ok&&) noexcept = default; | ||
| }; | ||
|
|
||
| template<class T> struct Err { | ||
| T inner; | ||
| Err(T&& i): inner(std::move(i)) {} | ||
| // We don't want to expose an lvalue-capable constructor in general | ||
| // however there is no problem doing this for trivially copyable types | ||
| template<typename X = T, typename = typename std::enable_if<std::is_trivially_copyable<X>::value>::type> | ||
| Err(T i): inner(i) {} | ||
| Err() = default; | ||
| Err(Err&&) noexcept = default; | ||
| Err(const Err &) = default; | ||
| Err& operator=(const Err&) = default; | ||
| Err& operator=(Err&&) noexcept = default; | ||
| }; | ||
|
|
||
| template<class T, class E> | ||
| class result { | ||
| private: | ||
| std::variant<Ok<T>, Err<E>> val; | ||
| public: | ||
| result(Ok<T>&& v): val(std::move(v)) {} | ||
| result(Err<E>&& v): val(std::move(v)) {} | ||
| result() = default; | ||
| result(const result &) = default; | ||
| result& operator=(const result&) = default; | ||
| result& operator=(result&&) noexcept = default; | ||
| result(result &&) noexcept = default; | ||
| ~result() = default; | ||
| bool is_ok() const { | ||
| return std::holds_alternative<Ok<T>>(this->val); | ||
| }; | ||
| bool is_err() const { | ||
| return std::holds_alternative<Err<E>>(this->val); | ||
| }; | ||
|
|
||
| std::optional<T> ok() && { | ||
| if (!this->is_ok()) { | ||
| return std::nullopt; | ||
| } | ||
| return std::make_optional(std::move(std::get<Ok<T>>(std::move(this->val)).inner)); | ||
| }; | ||
| std::optional<E> err() && { | ||
| if (!this->is_err()) { | ||
| return std::nullopt; | ||
| } | ||
| return std::make_optional(std::move(std::get<Err<E>>(std::move(this->val)).inner)); | ||
| } | ||
|
|
||
| void set_ok(T&& t) { | ||
| this->val = Ok<T>(std::move(t)); | ||
| } | ||
|
|
||
| void set_err(E&& e) { | ||
| this->val = Err<E>(std::move(e)); | ||
| } | ||
|
|
||
| template<typename T2> | ||
| result<T2, E> replace_ok(T2&& t) { | ||
| if (this->is_err()) { | ||
| return result<T2, E>(Err<E>(std::get<Err<E>>(std::move(this->val)))); | ||
| } else { | ||
| return result<T2, E>(Ok<T2>(std::move(t))); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| class Utf8Error {}; | ||
|
|
||
| // Use custom std::span on C++17, otherwise use std::span | ||
| #if __cplusplus >= 202002L | ||
|
|
||
| template<class T> using span = std::span<T>; | ||
|
|
||
| #else // __cplusplus < 202002L | ||
|
|
||
| // C++-17-compatible std::span | ||
| template<class T> | ||
| class span { | ||
|
|
||
| public: | ||
| constexpr span(T* data, size_t size) | ||
| : data_(data), size_(size) {} | ||
| template<size_t N> | ||
| constexpr span(std::array<typename std::remove_const<T>::type, N>& arr) | ||
| : data_(const_cast<T*>(arr.data())), size_(N) {} | ||
| constexpr T* data() const noexcept { | ||
| return this->data_; | ||
| } | ||
| constexpr size_t size() const noexcept { | ||
| return this->size_; | ||
| } | ||
| private: | ||
| T* data_; | ||
| size_t size_; | ||
| }; | ||
|
|
||
| #endif // __cplusplus >= 202002L | ||
|
|
||
| } // namespace diplomat | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| #[diplomat::bridge] | ||
| #[diplomat::abi_rename = "temporal_rs_{0}"] | ||
| #[diplomat::attr(auto, namespace = "temporal_rs")] | ||
| pub mod ffi { | ||
| use crate::error::ffi::TemporalError; | ||
| use diplomat_runtime::DiplomatStr; | ||
|
|
||
| #[diplomat::enum_convert(icu_calendar::any_calendar::AnyCalendarKind, needs_wildcard)] | ||
| pub enum AnyCalendarKind { | ||
| Buddhist, | ||
| Chinese, | ||
| Coptic, | ||
| Dangi, | ||
| Ethiopian, | ||
| EthiopianAmeteAlem, | ||
| Gregorian, | ||
| Hebrew, | ||
| Indian, | ||
| IslamicCivil, | ||
| IslamicObservational, | ||
| IslamicTabular, | ||
| IslamicUmmAlQura, | ||
| Iso, | ||
| Japanese, | ||
| JapaneseExtended, | ||
| Persian, | ||
| Roc, | ||
| } | ||
|
|
||
| impl AnyCalendarKind { | ||
| pub fn get_for_bcp47_string(s: &DiplomatStr) -> Option<Self> { | ||
| icu_calendar::any_calendar::AnyCalendarKind::get_for_bcp47_bytes(s).map(Into::into) | ||
| } | ||
| } | ||
|
|
||
| #[diplomat::opaque] | ||
| #[diplomat::transparent_convert] | ||
| pub struct Calendar(pub temporal_rs::Calendar); | ||
|
|
||
| impl Calendar { | ||
| pub fn create(kind: AnyCalendarKind) -> Box<Self> { | ||
| Box::new(Calendar(temporal_rs::Calendar::new(kind.into()))) | ||
| } | ||
|
|
||
| pub fn from_utf8(s: &DiplomatStr) -> Result<Box<Self>, TemporalError> { | ||
| temporal_rs::Calendar::from_utf8(s) | ||
| .map(|c| Box::new(Calendar(c))) | ||
| .map_err(Into::into) | ||
| } | ||
|
|
||
| pub fn is_iso(&self) -> bool { | ||
| self.0.is_iso() | ||
| } | ||
|
|
||
| pub fn identifier(&self) -> &'static str { | ||
| self.0.identifier() | ||
| } | ||
|
|
||
| // TODO the rest of calendar (needs all the date/time types) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #[diplomat::bridge] | ||
| #[diplomat::abi_rename = "temporal_rs_{0}"] | ||
| #[diplomat::attr(auto, namespace = "temporal_rs")] | ||
| pub mod ffi { | ||
|
|
||
| #[diplomat::enum_convert(temporal_rs::error::ErrorKind)] | ||
| pub enum ErrorKind { | ||
| Generic, | ||
| Type, | ||
| Range, | ||
| Syntax, | ||
| Assert, | ||
| } | ||
|
|
||
| // In the future we might turn this into an opaque type with a msg() field | ||
| pub struct TemporalError { | ||
| pub kind: ErrorKind, | ||
| } | ||
|
|
||
| impl TemporalError { | ||
| // internal | ||
| pub(crate) fn syntax() -> Self { | ||
| TemporalError { | ||
| kind: ErrorKind::Syntax, | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl From<temporal_rs::TemporalError> for ffi::TemporalError { | ||
| fn from(other: temporal_rs::TemporalError) -> Self { | ||
| Self { | ||
| kind: other.kind().into(), | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #![allow(unused)] // Until we add all the APIs | ||
| #![allow(clippy::needless_lifetimes)] // Diplomat requires explicit lifetimes at times | ||
|
|
||
| mod calendar; | ||
| mod error; | ||
| mod options; | ||
|
|
||
| mod plain_date; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.