Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ jobs:
run: cargo ws exec cargo clippy --all-features --all-targets
- name: Check clippy (No features)
run: cargo ws exec cargo clippy --no-default-features --all-targets
- name: Check dependencies
run: cargo run -p depcheck

msrv:
name: MSRV
Expand Down
4 changes: 4 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = ["diplomat-gen", "bakeddata", "provider", "temporal_capi"]
members = ["diplomat-gen", "bakeddata", "provider", "temporal_capi", "depcheck"]

[workspace.package]
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion bakeddata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ publish = false
[dependencies]
databake = "0.2.0"
serde_json = "1.0.140"
timezone_provider = { workspace = true }
timezone_provider = { workspace = true, features = ["datagen"] }
12 changes: 12 additions & 0 deletions depcheck/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "depcheck"
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]
190 changes: 190 additions & 0 deletions depcheck/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
//! Test for ensuring that we don't unintentionally add deps

use std::collections::BTreeSet;
use std::process::{self, Command};
use std::str;

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct DepSpec {
crate_name: String,
crate_version: String,
}

/// Get the deep (fully resolved) dependency list produced by `cargo tree -p {package} -e {edge_kind}`
fn get_dep_list(package: &str, edge_kind: &str, extra_args: &str) -> Vec<DepSpec> {
let mut cmd = Command::new("cargo");
cmd.arg("tree")
.arg("-p")
.arg(package)
.arg("-e")
.arg(edge_kind)
.arg("--no-default-features");
for arg in extra_args.split(' ') {
if !arg.is_empty() {
cmd.arg(arg);
}
}
let output = cmd.output().expect("Failed to run `cargo tree`");

if !output.status.success() {
eprintln!("Failed to run `cargo tree -p {package} -e {edge_kind} --no-default-features {extra_args}`:");
if let Ok(s) = str::from_utf8(&output.stderr) {
eprintln!("{s}");
}
process::exit(1);
}
let mut spec: Vec<_> = output
.stdout
.split(|b| *b == b'\n')
.filter_map(|slice| {
if slice.is_empty() {
return None;
}
if slice[0] == b'[' {
// cargo tree output has sections like `[dev-dependencies]`
return None;
}

let mut iter = slice.split(|b| *b == b' ');
let mut found_crate_name = None;
for section in &mut iter {
if section.is_empty() {
continue;
}
// The format is {line drawing characters} {crate name} {crate version}
if char::from(section[0]).is_ascii_alphabetic() {
found_crate_name =
Some(str::from_utf8(section).expect("Must be utf-8").to_owned());
break;
}
}
if let Some(crate_name) = found_crate_name {
let crate_version = iter
.next()
.expect("There must be a version after the crate name!");
let crate_version = str::from_utf8(crate_version)
.expect("Must be utf-8")
.to_owned();
Some(DepSpec {
crate_name,
crate_version,
})
} else {
None
}
})
.collect();
spec.sort();
spec.dedup();

spec
}

/// Given a `cargo tree` invocation and the dependency sets to check, checks for any unlisted or duplicated deps
///
/// `dep_list_name_for_error` is the name of the const above to show in the error suggestion
fn test_dep_list(
package: &str,
edge_kind: &str,
extra_args: &str,
sets: &[&BTreeSet<&str>],
dep_list_name_for_error: &str,
) {
println!("Testing `cargo tree -p {package} -e {edge_kind} --no-default-features {extra_args}`");
let mut errors = Vec::new();
let dep_list = get_dep_list(package, edge_kind, extra_args);
for i in dep_list.windows(2) {
if i[0].crate_name == i[1].crate_name {
errors.push(format!(
"Found two versions for `{0}` ({1} & {2})",
i[0].crate_name, i[0].crate_version, i[1].crate_version
));
}
}

'dep_loop: for i in dep_list {
if i.crate_name == package {
continue;
}
let name = &i.crate_name;
for s in sets {
if s.contains(&**name) {
continue 'dep_loop;
}
}
errors.push(format!(
"Found non-allowlisted crate `{name}`, consider adding to \
{dep_list_name_for_error} in depcheck/src/main.rs if intentional"
));
}

if !errors.is_empty() {
eprintln!("Found invalid dependencies:");
for e in errors {
eprintln!("\t{e}");
}
process::exit(1);
}
}

fn main() {
let basic_runtime: BTreeSet<_> = BASIC_RUNTIME_DEPS.iter().copied().collect();
let compiled_data: BTreeSet<_> = COMPILED_DEPS.iter().copied().collect();

test_dep_list(
"temporal_capi",
"normal,no-proc-macro",
"",
&[&basic_runtime],
"`BASIC_RUNTIME_DEPS`",
);

test_dep_list(
"temporal_capi",
"normal,no-proc-macro",
"--features compiled_data",
&[&basic_runtime, &compiled_data],
"`COMPILED_DEPS`",
);
}

