├── .gitignore
├── .dockerignore
├── Dockerfile
├── README.md
├── Cargo.toml
├── README_EN.md
├── src
├── morse.rs
└── main.rs
└── Cargo.lock
/.gitignore:
--------------------------------------------------------------------------------
1 | /target
2 |
--------------------------------------------------------------------------------
/.dockerignore:
--------------------------------------------------------------------------------
1 | /target
2 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM rust:alpine as build
2 | RUN apk add --no-cache alpine-sdk
3 | RUN mkdir /app
4 | COPY . /app
5 | RUN --mount=type=cache,target=/root/.cargo/registry \
6 | --mount=type=cache,target=/app/target \
7 | cd /app && cargo build --release && \
8 | cp /app/target/release/isodd /
9 |
10 |
11 | FROM alpine
12 | COPY --from=build /isodd .
13 | ENTRYPOINT [ "/isodd" ]
14 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | [](https://isoddapi.studio.site/)
4 |
5 |
6 |
7 | [日本語](./README.md) | [英語](./README_EN.md)
8 |
9 | 1,000,000 までの,アラビア数字,全角アラビア数字,ローマ数字,漢数字の偶数・奇数を判定することができる API です。
10 |
11 | [詳しくはサイトを確認してください。](https://isoddapi.studio.site/)
12 |
13 | ## 使う
14 |
15 | `https://isodd.api.kawaemon.dev/api/isodd`
16 |
17 | 数値の偶奇を取得します.
18 |
19 | ## 試す
20 |
21 | ```bash
22 | curl https://isodd.api.kawaemon.dev/api/isodd/1234
23 | curl https://isodd.api.kawaemon.dev/api/isodd/%E7%99%BE%E4%B8%83%E5%8D%81%E5%9B%9B
24 | ```
25 |
26 | ※実行には curl が必要です.
27 |
--------------------------------------------------------------------------------
/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "isodd"
3 | version = "0.1.0"
4 | edition = "2018"
5 |
6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7 |
8 | [dependencies]
9 | kanaria = "0.2.0"
10 | kanji-number-parser = { git = "https://github.com/kawaemon/kanji-number-parser.git" }
11 | num-bigint = "0.4.0"
12 | once_cell = "1.8.0"
13 | percent-encoding = "2.1.0"
14 | roman = "0.1.6"
15 | serde_json = "1.0.66"
16 | tokio = { version = "1.10.0", features = ["full"] }
17 | tracing = "0.1.26"
18 | tracing-subscriber = "0.2.20"
19 | warp = "0.3.1"
20 |
21 | [profile.release]
22 | lto = "fat"
23 | codegen-units = 1
24 |
--------------------------------------------------------------------------------
/README_EN.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | [](https://isoddapi.studio.site/)
4 |
5 |
6 |
7 | [Japanese](./README.md) | [English](./README_EN.md)
8 |
9 | This API can determine even and odd numbers of Arabic numerals, full-width Arabic numerals, Roman numerals, and Chinese numerals up to 1,000,000.
10 |
11 | [Check the site for details.](https://isoddapi.studio.site/)
12 |
13 | ## Use
14 |
15 | `https://isodd.api.kawaemon.dev/api/isodd`
16 |
17 | Check the site for details.
18 |
19 | ## Try
20 |
21 | ```bash
22 | curl https://isodd.api.kawaemon.dev/api/isodd/1234
23 | curl https://isodd.api.kawaemon.dev/api/isodd/%E7%99%BE%E4%B8%83%E5%8D%81%E5%9B%9B
24 | ```
25 |
26 | ※ You will need curl to run it.
27 |
--------------------------------------------------------------------------------
/src/morse.rs:
--------------------------------------------------------------------------------
1 | use num_bigint::BigUint;
2 | use once_cell::sync::Lazy;
3 | use std::collections::HashMap;
4 |
5 | fn decode_morse(encoded: &str) -> Option {
6 | static TABLE: Lazy> = Lazy::new(|| {
7 | let mut table = HashMap::with_capacity(10);
8 | table.insert([".----", ".-"], "1");
9 | table.insert(["..---", "..-"], "2");
10 | table.insert(["...--", "...-"], "3");
11 | table.insert(["....-", "....-"], "4");
12 | table.insert([".....", "....."], "5");
13 | table.insert(["-....", "-...."], "6");
14 | table.insert(["--...", "-..."], "7");
15 | table.insert(["---..", "-.."], "8");
16 | table.insert(["----.", "-."], "9");
17 | table.insert(["-----", "-"], "0");
18 | table
19 | });
20 |
21 | let mut decoded_string: Vec<&'static str> = vec![];
22 |
23 | for code in encoded.split(' ') {
24 | let v = TABLE.iter().find(|x| x.0.contains(&code))?;
25 | decoded_string.push(*v.1);
26 | }
27 |
28 | Some(decoded_string.join(""))
29 | }
30 |
31 | pub fn morse_code_translate(code: &str) -> Option {
32 | let morse = decode_morse(code.trim())?;
33 | Some(morse.parse().expect("failed to parse decoded digits"))
34 | }
35 |
--------------------------------------------------------------------------------
/src/main.rs:
--------------------------------------------------------------------------------
1 | mod morse;
2 |
3 | use crate::morse::morse_code_translate;
4 | use kanaria::string::UCSStr;
5 | use kanaria::utils::ConvertTarget;
6 | use num_bigint::BigUint;
7 | use percent_encoding::percent_decode;
8 | use serde_json::json;
9 | use std::str::FromStr;
10 | use warp::http::StatusCode;
11 | use warp::reply::{Json, WithStatus};
12 | use warp::{Filter, Reply};
13 |
14 | #[tokio::main]
15 | async fn main() {
16 | let port = std::env::var("PORT")
17 | .map(|x| x.parse().expect("PORT is not valid"))
18 | .unwrap_or(3000);
19 |
20 | let use_ansi = std::env::var("NO_COLOR").is_err();
21 |
22 | tracing_subscriber::fmt()
23 | .with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
24 | .with_ansi(use_ansi)
25 | .init();
26 |
27 | let route = warp::path!("api" / "isodd" / String)
28 | .and(warp::header::optional("Authorization"))
29 | .map(service)
30 | .with(warp::trace::request());
31 |
32 | tracing::info!("starting to serve at port {}", port);
33 | warp::serve(route).bind(([0, 0, 0, 0], port)).await;
34 | }
35 |
36 | fn reply(json: serde_json::Value, status: impl Into