Skip to main content

Client

Struct Client 

Source
pub struct Client { /* private fields */ }
Expand description

jp_holidays_lib::client::Client::init() にて初期化を行います。

§関連関数

  • init(): クライアントを初期化します。

§メソッド

  • get_holiday(): chrono::NaiveDate を渡して祝日を取得します。
  • get_holiday_ymd(): 年月日を渡して祝日を取得します。
  • is_holiday(): chrono::NaiveDate を渡して祝日かどうかを判定します。
  • is_holiday_ymd(): 年月日を渡して祝日かどうかを判定します。
  • is_day_off(): chrono::NaiveDate を渡して休日かどうかを判定します。
  • is_day_off_ymd.(): 年月日を渡して休日かどうかを判定します。
  • list_holidays(): 公開されている祝日をすべて取得します (BTreeMap<NaiveDate, String>)

Implementations§

Source§

impl Client

Source

pub async fn init() -> Result<Self, Error>

クライアントを初期化します。

§使用例
use chrono::NaiveDate;
use jp_holidays_lib::client::Client;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::init().await?;

    // 祝日を取得
    let date = NaiveDate::from_ymd_opt(1955, 11, 23).ok_or("存在しない日付です".to_string())?;

    let maybe_holiday = client.get_holiday(date);

    match maybe_holiday {
        Some(holiday) => println!("1955年 11月 23日 は{}", holiday),
        None => println!("1955年 11月 23日 は祝日ではありません"),
    };

    // 祝日かどうか確認
    let date = NaiveDate::from_ymd_opt(1956, 3, 21).ok_or("存在しない日付です".to_string())?;

    let is_holiday = client.is_holiday(date);

    println!(
        "1956 3月 21日 は{}",
        if is_holiday {
            "祝日です"
        } else {
            "祝日ではありません"
        }
    );

    Ok(())
}
§キャッシュの利用

非同期ランタイムに tokio を使用している場合、以下のようにキャッシュを活用できます。

use chrono::NaiveDate;
use jp_holidays_lib::{client::Client, error::Error};

// Client::init() は非同期に内閣府から祝日情報を取得するため、
// tokio::sync::OnceCell を使って初回のみ初期化し、その後はキャッシュを使用します。
static CACHE: tokio::sync::OnceCell<Client> = tokio::sync::OnceCell::const_new();

// キャッシュされた Client インスタンスを取得します
async fn get_client() -> Result<&'static Client, Error> {
    CACHE.get_or_try_init(Client::init).await
}

// 実行用の関数(main から呼び出し)
// スコープを抜けても Client はキャッシュされ続けます
async fn execute() -> Result<(), Box<dyn std::error::Error>> {
    let client = get_client().await?;

    // 祝日を取得
    let date = NaiveDate::from_ymd_opt(1955, 11, 23).ok_or("存在しない日付です".to_string())?;

    let maybe_holiday = client.get_holiday(date);

    match maybe_holiday {
        Some(holiday) => println!("1955年 11月 23日 は{}", holiday),
        None => println!("1955年 11月 23日 は祝日ではありません"),
    };

    // 祝日かどうか確認
    let date = NaiveDate::from_ymd_opt(1956, 3, 21).ok_or("存在しない日付です".to_string())?;

    let is_holiday = client.is_holiday(date);

    println!(
        "1956 3月 21日 は{}",
        if is_holiday {
            "祝日です"
        } else {
            "祝日ではありません"
        }
    );

    Ok(())
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    for i in 0..5 {
        let start = std::time::Instant::now();
        execute().await?;
        let duration = start.elapsed();
        println!("{}回目の実行時間: {:?}\n", i + 1, duration);
    }

    Ok(())
}
Source

pub fn list_holidays(&self) -> &BTreeMap<NaiveDate, String>

現在内閣府から公開されている範囲の祝日一覧を取得します。