/// Dependencies that are always allowed as runtime dependencies
///
pub const BASIC_RUNTIME_DEPS: &[&str] = &[
// temporal_rs crates
"temporal_rs",
// ICU4X components and utils
"calendrical_calculations",
"core_maths",
"diplomat-runtime",
"icu_calendar",
"icu_calendar_data",
"icu_collections",
"icu_locale",
"icu_locale_core",
"icu_locale_data",
"icu_provider",
"ixdtf",
"litemap",
"potential_utf",
"tinystr",
"writeable",
"yoke",
"zerofrom",
"zerotrie",
"zerovec",
// Other deps
"libm",
"num-traits",
"stable_deref_trait",
];

// Most of these should be removed
pub const COMPILED_DEPS: &[&str] = &[
"bytes",
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of these parser deps should be showing up here.

"combine",
"jiff-tzdb",
"memchr",
"tzif",
"timezone_provider",
];
18 changes: 11 additions & 7 deletions provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ include = [
"README.md"
]

[features]
datagen = ["dep:parse-zoneinfo", "dep:serde", "dep:databake",
"dep:yoke", "dep:serde_json",
"zerotrie/serde", "zerotrie/databake", "zerovec/serde", "zerovec/databake"]
[dependencies]

# Provider dependency
zerotrie = { version = "0.2.2", features = ["serde", "databake"] }
zerovec = { version = "0.11.2", features = ["serde", "databake"] }
zerotrie = "0.2.2"
zerovec = "0.11.2"

# IANA dependency
parse-zoneinfo = "0.3.1"
parse-zoneinfo = {version = "0.3.1", optional = true }

# Databake dependencies
serde = { version = "1.0.219", features = ["derive"] }
databake = { version = "0.2.0", features = ["derive"] }
yoke = { version = "0.8.0", features = ["derive"] }
serde_json = "1.0.140"
serde = { version = "1.0.219", features = ["derive"], optional = true }
databake = { version = "0.2.0", features = ["derive"], optional = true }
yoke = { version = "0.8.0", features = ["derive"], optional = true }
serde_json = { version = "1.0.140", optional = true }

4 changes: 3 additions & 1 deletion provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
//!

mod tzdb;
pub use tzdb::IanaIdentifierNormalizer;

pub use tzdb::{IanaDataError, IanaIdentifierNormalizer};
#[cfg(feature = "datagen")]
pub use tzdb::IanaDataError;

/// A prelude of needed types for interacting with `timezone_provider` data.
pub mod prelude {
Expand Down
28 changes: 19 additions & 9 deletions provider/src/tzdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,44 @@
// - IANA TZif data (much harder)
//

use std::borrow::Cow;
#[cfg(feature = "datagen")]
use std::{
borrow::Cow,
collections::{BTreeMap, BTreeSet},
fs, io,
path::Path,
};

#[cfg(feature = "datagen")]
use parse_zoneinfo::{
line::{Line, LineParser},
table::{Table, TableBuilder},
};
use zerotrie::{ZeroAsciiIgnoreCaseTrie, ZeroTrieBuildError};

use zerotrie::ZeroAsciiIgnoreCaseTrie;
use zerovec::{VarZeroVec, ZeroVec};

/// A data struct for IANA identifier normalization
#[derive(PartialEq, Debug, Clone, yoke::Yokeable, serde::Serialize, databake::Bake)]
#[databake(path = timezone_provider)]
#[derive(serde::Deserialize)]
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(
feature = "datagen",
derive(serde::Serialize, yoke::Yokeable, serde::Deserialize, databake::Bake)
)]
#[cfg_attr(feature = "datagen", databake(path = timezone_provider))]
pub struct IanaIdentifierNormalizer<'data> {
/// TZDB version
pub version: Cow<'data, str>,
/// An index to the location of the normal identifier.
#[serde(borrow)]
#[cfg_attr(feature = "datagen", serde(borrow))]
pub available_id_index: ZeroAsciiIgnoreCaseTrie<ZeroVec<'data, u8>>,

/// The normalized IANA identifier
#[serde(borrow)]
#[cfg_attr(feature = "datagen", serde(borrow))]
pub normalized_identifiers: VarZeroVec<'data, str>,
}

// ==== End Data marker implementation ====

#[cfg(feature = "datagen")]
const ZONE_INFO_FILES: [&str; 9] = [
"africa",
"antarctica",
Expand All @@ -52,11 +58,13 @@ const ZONE_INFO_FILES: [&str; 9] = [
"southamerica",
];

#[cfg(feature = "datagen")]
pub struct TzdbDataProvider {
version: String,
data: Table,
}

#[cfg(feature = "datagen")]
impl TzdbDataProvider {
pub fn new(tzdata: &Path) -> Result<Self, io::Error> {
let parser = LineParser::default();
Expand Down Expand Up @@ -91,12 +99,14 @@ impl TzdbDataProvider {
// ==== Begin DataProvider impl ====

#[derive(Debug)]
#[cfg(feature = "datagen")]
pub enum IanaDataError {
Io(io::Error),
Build(ZeroTrieBuildError),
Build(zerotrie::ZeroTrieBuildError),
}

impl IanaIdentifierNormalizer<'_> {
#[cfg(feature = "datagen")]
pub fn build(tzdata: &Path) -> Result<Self, IanaDataError> {
let provider = TzdbDataProvider::new(tzdata).unwrap();
let mut identifiers = BTreeSet::default();
Expand Down