-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f44a7f2
commit 2fd12f6
Showing
12 changed files
with
206 additions
and
65 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,5 @@ | ||
opening-hours/data/osm_examples.txt | ||
docs | ||
fuzz/artifacts | ||
opening-hours/data/osm_examples.txt | ||
__pycache__ | ||
**/target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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,54 @@ | ||
//! Run stub generation. | ||
//! See https://github.com/Jij-Inc/pyo3-stub-gen | ||
use std::fs::File; | ||
use std::io::Read; | ||
|
||
const STUB_SOURCE_PATH: &str = "opening_hours.pyi"; | ||
const STUB_TARGET_PATH: &str = "../opening_hours.pyi"; | ||
|
||
// ⚠️ Do not copy this code as it is optimized for concision and not performance. | ||
fn load_file(path: &str) -> Vec<u8> { | ||
let mut file = File::open(path).unwrap_or_else(|err| panic!("could not load {path:?}: {err}")); | ||
let mut res = Vec::new(); | ||
|
||
file.read_to_end(&mut res) | ||
.unwrap_or_else(|err| panic!("could not read content from {path:?}: {err}")); | ||
|
||
res | ||
} | ||
|
||
fn main() -> pyo3_stub_gen::Result<()> { | ||
let is_subcommand_check = match std::env::args().nth(1).as_deref() { | ||
None => false, | ||
Some("check") => true, | ||
Some(x) => panic!("Unknown subcommand {x:?}"), | ||
}; | ||
|
||
let stub = opening_hours::stub_info()?; | ||
stub.generate()?; | ||
|
||
std::process::Command::new("poetry") | ||
.args(["run", "ruff", "format", STUB_SOURCE_PATH]) | ||
.output() | ||
.expect("failed to format stubs with ruff"); | ||
|
||
let changed = load_file(STUB_SOURCE_PATH) != load_file(STUB_TARGET_PATH); | ||
|
||
if changed { | ||
if is_subcommand_check { | ||
std::fs::remove_file(STUB_SOURCE_PATH).expect("could not remove stub file"); | ||
panic!("File changed!"); | ||
} else { | ||
println!("Installing new stubs."); | ||
|
||
std::fs::rename(STUB_SOURCE_PATH, STUB_TARGET_PATH) | ||
.expect("could not move stub to project's root"); | ||
} | ||
} else { | ||
println!("No change detected."); | ||
std::fs::remove_file(STUB_SOURCE_PATH).expect("could not remove stub file"); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains 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,18 @@ | ||
use pyo3::prelude::*; | ||
use pyo3_stub_gen::{PyStubType, TypeInfo}; | ||
|
||
/// Tiny wrapper that is here to extend type with stub generation. | ||
#[derive(FromPyObject, IntoPyObject)] | ||
pub(crate) struct TimeZoneWrapper(chrono_tz::Tz); | ||
|
||
impl From<TimeZoneWrapper> for chrono_tz::Tz { | ||
fn from(val: TimeZoneWrapper) -> Self { | ||
val.0 | ||
} | ||
} | ||
|
||
impl PyStubType for TimeZoneWrapper { | ||
fn type_output() -> TypeInfo { | ||
TypeInfo::with_module("zoneinfo.ZoneInfo", "zoneinfo".into()) | ||
} | ||
} |
This file contains 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
Oops, something went wrong.