§使用例
use chrono::NaiveDate;
use jp_holidays_lib::client::Client;
use std::ops::Bound::{Excluded, Included};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::init().await?;

    // 2018年 の祝日のみを取得します。
    let start = NaiveDate::from_ymd_opt(2018, 1, 1).ok_or("存在しない日付です".to_string())?;
    let end = NaiveDate::from_ymd_opt(2019, 1, 1).ok_or("存在しない日付です".to_string())?;

    // 公開されている祝日をすべて取得します。その後範囲を絞ります。
    let holidays_2018 = client
        .list_holidays()
        .range((Included(start), Excluded(end)));

    for (date, name) in holidays_2018 {
        println!("{} | {}", date, name);
    }

    Ok(())
}
Source

pub fn get_holiday(&self, date: NaiveDate) -> Option<&str>

 chrono::NaiveDate を渡して祝日を取得します。

§使用例
use chrono::NaiveDate;
use jp_holidays_lib::client::Client;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
   let client = Client::init().await?;

   // 祝日を取得
   let date = NaiveDate::from_ymd_opt(1955, 11, 23).ok_or("存在しない日付です".to_string())?;

   let maybe_holiday = client.get_holiday(date);

   match maybe_holiday {
       Some(holiday) => println!("1955年 11月 23日 は{}", holiday),
       None => println!("1955年 11月 23日 は祝日ではありません"),
   };

   Ok(())
}
Source

pub fn get_holiday_ymd( &self, year: i32, month: u32, day: u32, ) -> Result<Option<&str>, Error>

 年月日を渡して祝日を取得します。

§使用例
use jp_holidays_lib::client::Client;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
   let client = Client::init().await?;

   // 祝日を取得
   let maybe_holiday = client.get_holiday_ymd(1955, 11, 23);
   match maybe_holiday? {
       Some(holiday) => println!("1955年 11月 23日 は{}", holiday),
       None => println!("1955年 11月 23日 は祝日ではありません"),
   };

   Ok(())
}
Source

pub fn is_holiday(&self, date: NaiveDate) -> bool

 chrono::NaiveDate を渡して祝日かどうか確認します。

§使用例
use chrono::NaiveDate;
use jp_holidays_lib::client::Client;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
   let client = Client::init().await?;

   // 祝日かどうか確認
   let date = NaiveDate::from_ymd_opt(1956, 3, 21).ok_or("存在しない日付です".to_string())?;

   let is_holiday = client.is_holiday(date);

   println!(
       "1956 3月 21日 は{}",
       if is_holiday {
           "祝日です"
       } else {
           "祝日ではありません"
       }
   );

   Ok(())
}
Source

pub fn is_holiday_ymd( &self, year: i32, month: u32, day: u32, ) -> Result<bool, Error>

 年月日を渡して祝日かどうか確認します。

§使用例
use jp_holidays_lib::client::Client;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
   let client = Client::init().await?;

   // 祝日かどうか確認
   let is_holiday = client.is_holiday_ymd(1956, 3, 21)?;
   println!(
       "1956 3月 21日 は{}",
       if is_holiday {
           "祝日です"
       } else {
           "祝日ではありません"
       }
   );

   Ok(())
}
Source

pub fn is_day_off(&self, date: NaiveDate) -> bool

 chrono::NaiveDate を渡して休日(祝日+土日)かどうか確認します。

§使用例
use chrono::NaiveDate;
use jp_holidays_lib::client::Client;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
   let client = Client::init().await?;

   // 休日かどうか確認
   let date = NaiveDate::from_ymd_opt(1956, 3, 21).ok_or("存在しない日付です".to_string())?;

   let is_day_off = client.is_day_off(date);

   println!(
       "1956 3月 21日 は{}",
       if is_day_off {
           "休日です"
       } else {
           "休日ではありません"
       }
   );

   Ok(())
}
Source

pub fn is_day_off_ymd( &self, year: i32, month: u32, day: u32, ) -> Result<bool, Error>

 年月日を渡して休日(祝日+土日)かどうか確認します。

§使用例
use jp_holidays_lib::client::Client;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
   let client = Client::init().await?;

   // 休日かどうか確認
   let is_day_off = client.is_day_off_ymd(1956, 3, 21)?;
   println!(
       "1956 3月 21日 は{}",
       if is_day_off {
           "休日です"
       } else {
           "休日ではありません"
       }
   );

   Ok(())
}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more