Skip to content

Commit 50f104d

Browse files
authored
Merge pull request #132 from rage/notif
updated the warning system to support different kinds of messages
2 parents a369252 + 9a61f64 commit 50f104d

File tree

8 files changed

+65
-47
lines changed

8 files changed

+65
-47
lines changed

plugins/python3/src/plugin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use tmc_langs_framework::{
1919
};
2020
use tmc_langs_util::{
2121
file_util,
22-
warning_reporter::{self, Warning},
22+
notification_reporter::{self, Notification},
2323
};
2424
use walkdir::WalkDir;
2525

@@ -122,7 +122,7 @@ impl Python3Plugin {
122122
let (major, minor, patch) = Self::get_local_python_ver()?;
123123

124124
if major < recommended_major || major == recommended_major && minor < recommended_minor {
125-
warning_reporter::warn(Warning::new(format!("Your Python is out of date. Minimum maintained release is {}.{}, your Python version was detected as {}.{}. Updating to a newer release is recommended.", recommended_major, recommended_minor, major, minor)));
125+
notification_reporter::notify(Notification::warning(format!("Your Python is out of date. Minimum maintained release is {}.{}, your Python version was detected as {}.{}. Updating to a newer release is recommended.", recommended_major, recommended_minor, major, minor)));
126126
}
127127

128128
if major < minimum_major

tmc-langs-cli/api/warnings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2-
"output-kind": "warning",
3-
"warning": "some warning"
2+
"output-kind": "notification",
3+
"notification-kind": "warning",
4+
"message": "some warning"
45
}

tmc-langs-cli/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::io::{Read, Write};
1919
use std::ops::Deref;
2020
use std::path::{Path, PathBuf};
2121
use std::{env, io::Cursor};
22-
use tmc_langs::{file_util, warning_reporter, CommandError, StyleValidationResult};
22+
use tmc_langs::{file_util, notification_reporter, CommandError, StyleValidationResult};
2323
use tmc_langs::{
2424
ClientError, Credentials, DownloadOrUpdateCourseExercisesResult, DownloadResult,
2525
FeedbackAnswer, TmcClient, TmcConfig,
@@ -75,8 +75,8 @@ fn run_inner() -> Result<(), ()> {
7575
let matches = app::create_app().get_matches();
7676
let pretty = matches.is_present("pretty");
7777

78-
warning_reporter::init(Box::new(move |warning| {
79-
let warning_output = Output::Warning(warning);
78+
notification_reporter::init(Box::new(move |warning| {
79+
let warning_output = Output::Notification(warning);
8080
if let Err(err) = print_output(&warning_output, pretty) {
8181
log::error!("printing warning failed: {}", err);
8282
}

tmc-langs-cli/src/output.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use schemars::JsonSchema;
44
use serde::Serialize;
55
use std::path::PathBuf;
66
use tmc_langs::{
7-
warning_reporter::Warning, ClientUpdateData, CombinedCourseData, ConfigValue, Course,
7+
notification_reporter::Notification, ClientUpdateData, CombinedCourseData, ConfigValue, Course,
88
CourseData, CourseDetails, CourseExercise, DownloadOrUpdateCourseExercisesResult, ExerciseDesc,
99
ExerciseDetails, ExerciseDownload, ExercisePackagingConfiguration, LocalExercise,
1010
NewSubmission, Organization, Review, RunResult, StyleValidationResult, Submission,
@@ -22,7 +22,7 @@ pub enum Output {
2222
/// Status update output as a command progresses.
2323
StatusUpdate(StatusUpdateData),
2424
/// Additional warnings, such as for an outdated Python dependency.
25-
Warning(Warning),
25+
Notification(Notification),
2626
}
2727

2828
impl Output {
@@ -240,8 +240,8 @@ mod test {
240240
}
241241

242242
#[test]
243-
fn warning() {
244-
let status_update = Output::Warning(Warning::new("some warning"));
243+
fn notification() {
244+
let status_update = Output::Notification(Notification::warning("some warning"));
245245
let actual = serde_json::to_string_pretty(&status_update).unwrap();
246246
let expected = read_api_file("warnings.json");
247247
assert_eq!(actual, expected);

tmc-langs-util/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
pub mod error;
66
pub mod file_util;
7+
pub mod notification_reporter;
78
pub mod progress_reporter;
8-
pub mod warning_reporter;
99

1010
pub use error::FileError;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//! Contains an utility for reporting warnings.
2+
3+
use once_cell::sync::OnceCell;
4+
use serde::Serialize;
5+
6+
type NotificationClosure = Box<dyn 'static + Sync + Send + Fn(Notification)>;
7+
8+
static NOTIFICATION_REPORTER: OnceCell<NotificationClosure> = OnceCell::new();
9+
10+
/// Initializes the warning reporter with the given closure to be called with any warnings.
11+
/// Can only be initialized once, repeated calls do nothing.
12+
pub fn init(reporter: NotificationClosure) {
13+
NOTIFICATION_REPORTER.get_or_init(|| reporter);
14+
}
15+
16+
/// Calls the warning closure with the given warning.
17+
pub fn notify(notification: Notification) {
18+
if let Some(reporter) = NOTIFICATION_REPORTER.get() {
19+
reporter(notification);
20+
}
21+
}
22+
23+
#[derive(Debug, Serialize)]
24+
#[serde(rename_all = "kebab-case")]
25+
pub struct Notification {
26+
notification_kind: NotificationKind,
27+
message: String,
28+
}
29+
30+
#[derive(Debug, Serialize)]
31+
#[serde(rename_all = "lowercase")]
32+
pub enum NotificationKind {
33+
Warning,
34+
Info,
35+
}
36+
37+
impl Notification {
38+
pub fn warning(message: impl ToString) -> Self {
39+
Self {
40+
notification_kind: NotificationKind::Warning,
41+
message: message.to_string(),
42+
}
43+
}
44+
45+
pub fn info(message: impl ToString) -> Self {
46+
Self {
47+
notification_kind: NotificationKind::Info,
48+
message: message.to_string(),
49+
}
50+
}
51+
}

tmc-langs-util/src/warning_reporter.rs

Lines changed: 0 additions & 34 deletions
This file was deleted.

tmc-langs/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub use tmc_langs_framework::{
3232
};
3333
pub use tmc_langs_util::{
3434
file_util::{self, FileLockGuard},
35-
warning_reporter,
35+
notification_reporter,
3636
};
3737

3838
use crate::config::ProjectsConfig;

0 commit comments

Comments
 (0)