1use std::str::FromStr;
2
3use anyhow::{Context, Result};
4use clap::{Parser, Subcommand};
5use clap_complete::Shell;
6use reqwest::Client;
7use serde::{Deserialize, Serialize};
8use tracing::info;
9
10mod config;
11mod file;
12mod submission;
13
14pub use config::Config;
15pub use file::FileSubmission;
16pub use submission::Submission;
17
18#[derive(Serialize, Deserialize, Clone)]
20pub struct AccessToken(String);
21
22impl std::fmt::Debug for AccessToken {
23 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24 write!(f, "AccessToken")
25 }
26}
27
28impl AccessToken {
29 pub fn secret(&self) -> &str {
30 &self.0
31 }
32}
33
34#[derive(Parser, Clone, Debug)]
35#[command(version, about, long_about = None)]
36pub struct CLI {
37 #[arg(long)]
40 pub access_token: Option<String>,
41
42 #[arg(long, short)]
45 pub course_id: Option<u64>,
46
47 #[arg(long, short)]
50 pub base_url: Option<String>,
51
52 #[arg(long)]
54 generate: Option<Shell>,
55
56 #[command(subcommand)]
57 pub command: Command,
58
59 pub assignment_id: u64,
61}
62
63#[derive(Subcommand, Clone, Debug)]
64pub enum Command {
65 Debug,
67 Submissions,
69 Grade,
71 #[command(subcommand)]
73 Count(CountOptions),
74}
75
76#[derive(Subcommand, Clone, Debug)]
77pub enum CountOptions {
78 Unsubmitted,
79 Submitted,
80 Graded,
81}
82
83#[derive(Debug)]
84pub struct Grade {
85 pub user_id: u64,
86 pub grade: f32,
87}
88
89impl FromStr for Grade {
90 type Err = anyhow::Error;
91
92 fn from_str(s: &str) -> Result<Self, Self::Err> {
93 let mut parts = s.split(": ");
94 let user_id = parts
95 .next()
96 .context("Unable to parse user id from stdin.")?;
97 let grade = parts.next().context("Unable to parse grade from stdin.")?;
98
99 Ok(Self {
100 user_id: user_id.parse().context("Unable to parse user id to u64")?,
101 grade: grade.parse().context("Unable to parse grade to f32")?,
102 })
103 }
104}
105
106#[derive(Debug)]
107pub struct Comment {
108 pub user_id: u64,
109 pub comment: String,
110}
111
112impl FromStr for Comment {
113 type Err = anyhow::Error;
114
115 fn from_str(s: &str) -> Result<Self, Self::Err> {
116 let (user_id, comment) = s
117 .split_once(": ")
118 .context("Unable to parse comment line.")?;
119
120 Ok(Self {
121 user_id: user_id.parse().context("Unable to parse user id to u64")?,
122 comment: comment.to_string(),
123 })
124 }
125}
126
127pub fn create_client(auth_token: AccessToken) -> Result<Client> {
128 info!("Building application reqwest client...");
129 info!("Setting auth header...");
130 let mut auth_bearer: reqwest::header::HeaderValue = ("Bearer ".to_owned()
131 + auth_token.secret())
132 .try_into()
133 .unwrap();
134 auth_bearer.set_sensitive(true);
135 info!("Auth header set!");
136
137 let mut headers = reqwest::header::HeaderMap::new();
138 headers.insert(reqwest::header::AUTHORIZATION, auth_bearer);
139 headers.insert("per_page", 100.into());
140
141 Ok(reqwest::ClientBuilder::new()
142 .default_headers(headers)
143 .build()?)
144}