Skip to content
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

Make tzif public API easier to use #2027

Merged
merged 7 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 1 addition & 1 deletion 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 utils/tzif/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
name = "tzif"
authors = ["The ICU4X Project Developers"]
description = "A parser for TZif files"
version = "0.1.1"
version = "0.2.0"
edition = "2021"
readme = "README.md"
license-file = "LICENSE"
Expand Down
46 changes: 46 additions & 0 deletions utils/tzif/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use std::fmt;

/// An error enum for all error types.
#[derive(Debug)]
pub enum Error {
/// A [`std::io::Error`].
Io(std::io::Error),
/// A [`combine::stream::read::Error`].
Read(combine::stream::read::Error),
/// A [`combine::error::UnexpectedParse`].
Parse(combine::error::UnexpectedParse),
}

impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::Io(err)
}
}

impl From<combine::stream::read::Error> for Error {
fn from(err: combine::stream::read::Error) -> Self {
Error::Read(err)
}
}

impl From<combine::error::UnexpectedParse> for Error {
fn from(err: combine::error::UnexpectedParse) -> Self {
Error::Parse(err)
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io(err) => write!(f, "{}", err),
Error::Read(err) => write!(f, "{}", err),
Error::Parse(err) => write!(f, "{}", err),
}
}
}

impl std::error::Error for Error {}
54 changes: 36 additions & 18 deletions utils/tzif/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,53 @@
//!
//! ### Parse TZif Files
//! ```no_run
//! use combine::{Parser, stream};
//! use std::fs::File;
//! use tzif::tzif;
//!
//! let file = File::open("path_to_file").unwrap();
//! let stream = stream::buffered::Stream::new(
//! stream::position::Stream::new(stream::read::Stream::new(file)),
//! 0, /* lookahead */
//! );
//! let data = tzif().parse(stream).unwrap();
//! use tzif::TzifParser;
//! let data = TzifParser::parse_file("path_to_file").unwrap();
//! ```
//!
//! ### Parse POSIX time-zone strings
//! ```rust
//! use combine::Parser;
//! use tzif::posix_tz_string;
//!
//! let data = posix_tz_string()
//! .parse(b"WGT3WGST,M3.5.0/-2,M10.5.0/-1".as_slice())
//! .unwrap();
//! use tzif::PosixParser;
//! let data = PosixParser::parse_bytes(b"WGT3WGST,M3.5.0/-2,M10.5.0/-1").unwrap();
//! ```

#![warn(missing_docs)]

use combine::{stream, Parser};
use data::{posix::PosixTzString, tzif::TzifData};
use error::Error;
use std::fs::File;
use std::path::Path;

/// The parsed data representations.
pub mod data;

/// The parser implementations.
pub mod parse;

pub use parse::posix::posix_tz_string;
pub use parse::tzif::tzif;
/// Error types an implementations.
pub mod error;

/// A parser for [Time Zone Information Format (`TZif`)](https://tools.ietf.org/id/draft-murchison-tzdist-tzif-00.html) files.
pub struct TzifParser {}

impl TzifParser {
/// Parses a `TZif` file at the provided `path`.
pub fn parse_file<P: AsRef<Path>>(path: P) -> Result<TzifData, Error> {
Copy link
Member

Choose a reason for hiding this comment

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

thought: Should these just be toplevel public functions (parse_tzif_file and parse_posix_file)?

Typically when I see a Parser object I imagine a mutable object that holds state

current design is mostly fine though

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that's reasonable.

I'll make the changes.

let file = File::open(path)?;
let stream = stream::buffered::Stream::new(
stream::position::Stream::new(stream::read::Stream::new(file)),
0, /* lookahead */
);
Ok(parse::tzif::tzif().parse(stream)?.0)
}
}

/// A parser for [POSIX time-zone strings](https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html)
pub struct PosixParser {}
impl PosixParser {
/// Parses a POSIX time-zone string from the given bytes.
pub fn parse_bytes(bytes: &[u8]) -> Result<PosixTzString, Error> {
Ok(parse::posix::posix_tz_string().parse(bytes)?.0)
}
}
1 change: 0 additions & 1 deletion utils/tzif/src/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use combine::parser::combinator::Either;
use combine::parser::error::{unexpected_any, Unexpected};
use combine::parser::token::Value;
use combine::{value, Parser, Stream};

/// Parser definition for POSIX time-zone strings as specified by
/// <https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html>
pub mod posix;
Expand Down
28 changes: 10 additions & 18 deletions utils/tzif/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,29 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use combine::{stream, Parser};
use std::{fs::File, path::Path};
use tzif::parse::posix::posix_tz_string;
use tzif::parse::tzif::tzif;
use std::path::Path;
use tzif::TzifParser;
use walkdir::WalkDir;

fn parse_tzif_file<P: AsRef<Path>>(path: P) {
fn parse_tzif_file<P: AsRef<Path>>(path: P) -> Result<(), tzif::error::Error> {
println!("parsing {:?}", path.as_ref().to_str());
let file = File::open(path).unwrap();
let stream = stream::buffered::Stream::new(
stream::position::Stream::new(stream::read::Stream::new(file)),
0, /* lookahead */
);
let parsed = tzif().parse(stream);
assert!(parsed.is_ok());
println!("{:#?}", parsed.unwrap().0);
let parsed = TzifParser::parse_file(path)?;
println!("{:#?}", parsed);
Ok(())
}

#[test]
fn parse_tzif_testdata() {
fn parse_tzif_testdata() -> Result<(), tzif::error::Error> {
for entry in WalkDir::new("testdata").follow_links(true) {
let entry = entry.unwrap();
if entry.file_type().is_file() {
parse_tzif_file(entry.path())
parse_tzif_file(entry.path())?
}
}
Ok(())
}

#[test]
fn parse_posix_tz_string() {
assert!(posix_tz_string()
.parse(b"WGT3WGST,M3.5.0/-2,M10.5.0/-1".as_slice())
.is_ok());
assert!(tzif::PosixParser::parse_bytes(b"WGT3WGST,M3.5.0/-2,M10.5.0/-1").is_ok());
}