pub struct Canvas {}
Expand description

Represents the main interface for interacting with the Canvas Learning Management System (LMS).

This structure provides methods for performing various operations related to the Canvas LMS, such as fetching courses or handling authentication. It acts as a central point for accessing the functionalities offered by the Canvas API.

Examples

// Example of using the Canvas struct to fetch courses
let canvas = Canvas { /* fields initialization */ };
match canvas.fetch_courses_with_credentials(&canvas_info) {
    Ok(courses) => println!("Courses retrieved: {:?}", courses),
    Err(e) => eprintln!("Error fetching courses: {:?}", e),
}

Implementations§

source§

impl Canvas

Implementation of Canvas struct functionalities.

Provides methods to interact with the Canvas Learning Management System (LMS), encapsulating the logic for operations such as fetching courses, managing credentials, and other interactions with the Canvas API.

Methods

  • fetch_courses_with_credentials: Fetches courses using specific Canvas credentials.
  • fetch_courses: Retrieves courses using stored or system-provided credentials.
  • load_credentials_from_file: Loads Canvas credentials from a configuration file.
  • load_credentials_from_system: Loads Canvas credentials stored in the system keyring.

Each method focuses on a specific aspect of Canvas LMS interaction, ensuring ease of use in various application contexts.

source

pub fn fetch_courses_with_credentials(info: &CanvasInfo) -> CanvasResult

Fetches a list of courses from the Canvas API using the provided credentials.

This function attempts to retrieve all courses accessible with the given Canvas credentials. It utilizes the Canvas API endpoint to gather course information, authenticating the request with the credentials supplied in the CanvasInfo structure.

Arguments
  • info - A reference to CanvasInfo containing the URL and API token for Canvas API access.
Returns

A CanvasResult enum, which can be either:

  • CanvasResult::Ok(Vec<Course>) on successful retrieval of courses.
  • CanvasResult::ErrConnection(String) for connection-related errors.
  • CanvasResult::ErrCredentials(String) for authentication failures.
Examples
let canvas_info = CanvasInfo { /* fields initialization */ };
match fetch_courses_with_credentials(&canvas_info) {
    CanvasResult::Ok(courses) => println!("Courses fetched: {:?}", courses),
    CanvasResult::ErrConnection(err) => eprintln!("Connection error: {}", err),
    CanvasResult::ErrCredentials(err) => eprintln!("Credentials error: {}", err),
}
source

pub fn fetch_single_course_with_credentials( info: &CanvasInfo, course_id: u64 ) -> CanvasResult

Fetches details of a specific course from the Canvas API using provided credentials.

This function retrieves information about a single course, identified by course_id, using the credentials provided in info. It communicates with the Canvas API and returns the course data in a CanvasResult.

Arguments
  • info - A reference to CanvasInfo containing the URL and API token for Canvas API access.
  • course_id - The unique identifier for the course to be fetched.
Returns

A CanvasResult enum, which is either:

  • CanvasResult::Ok(Vec<Course>) on successful retrieval of the course.
  • CanvasResult::ErrConnection(String) for connection-related errors.
  • CanvasResult::ErrCredentials(String) for authentication failures.
Examples
let canvas_info = CanvasInfo { /* fields initialization */ };
let course_id = 123;
match fetch_single_course_with_credentials(&canvas_info, course_id) {
    CanvasResult::Ok(course) => println!("Course fetched: {:?}", course),
    CanvasResult::ErrConnection(err) => eprintln!("Connection error: {}", err),
    CanvasResult::ErrCredentials(err) => eprintln!("Credentials error: {}", err),
}
source

pub fn fetch_courses<F>(f: F) -> CanvasResultwhere F: FnOnce(&CanvasInfo) -> CanvasResult + Copy,

Fetches a list of courses using a provided function for specific fetching logic.

This higher-order function takes another function f as an argument, which contains the logic for fetching courses. It allows for customization in the way courses are retrieved, while fetch_courses itself handles credential management and error handling.

Type Parameters
  • F: A function or closure that takes a reference to CanvasInfo and returns CanvasResult.
Arguments
  • f - The function or closure that will be called with the loaded CanvasInfo to fetch courses.
Returns

A CanvasResult enum, either:

  • CanvasResult::Ok(Vec<Course>) on successful retrieval of courses.
  • CanvasResult::ErrConnection(String) for connection-related errors.
  • CanvasResult::ErrCredentials(String) for authentication failures.
Examples
match Canvas::fetch_courses(|credential| Canvas::fetch_courses_with_credentials(credential)) {
    CanvasResult::Ok(courses) => println!("Courses fetched: {:?}", courses),
    CanvasResult::ErrConnection(err) => eprintln!("Connection error: {}", err),
    CanvasResult::ErrCredentials(err) => eprintln!("Credentials error: {}", err),
}

or

let course_id = 123;
match Canvas::fetch_courses(|credential| Canvas::fetch_single_course_with_credentials(credential, course_id)) {
   CanvasResult::Ok(courses) => println!("Courses fetched: {:?}", courses),
   CanvasResult::ErrConnection(err) => eprintln!("Connection error: {}", err),
   CanvasResult::ErrCredentials(err) => eprintln!("Credentials error: {}", err),
}
source

pub fn load_credentials_from_file() -> Result<CanvasInfo, String>

Loads Canvas credentials from a configuration file.

This function attempts to read Canvas API credentials from a predefined configuration file. It looks for a file named config.json in the user’s Downloads directory and tries to deserialize the contents into a CanvasInfo structure. The CanvasInfo contains the base URL and API token required for authenticating Canvas API requests.

Returns

Returns a Result with either:

  • Ok(CanvasInfo) containing the loaded credentials on successful file reading and deserialization.
  • Err(String) with an error message if the file cannot be read, the path is invalid, or the contents cannot be deserialized into CanvasInfo.
Examples
match load_credentials_from_file() {
    Ok(canvas_info) => println!("CanvasInfo loaded: {:?}", canvas_info),
    Err(e) => eprintln!("Error loading credentials: {}", e),
}
source

pub fn load_credentials_from_system() -> Result<CanvasInfo, String>

Loads Canvas credentials from the system’s keyring.

This function retrieves the Canvas API credentials (URL and token) stored in the system’s keyring. It uses the keyring crate to access the secure storage provided by the operating system. The credentials are expected to be stored under the application’s name, fetched from the CARGO_PKG_NAME environment variable. This approach enhances security by avoiding plain text storage of sensitive information.

Returns

Returns a Result with either:

  • Ok(CanvasInfo) containing the credentials if successfully retrieved from the system’s keyring.
  • Err(String) with an error message if there are issues accessing the keyring or retrieving the credentials.
Examples
match load_credentials_from_system() {
    Ok(canvas_info) => println!("CanvasInfo loaded from system: {:?}", canvas_info),
    Err(e) => eprintln!("Error loading credentials from system: {}", e),
}

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more