Initial commit

This commit is contained in:
Jana Lemke 2023-05-24 11:11:12 +02:00
commit 526d6a0c7d
3 changed files with 105 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
Cargo.lock

13
Cargo.toml Normal file
View file

@ -0,0 +1,13 @@
[package]
name = "ibis"
version = "0.1.0"
authors = ["Jana Lemke <dev@jana-lemke.de>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
structopt = "0.3.16"
serde = { version = "1.0", features = ["derive"]}
toml = "0.5.7"
glob = "0.3.0"

90
src/main.rs Normal file
View file

@ -0,0 +1,90 @@
use serde::{Deserialize, Serialize};
use structopt::StructOpt;
use std::path::{Path, PathBuf};
use glob::glob;
use std::fs::File;
use std::io::prelude::*;
#[derive(Serialize, Deserialize)]
struct Config {
link_path: PathBuf,
files_root: PathBuf,
}
#[derive(Serialize, Deserialize)]
struct Course{
name: String,
short: String,
semester: String,
url: Option<String>,
moodle: Option<String>
}
#[derive(StructOpt)]
struct Cli {
#[structopt(subcommand)]
cmd: Option<Command>,
}
#[derive(StructOpt)]
enum Command {
List(ObjectStruct),
Select(ObjectStruct),
Add(ObjectStruct),
Init,
}
#[derive(StructOpt)]
struct ObjectStruct {
#[structopt(subcommand)]
object: Option<Object>,
}
#[derive(StructOpt, Debug)]
enum Object {
Courses,
Lectures,
}
fn main() {
let conf = Config{link_path: PathBuf::from("/home/jana/current-course"), files_root: PathBuf::from("/home/jana/uni")};
let args = Cli::from_args();
match args.cmd {
Some(subcommand) => match subcommand {
Command::List(object) => list(object.object, conf),
Command::Add(object) => (),
Command::Select(object) => (),
Command::Init => (),
},
None => println!("No command given"),
}
}
fn list(object: Option<Object>, conf: Config) {
if object.is_some() {
let object = object.unwrap();
match object {
Object::Courses => {list_courses(conf);},
Object::Lectures => list_lectures(),
}
}
}
fn list_courses(conf: Config) -> Vec<Course>{
let mut courses: Vec<Course> = vec![];
let pattern = [conf.files_root.canonicalize().unwrap().to_str().unwrap(), "/**/course.toml"].concat();
let mut handle;
for entry in glob(pattern.as_str()).unwrap(){
let path = entry.unwrap();
let mut coursestring = String::new();
handle = File::open(path).unwrap();
handle.read_to_string(&mut coursestring);
courses.push(toml::from_str(&coursestring).unwrap());
}
for c in &courses{
println!("Course {}({}) in {}, url {:?}, moodle {:?}", c.name, c.short, c.semester, c.url, c.moodle);
}
courses
}
fn list_lectures() {
println!("Listing lectures")
}