traPtitech / rust-traq

⚠️ Community Driven ⚠️ Rust client library for the traQ API
MIT License
1 stars 0 forks source link

get_userが動かない #5

Closed H1rono closed 1 year ago

H1rono commented 1 year ago
use std::error::Error;

use traq::apis::configuration::Configuration;
use traq::apis::user_api::get_user;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let access_token = std::env::var("BOT_ACCESS_TOKEN")?;
    let config = Configuration {
        bearer_access_token: Some(access_token),
        ..Default::default()
    };
    let user = get_user(&config, "2a443820-6346-47ee-8e24-1fb92eb13b53").await?;
    println!("{:?}", user);
    Ok(())
}

Error: Serde(Error("expected value", line: 1, column: 54))

他にも動かないやつがあるかも

H1rono commented 1 year ago

src/main.rs

use std::error::Error;
use std::io::Read;
use traq::models::UserDetail;

fn main() -> Result<(), Box<dyn Error>> {
    let mut stdin = std::io::stdin().lock();
    let mut buff = Vec::new();
    stdin.read_to_end(&mut buff)?;
    let content = String::from_utf8(buff)?;
    let u: UserDetail = serde_json::from_str(&content)?;
    println!("user: {}", serde_json::to_string_pretty(&u)?);
    Ok(())
}

testdata.json

{
    "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
    "state": 0,
    "bot": true,
    "iconFileId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
    "displayName": "string",
    "name": "rDpDFbtRGP73OLgrBxTCLewgEJlZViq",
    "twitterId": "IhUDsKmzOI4aaFK",
    "lastOnline": "2023-07-20T13:22:25.995Z",
    "updatedAt": "2023-07-20T13:22:25.995Z",
    "tags": [
        {
            "tagId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
            "tag": "string",
            "isLocked": true,
            "createdAt": "2023-07-20T13:22:25.995Z",
            "updatedAt": "2023-07-20T13:22:25.995Z"
        }
    ],
    "groups": [
        "3fa85f64-5717-4562-b3fc-2c963f66afa6"
    ],
    "bio": "string",
    "homeChannel": "3fa85f64-5717-4562-b3fc-2c963f66afa6"
}

shell

$ cat testdata.json | cargo run
...
Error: Error("expected value", line: 3, column: 14)
H1rono commented 1 year ago

ここが問題

https://github.com/traPtitech/rust-traq/blob/ab446926d3afb512bfec56fd1557e20a2ee2b6ec/src/models/user_account_state.rs#L13-L22

serde(rename = "0")だと整数の0ではなく文字列の"0"を受け付けるため

H1rono commented 1 year ago

serde-reprを使うのが丸そう

H1rono commented 1 year ago

他に直すところ

https://github.com/traPtitech/rust-traq/blob/ab446926d3afb512bfec56fd1557e20a2ee2b6ec/src/models/bot_state.rs#L13-L22 https://github.com/traPtitech/rust-traq/blob/ab446926d3afb512bfec56fd1557e20a2ee2b6ec/src/models/channel_subscribe_level.rs#L13-L22