Skip to content

Commit

Permalink
Merge pull request #39 from Kijewski/pr-freebsd
Browse files Browse the repository at this point in the history
Implement for {Free,Net,Open}BSD and Dragonfly
  • Loading branch information
astraw authored Jun 29, 2022
2 parents 295ffa2 + 00de8e0 commit faa31e4
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
33 changes: 33 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,39 @@ jobs:
- run: which wasm-pack || cargo install wasm-pack
- run: wasm-pack test --node

build-cross:
strategy:
matrix:
target: [x86_64-unknown-freebsd, x86_64-unknown-netbsd]
runs-on: ubuntu-latest
name: "Build for ${{ matrix.target }}"
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install Rust
id: actions-rs
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
target: ${{ matrix.target }}
- name: Cache
uses: actions/cache@v3
with:
key: ${{ runner.os }}-${{ matrix.target }}-${{ steps.actions-rs.outputs.rustc_hash }}-${{ hashFiles('Cargo.lock') }}
restore-keys: |
${{ runner.os }}-${{ matrix.target }}-${{ steps.actions-rs.outputs.rustc_hash }}-
${{ runner.os }}-${{ matrix.target }}-
path: |
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
~/.cargo/bin/
target/
- run: which cross || cargo install cross
- run: cross build --target ${{ matrix.target }} --examples

check:
strategy:
matrix:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ mod tz_wasm32;
#[cfg(target_arch = "wasm32")]
use tz_wasm32 as platform;

#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
mod tz_freebsd;
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
use tz_freebsd as platform;

#[cfg(any(target_os = "netbsd", target_os = "openbsd"))]
mod tz_netbsd;
#[cfg(any(target_os = "netbsd", target_os = "openbsd"))]
use tz_netbsd as platform;

/// Error types
#[derive(Debug)]
pub enum GetTimezoneError {
Expand Down
7 changes: 7 additions & 0 deletions src/tz_freebsd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub(crate) fn get_timezone_inner() -> Result<String, crate::GetTimezoneError> {
// see https://gitlab.gnome.org/GNOME/evolution-data-server/-/issues/19
let mut contents = std::fs::read_to_string("/var/db/zoneinfo")?;
// Trim to the correct length without allocating.
contents.truncate(contents.trim_end().len());
Ok(contents)
}
18 changes: 18 additions & 0 deletions src/tz_netbsd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::fs::read_link;

const PREFIX: &str = "/usr/share/zoneinfo/";

pub(crate) fn get_timezone_inner() -> Result<String, crate::GetTimezoneError> {
// see https://www.cyberciti.biz/faq/openbsd-time-zone-howto/
let mut s = read_link("/etc/localtime")?
.into_os_string()
.into_string()
.map_err(|_| crate::GetTimezoneError::FailedParsingString)?;
if !s.starts_with(PREFIX) {
return Err(crate::GetTimezoneError::FailedParsingString);
}

// Trim to the correct length without allocating.
s.replace_range(..PREFIX.len(), "");
Ok(s)
}

0 comments on commit faa31e4

Please sign in to comment.