1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
use crate::types::TimeType;
use std::borrow::Cow;
/// Use this struct to add more information regarding the section that you want to enroll/waitlist
/// in.
pub struct EnrollWaitAdd<'a> {
/// The section ID. For example, `0123123`.
pub section_id: Cow<'a, str>,
/// The grading option. Can either be L, P, or S.
/// If None is specified, this uses the default option.
pub grading_option: Option<GradeOption>,
/// The number of units. If none is specified, this
/// uses the default unit count.
pub unit_count: Option<u8>,
}
impl<'a> EnrollWaitAdd<'a> {
/// Creates a new builder for the `EnrollWaitAdd` structure.
///
/// # Returns
/// The builder.
pub fn builder() -> EnrollWaitAddBuilder<'a> {
EnrollWaitAddBuilder {
section_id: None,
grading_option: None,
unit_count: None,
}
}
}
pub struct EnrollWaitAddBuilder<'a> {
section_id: Option<Cow<'a, str>>,
grading_option: Option<GradeOption>,
unit_count: Option<u8>,
}
impl<'a> EnrollWaitAddBuilder<'a> {
pub fn new() -> Self {
EnrollWaitAddBuilder {
section_id: None,
grading_option: None,
unit_count: None,
}
}
/// Sets the section ID for this builder. For example, `0123123` is a possible section ID.
///
/// # Parameters
/// - `section_id`: The section ID.
///
/// # Returns
/// The builder.
pub fn with_section_id(mut self, section_id: impl Into<Cow<'a, str>>) -> Self {
self.section_id = Some(section_id.into());
self
}
/// Sets the grading option (L, P/NP, S/U) for this builder.
///
/// # Parameters
/// - `grading_option`: The grading option.
///
/// # Returns
/// The builder.
pub fn with_grading_option(mut self, grading_option: GradeOption) -> Self {
self.grading_option = Some(grading_option);
self
}
/// Sets the number of units for this builder.
///
/// # Parameters
/// - `units`: The number of units.
///
/// # Returns
/// The builder.
pub fn with_unit_count(mut self, units: u8) -> Self {
self.unit_count = Some(units);
self
}
/// Tries to build the `EnrollWaitAdd` object.
///
/// # Returns
/// The result of constructing this `EnrollWaitAdd` object. It is guaranteed that this will
/// result the `EnrollWaitAdd` object if the section ID has been provided.
pub fn try_build(self) -> Option<EnrollWaitAdd<'a>> {
if let Some(section_id) = self.section_id {
Some(EnrollWaitAdd {
section_id,
grading_option: self.grading_option,
unit_count: self.unit_count,
})
} else {
None
}
}
}
impl<'a> Default for EnrollWaitAddBuilder<'a> {
fn default() -> Self {
EnrollWaitAddBuilder::new()
}
}
// This trait implementation may be helpful later.
impl<'a> AsRef<EnrollWaitAdd<'a>> for EnrollWaitAdd<'a> {
fn as_ref(&self) -> &EnrollWaitAdd<'a> {
self
}
}
/// Use this struct to add more information regarding the course that you want to plan.
///
/// Prefer using the `PlanAddBuilder` to construct this object.
pub struct PlanAdd<'a> {
/// The subject code. For example, `CSE`.
pub subject_code: Cow<'a, str>,
/// The course code. For example, `12`.
pub course_code: Cow<'a, str>,
/// The section ID. For example, `0123123`.
pub section_id: Cow<'a, str>,
/// The section code. For example `A00`.
pub section_code: Cow<'a, str>,
/// The grading option.
pub grading_option: Option<GradeOption>,
/// The schedule name. By default, this will use the default schedule.
pub schedule_name: Option<Cow<'a, str>>,
/// The number of units.
pub unit_count: u8,
}
impl<'a> PlanAdd<'a> {
/// Creates a builder to construct this `PlanAdd` object. This is recommended over
/// directly creating the object yourself.
///
/// # Returns
/// The builder.
pub fn builder() -> PlanAddBuilder<'a> {
PlanAddBuilder::new()
}
}
pub struct PlanAddBuilder<'a> {
subject_code: Option<Cow<'a, str>>,
course_code: Option<Cow<'a, str>>,
section_id: Option<Cow<'a, str>>,
section_code: Option<Cow<'a, str>>,
grading_option: Option<GradeOption>,
schedule_name: Option<Cow<'a, str>>,
unit_count: Option<u8>,
}
impl<'a> PlanAddBuilder<'a> {
/// Creates a new builder for the `PlanAdd` structure.
///
/// # Returns
/// The builder.
pub fn new() -> Self {
PlanAddBuilder {
subject_code: None,
course_code: None,
section_id: None,
section_code: None,
grading_option: None,
schedule_name: None,
unit_count: None,
}
}
/// Sets the subject code for this builder. For example, if `CSE 100` is the course,
/// then you would use `CSE`.
///
/// # Parameters
/// - `subj_code`: The subject code.
///
/// # Returns
/// The builder.
pub fn with_subject_code(mut self, subj_code: impl Into<Cow<'a, str>>) -> Self {
self.subject_code = Some(subj_code.into());
self
}
/// Sets the course code for this builder. For example, if `CSE 100` is the course,
/// then you would use `100`.
///
/// # Parameters
/// - `course_code`: The course code.
///
/// # Returns
/// The builder.
pub fn with_course_code(mut self, course_code: impl Into<Cow<'a, str>>) -> Self {
self.course_code = Some(course_code.into());
self
}
/// Sets the section ID for this builder. For example, `0123123` is a possible section ID.
///
/// # Parameters
/// - `section_id`: The section ID.
///
/// # Returns
/// The builder.
pub fn with_section_id(mut self, section_id: impl Into<Cow<'a, str>>) -> Self {
self.section_id = Some(section_id.into());
self
}
/// Sets the section code for this builder. For example, `A01` is a possible section code.
///
/// # Parameters
/// - `section_code`: The section code.
///
/// # Returns
/// The builder.
pub fn with_section_code(mut self, section_code: impl Into<Cow<'a, str>>) -> Self {
self.section_code = Some(section_code.into());
self
}
/// Sets the grading option (L, P/NP, S/U) for this builder.
///
/// # Parameters
/// - `grading_option`: The grading option.
///
/// # Returns
/// The builder.
pub fn with_grading_option(mut self, grading_option: GradeOption) -> Self {
self.grading_option = Some(grading_option);
self
}
/// Sets the schedule name for this builder.
///
/// # Parameters
/// - `schedule_name`: The schedule name.
///
/// # Returns
/// The builder.
pub fn with_schedule_name(mut self, schedule_name: impl Into<Cow<'a, str>>) -> Self {
self.schedule_name = Some(schedule_name.into());
self
}
/// Sets the number of units for this builder.
///
/// # Parameters
/// - `units`: The number of units.
///
/// # Returns
/// The builder.
pub fn with_unit_count(mut self, units: u8) -> Self {
self.unit_count = Some(units);
self
}
/// Tries to build the `PlanAdd` object.
///
/// # Returns
/// The result of constructing this `PlanAdd` object. It is guaranteed that this will result
/// the `PlanAdd` object if the following have been provided at the time of construction:
/// - subject code,
/// - course code,
/// - section ID,
/// - section code,
/// - unit count.
pub fn try_build(self) -> Option<PlanAdd<'a>> {
if let (Some(s), Some(c), Some(sec_id), Some(sec_code), Some(u)) = (
self.subject_code,
self.course_code,
self.section_id,
self.section_code,
self.unit_count,
) {
Some(PlanAdd {
subject_code: s,
course_code: c,
section_id: sec_id,
section_code: sec_code,
grading_option: self.grading_option,
schedule_name: self.schedule_name,
unit_count: u,
})
} else {
None
}
}
}
impl<'a> Default for PlanAddBuilder<'a> {
fn default() -> Self {
PlanAddBuilder::new()
}
}
/// A struct that represents an event to be added.
///
/// Prefer using the corresponding `EventAddBuilder` to build this object.
pub struct EventAdd<'a> {
/// The name of the event. This is required.
pub event_name: Cow<'a, str>,
/// The location of the event. This is optional.
pub location: Option<Cow<'a, str>>,
/// The days that this event will be held.
pub event_days: Vec<DayOfWeek>,
/// The hour start time. For example, if the event starts at
/// 3:50 PM, use `15` (since `12 + 3 = 15`).
pub start_hr: TimeType,
/// The minute start time. For example, if the event starts at
/// 3:50 PM, use `50`.
pub start_min: TimeType,
/// The hour end time. For example, if the event ends at 3:50 PM,
/// use `15` (since `12 + 3 = 15`).
pub end_hr: TimeType,
/// The minute end time. For example, if the event ends at 3:50 PM,
/// use `50`.
pub end_min: TimeType,
}
impl<'a> EventAdd<'a> {
/// Creates a builder to construct this `EventAdd` object. This is recommended over
/// directly creating the object yourself.
///
/// # Returns
/// The builder.
pub fn builder() -> EventAddBuilder<'a> {
EventAddBuilder::new()
}
}
pub struct EventAddBuilder<'a> {
event_name: Option<Cow<'a, str>>,
location: Option<Cow<'a, str>>,
event_days: Vec<DayOfWeek>,
start_hr: Option<TimeType>,
start_min: Option<TimeType>,
end_hr: Option<TimeType>,
end_min: Option<TimeType>,
}
impl<'a> EventAddBuilder<'a> {
/// Creates a new builder for the `EventAdd` structure.
///
/// # Returns
/// The builder.
pub fn new() -> Self {
EventAddBuilder {
event_name: None,
location: None,
event_days: vec![],
start_hr: None,
start_min: None,
end_hr: None,
end_min: None,
}
}
/// Sets the name of this event.
///
/// # Parameter
/// - `name`: The name of the event.
///
/// # Return
/// The builder.
pub fn with_name(mut self, name: impl Into<Cow<'a, str>>) -> Self {
self.event_name = Some(name.into());
self
}
/// Sets the location of this event.
///
/// # Parameter
/// - `loc`: The location of the event.
///
/// # Return
/// The builder.
pub fn with_location(mut self, loc: impl Into<Cow<'a, str>>) -> Self {
self.location = Some(loc.into());
self
}
/// Adds a day when this event will occur.
///
/// # Parameter
/// - `day`: The day that the event will be held.
///
/// # Return
/// The builder.
pub fn with_day(mut self, day: DayOfWeek) -> Self {
self.event_days.push(day);
self
}
/// Sets the start time of the event.
///
/// # Parameter
/// - `hr`: The starting hour of the event, in 24-hour format.
/// - `min`: The starting minute of the event.
///
/// # Return
/// The builder. The builder will only be modified if the `hr` and `min` arguments
/// are valid (`0 <= hr <= 23` AND `0 <= min <= 59`).
pub fn with_start_time(mut self, hr: TimeType, min: TimeType) -> Self {
if (0..=23).contains(&hr) && (0..=59).contains(&min) {
self.start_hr = Some(hr);
self.start_min = Some(min);
}
self
}
/// Sets the end time of the event.
///
/// # Parameter
/// - `hr`: The ending hour of the event, in 24-hour format.
/// - `min`: The ending minute of the event.
///
/// # Return
/// The builder. The builder will only be modified if the `hr` and `min` arguments
/// are valid (`0 <= hr <= 23` AND `0 <= min <= 59`).
pub fn with_end_time(mut self, hr: TimeType, min: TimeType) -> Self {
if (0..=23).contains(&hr) && (0..=59).contains(&min) {
self.end_hr = Some(hr);
self.end_min = Some(min);
}
self
}
/// Attempts to build the event.
///
/// # Returns
/// The result of the construction of this object. It is guaranteed that this construction
/// will be successful if the following fields were set:
/// - the event name,
/// - the event start time, and
/// - the event end time.
pub fn try_build(self) -> Option<EventAdd<'a>> {
if let (Some(name), Some(s_hr), Some(s_min), Some(e_hr), Some(e_min)) = (
self.event_name,
self.start_hr,
self.start_min,
self.end_hr,
self.end_min,
) {
Some(EventAdd {
event_name: name,
location: self.location,
event_days: self.event_days,
start_hr: s_hr,
start_min: s_min,
end_hr: e_hr,
end_min: e_min,
})
} else {
None
}
}
}
impl<'a> Default for EventAddBuilder<'a> {
fn default() -> Self {
EventAddBuilder::new()
}
}
/// The possible grading options.
#[derive(PartialOrd, PartialEq, Debug)]
pub enum GradeOption {
/// S/U grading (Satisfactory/Unsatisfactory) option.
S,
/// P/NP grading (Pass/No Pass) option.
P,
/// Letter grading option.
L,
}
impl GradeOption {
/// Gets the (static) string representation of this `enum`.
///
/// # Returns
/// The static string representation.
pub fn as_str(&self) -> &'static str {
match self {
GradeOption::L => "L",
GradeOption::S => "S",
GradeOption::P => "P",
}
}
}
/// An enum that represents how a course should be added to the person's schedule when
/// calling the corresponding `add_section` method (and associated methods).
pub enum AddType {
/// Indicates that the user wants to enroll into the section.
Enroll,
/// Indicates that the user wants to waitlist the section.
Waitlist,
/// Have the library check whether the user should enroll or waitlist.
DecideForMe,
}
/// An enum that's similar to `AddType`, but explicitly only allows `Enroll` or `Waitlist`
/// actions.
pub enum ExplicitAddType {
/// Indicates that the user wants to enroll into the section.
Enroll,
/// Indicates that the user wants to waitlist the section.
Waitlist,
}
/// Used to construct search requests for the `search_courses` function.
///
/// When building your request, you can either use one of the helper methods
/// to add information to your request, or even just directly modify the fields.
/// Note that the former does some validation.
#[derive(Clone)]
pub struct SearchRequestBuilder {
pub subjects: Vec<String>,
pub courses: Vec<String>,
pub departments: Vec<String>,
pub instructor: Option<String>,
pub title: Option<String>,
pub level_filter: u32,
pub days: u32,
pub start_time: Option<(TimeType, TimeType)>,
pub end_time: Option<(TimeType, TimeType)>,
pub only_open: bool,
}
impl SearchRequestBuilder {
/// Creates a new instance of the `SearchRequestBuilder`, which is used to search for specific
/// courses.
///
/// # Returns
/// The empty `SearchRequestBuilder`.
pub fn new() -> Self {
Self {
subjects: vec![],
courses: vec![],
departments: vec![],
instructor: None,
title: None,
level_filter: 0,
days: 0,
start_time: None,
end_time: None,
only_open: false,
}
}
/// Adds a subject to this search request. Valid search requests are uppercase and at most
/// 4 characters long. Some examples include `MATH` or `CSE`.
///
/// # Parameters
/// - `subject`: The subject.
///
/// # Returns
/// The `SearchRequestBuilder`
pub fn add_subject(mut self, subject: impl Into<String>) -> Self {
let s: String = subject.into();
if s.len() > 4 {
return self;
}
self.subjects.push(s.to_uppercase());
self
}
/// Adds a course (either a subject code, course code, or both) to the search request. Some
/// examples include `20E`, `math 20d`, `101`, `CSE`.
///
/// # Parameters
/// - `course`: The course.
///
/// # Returns
/// The `SearchRequestBuilder`
pub fn add_course(mut self, course: impl Into<String>) -> Self {
self.courses.push(course.into());
self
}
/// Adds a department to the search request. Valid search requests are uppercase and at most 4
/// characters long. Some examples include `MATH` or `CSE`.
///
/// # Parameters
/// - `department`: The department.
///
/// # Returns
/// The `SearchRequestBuilder`
pub fn add_department(mut self, department: impl Into<String>) -> Self {
let d: String = department.into();
if d.len() > 4 {
return self;
}
self.departments.push(d.to_uppercase());
self
}
/// Sets the instructor to the specified instructor.
///
/// # Parameters
/// - `instructor`: The instructor. This should be formatted in `Last Name, First Name` form.
///
/// # Returns
/// The `SearchRequestBuilder`
pub fn set_instructor(mut self, instructor: impl Into<String>) -> Self {
self.instructor = Some(instructor.into());
self
}
/// Sets the course title to the specified title. Some examples could be `differential equ`,
/// `data structures`, `algorithms`, and so on.
///
/// # Parameters
/// - `title`: The title of the course.
///
/// # Returns
/// The `SearchRequestBuilder`
pub fn set_title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
/// Restrict search results to to the specified filter. This can be applied multiple times.
///
/// # Parameters
/// - `filter`: The filter.
///
/// # Returns
/// The `SearchRequestBuilder`
pub fn filter_courses_by(mut self, filter: CourseLevelFilter) -> Self {
self.level_filter |= match filter {
CourseLevelFilter::LowerDivision => 1 << 11,
CourseLevelFilter::FreshmenSeminar => 1 << 10,
CourseLevelFilter::LowerDivisionIndependentStudy => 1 << 9,
CourseLevelFilter::UpperDivision => 1 << 8,
CourseLevelFilter::Apprenticeship => 1 << 7,
CourseLevelFilter::UpperDivisionIndependentStudy => 1 << 6,
CourseLevelFilter::Graduate => 1 << 5,
CourseLevelFilter::GraduateIndependentStudy => 1 << 4,
CourseLevelFilter::GraduateResearch => 1 << 3,
CourseLevelFilter::Lvl300 => 1 << 2,
CourseLevelFilter::Lvl400 => 1 << 1,
CourseLevelFilter::Lvl500 => 1 << 0,
};
self
}
/// Only shows courses based on the specified day(s).
///
/// # Parameters
/// - `day`: The day.
///
/// # Returns
/// The `SearchRequestBuilder`
pub fn apply_day(mut self, day: DayOfWeek) -> Self {
let day = match day {
DayOfWeek::Monday => 1,
DayOfWeek::Tuesday => 2,
DayOfWeek::Wednesday => 3,
DayOfWeek::Thursday => 4,
DayOfWeek::Friday => 5,
DayOfWeek::Saturday => 6,
DayOfWeek::Sunday => 7,
};
self.days |= 1 << (7 - day);
self
}
/// Sets the start time to the specified time.
///
/// # Parameters
/// - `hour`: The hour. This should be between 0 and 23, inclusive.
/// - `min`: The minute. This should be between 0 and 59, inclusive.
///
/// # Returns
/// The `SearchRequestBuilder`
pub fn set_start_time(mut self, hour: TimeType, min: TimeType) -> Self {
if hour > 23 || min > 59 {
return self;
}
self.start_time = Some((hour, min));
self
}
/// Sets the end time to the specified time.
///
/// # Parameters
/// - `hour`: The hour. This should be between 0 and 23, inclusive.
/// - `min`: The minute. This should be between 0 and 59, inclusive.
///
/// # Returns
/// The `SearchRequestBuilder`
pub fn set_end_time(mut self, hour: TimeType, min: TimeType) -> Self {
if hour > 23 || min > 59 {
return self;
}
self.end_time = Some((hour, min));
self
}
/// Whether to only show sections with open seats.
///
/// # Returns
/// The `SearchRequestBuilder`
pub fn only_allow_open(mut self) -> Self {
self.only_open = true;
self
}
}
impl Default for SearchRequestBuilder {
fn default() -> Self {
Self::new()
}
}
/// The day of week enum, which designates what days you want
/// to filter specific sections by.
#[derive(PartialOrd, PartialEq, Debug)]
pub enum DayOfWeek {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday,
}
/// The course level filter enum, which can be used to filter
/// specific sections by.
pub enum CourseLevelFilter {
/// Level 1-99 courses.
LowerDivision,
/// Level 87, 90 courses.
FreshmenSeminar,
/// Level 99 courses.
LowerDivisionIndependentStudy,
/// Level 100-198 courses
UpperDivision,
/// Level 195 courses
Apprenticeship,
/// Level 199 courses
UpperDivisionIndependentStudy,
/// Level 200-297 courses
Graduate,
/// Level 298 courses
GraduateIndependentStudy,
/// Level 299 courses
GraduateResearch,
/// Level 300+ courses
Lvl300,
/// Level 400+ courses
Lvl400,
/// Level 500+ courses
Lvl500,
}
/// Lets you choose how you want to search for a course. It is recommended that
/// you use one of the associated functions to create this enum instance.
pub enum SearchType {
/// Searches for a course by section ID.
BySection(String),
/// Searches for a course by more than one section ID.
ByMultipleSections(Vec<String>),
/// Searches for a (set of) course(s) by multiple specifications.
Advanced(SearchRequestBuilder),
}