~nicohman/ravenlib

b42e537ff1234c099541382ad5e7bf56224e502f — nicohman 1 year, 9 months ago 2b23cc0 + d3c94c5
Merge branch 'master' of git.sr.ht:~nicohman/ravenlib
6 files changed, 93 insertions(+), 10 deletions(-)

A .build.yml
M Cargo.toml
M src/lib.rs
M src/ravenserver.rs
M src/themes.rs
M tests/raven
A .build.yml => .build.yml +22 -0
@@ 0,0 1,22 @@
image: archlinux
packages:
  - cargo
  - cmake
  - fontconfig
  - rsync
  - python3
  - git
sources:
  - https://git.sr.ht/~nicohman/ravenlib
tasks:
  - update: |
      cd ravenlib/tests
      git checkout master
      git pull
  - test: |
      cd ravenlib
      cargo test
triggers:
  - action: email
    condition: failure
    to: Nico Hickman <nicohman@demenses.net>

M Cargo.toml => Cargo.toml +5 -1
@@ 1,6 1,6 @@
[package]
name = "ravenlib"
version = "1.2.2"
version = "1.2.3"
authors = ["nicohman <nicohman@demenses.net>"]
repository="https://git.sr.ht/~nicohman/ravenlib"
description="Library for raven theme manager"


@@ 22,4 22,8 @@ dirs = "1.0"
error-chain = "0.12.0"
[dev-dependencies]
time = "0.1"
human-panic = "1.0.1"
structopt = "0.2.10"
[features]
default = ["logging"]
logging = []

M src/lib.rs => src/lib.rs +5 -1
@@ 82,6 82,7 @@ pub mod config {
            println!(
                    "The config file format has changed. Please check ~/.config/raven/config.json to reconfigure raven."
                );

        }
        let mut file = OpenOptions::new()
            .create(true)


@@ 89,6 90,7 @@ pub mod config {
            .open(get_home() + "/.config/raven/config.json")?;
        let default = serde_json::to_string(&Config::default())?;
        file.write_all(default.as_bytes())?;
        #[cfg(feature = "logging")]
        println!("Correctly initialized base config and directory structure.");
        Ok(())
    }


@@ 155,7 157,7 @@ pub mod config {
            .create(true)
            .write(true)
            .open(get_home() + "/.config/raven/themes/" + &theme_name + "/theme.json")?
            .write_all(serde_json::to_string(&themes).unwrap().as_bytes())?;
            .write_all(serde_json::to_string(&themes)?.as_bytes())?;
        Ok(themes)
    }
    pub fn load_store<N>(theme: N) -> Result<ThemeStore>


@@ 188,6 190,7 @@ pub mod daemon {
            .arg("-c")
            .arg("ravend")
            .spawn()?;
        #[cfg(feature = "logging")]
        println!("Started cycle daemon.");
        Ok(child)
    }


@@ 197,6 200,7 @@ pub mod daemon {
            .arg("-SIGKILL")
            .arg("ravend")
            .output()?;
        #[cfg(feature = "logging")]
        println!("Stopped cycle daemon.");
        Ok(())
    }

M src/ravenserver.rs => src/ravenserver.rs +42 -2
@@ 58,9 58,11 @@ where
            get_home() + "/.config/raven/themes/" + &theme_name,
        )?;
        b.into_inner()?;
        #[cfg(feature = "logging")]
        println!("Wrote theme to {}", tname);
        Ok(tname)
    } else {
        #[cfg(feature = "logging")]
        println!("Theme does not exist");
        Err(ErrorKind::InvalidThemeName(theme_name).into())
    }


@@ 74,6 76,7 @@ where
    let fd = File::open(fname)?;
    let mut arch = Archive::new(fd);
    arch.unpack(get_home() + "/.config/raven/themes/")?;
    #[cfg(feature = "logging")]
    println!("Imported theme.");
    Ok(())
}


@@ 94,6 97,7 @@ fn up_info(inf: UserInfo) -> Result<()>{
/// Logs a user out by deleting the userinfo file
pub fn logout() -> Result<()> {
    fs::remove_file(get_home() + "/.config/raven/ravenserver.json")?;
    #[cfg(feature = "logging")]
    println!("Successfully logged you out");
    Ok(())
}


@@ 122,20 126,25 @@ where
        .send()?;

        if res.status().is_success() {
            #[cfg(feature = "logging")]
            println!("Successfully deleted user and all owned themes. Logging out");
            logout()?;
            Ok(())
        } else {
            if res.status() == reqwest::StatusCode::FORBIDDEN {
                #[cfg(feature = "logging")]
                println!("You are trying to delete a user you are not. Bad!");
                Err(ErrorKind::Server(RavenServerErrorKind::PermissionDenied.into()).into())
            } else if res.status() == reqwest::StatusCode::UNAUTHORIZED {
                #[cfg(feature = "logging")]
                println!("You're trying to delete a user w/o providing authentication credentials");
                Err(ErrorKind::Server(RavenServerErrorKind::NotLoggedIn.into()).into())
            } else if res.status() == reqwest::StatusCode::NOT_FOUND {
                #[cfg(feature = "logging")]
                println!("You're trying to delete a user that doesn't exist");
                Err(ErrorKind::Server(RavenServerErrorKind::DoesNotExist(info.name).into()).into())
            } else {
                #[cfg(feature = "logging")]
                println!("Server error. Code {:?}", res.status());
                Err(ErrorKind::Server(RavenServerErrorKind::ServerError(res.status()).into()).into())
            }


@@ 153,24 162,29 @@ where
            .post(&(get_host()? + "/themes/user/create?name=" + &name + "&pass=" + &pass))
            .send()?;
            if res.status().is_success() {
                #[cfg(feature = "logging")]
                println!("Successfully created user. Sign in with `raven login [name] [password]`");
                Ok(true)
            } else {
                if res.status() == reqwest::StatusCode::FORBIDDEN {
                    #[cfg(feature = "logging")]
                    println!("User already created. Pick a different name!");
                    Err(ErrorKind::Server(RavenServerErrorKind::PermissionDenied.into()).into())
                } else if res.status() == reqwest::StatusCode::PAYLOAD_TOO_LARGE {
                    #[cfg(feature = "logging")]
                    println!(
                            "Either your username or password was too long. The limit is 20 characters for username, and 100 for password."
                        );
                        Err(ErrorKind::Server(RavenServerErrorKind::TooLarge.into()).into())
                } else {
                    #[cfg(feature = "logging")]
                    println!("Server error. Code {:?}", res.status());
                    Err(ErrorKind::Server(RavenServerErrorKind::ServerError(res.status()).into()).into())
                }
            }

    } else {
        #[cfg(feature = "logging")]
        println!("Passwords need to match");
        return Ok(false);
    }


@@ 181,7 195,7 @@ where
    N: Into<String>,
{
    let name = name.into();
    let info = load_info().unwrap();
    let info = load_info()?;
    if fs::metadata(get_home() + "/.config/raven/themes/" + &name).is_ok() {
        let tname = export(name.as_str(), true)?;
            let form = reqwest::multipart::Form::new()


@@ 193,6 207,7 @@ where
                if res.status().is_success() {
                    let mut up = false;
                    if res.status() == reqwest::StatusCode::CREATED {
                        #[cfg(feature = "logging")]
                        println!("Theme successfully uploaded.");
                        up = true;
                    }


@@ 205,14 220,17 @@ where
                    Ok(up)
                } else {
                    if res.status() == reqwest::StatusCode::FORBIDDEN {
                        #[cfg(feature = "logging")]
                        println!("That theme already exists, and you are not its owner.");
                        Err(ErrorKind::Server(RavenServerErrorKind::PermissionDenied.into()).into())
                    } else {
                        #[cfg(feature = "logging")]
                        println!("Server error. Code {:?}", res.status());
                        Err(ErrorKind::Server(RavenServerErrorKind::ServerError(res.status()).into()).into())
                    }
                }
    } else {
        #[cfg(feature = "logging")]
        println!("That theme does not exist");
        Err(ErrorKind::InvalidThemeName(name).into())
    }


@@ 261,24 279,30 @@ where
        )
        .send()?;
        if res.status().is_success() {
            #[cfg(feature = "logging")]
            println!("Successfully updated theme metadata");
            Ok(())
        } else {
            if res.status() == reqwest::StatusCode::NOT_FOUND {
                #[cfg(feature = "logging")]
                println!("That theme hasn't been published");
                Err(ErrorKind::Server(RavenServerErrorKind::DoesNotExist(name).into()).into())
            } else if res.status() == reqwest::StatusCode::FORBIDDEN {
                #[cfg(feature = "logging")]
                println!("Can't edit the metadata of a theme that isn't yours");
                Err(ErrorKind::Server(RavenServerErrorKind::PermissionDenied.into()).into())
            } else if res.status() == reqwest::StatusCode::PRECONDITION_FAILED {
                #[cfg(feature = "logging")]
                println!("That isn't a valid metadata type");
                Err(ErrorKind::Server(RavenServerErrorKind::PreConditionFailed("metadata type".to_string()).into()).into())
            } else if res.status() == reqwest::StatusCode::PAYLOAD_TOO_LARGE {
                #[cfg(feature = "logging")]
                println!(
                        "Your description or screenshot url was more than 200 characters long. Please shorten it."
                    );
                Err(ErrorKind::Server(RavenServerErrorKind::TooLarge.into()).into())
            } else {
                #[cfg(feature = "logging")]
                println!("Server error. Code {:?}", res.status());
                Err(ErrorKind::Server(RavenServerErrorKind::ServerError(res.status()).into()).into())
            }


@@ 296,19 320,24 @@ where
        .post(&(get_host()? + "/themes/delete/" + &name + "?token=" + &info.token))
        .send()?;
        if res.status().is_success() {
            #[cfg(feature = "logging")]
            println!("Successfully unpublished theme");
            Ok(())
        } else {
            if res.status() == reqwest::StatusCode::NOT_FOUND {
                #[cfg(feature = "logging")]
                println!("Can't unpublish a nonexistent theme");
                Err(ErrorKind::Server(RavenServerErrorKind::DoesNotExist(name).into()).into())
            } else if res.status() == reqwest::StatusCode::FORBIDDEN {
                #[cfg(feature = "logging")]
                println!("Can't unpublish a theme that isn't yours");
                Err(ErrorKind::Server(RavenServerErrorKind::PermissionDenied.into()).into())
            } else if res.status() == reqwest::StatusCode::UNAUTHORIZED {
                #[cfg(feature = "logging")]
                println!("Did not provide a valid login token");
                Err(ErrorKind::Server(RavenServerErrorKind::PermissionDenied.into()).into())
            } else {
                #[cfg(feature = "logging")]
                println!("Server error. Code {:?}", res.status());
                Err(ErrorKind::Server(RavenServerErrorKind::ServerError(res.status()).into()).into())
            }


@@ 365,8 394,10 @@ where
                            name
                        );
                    import(tname.as_str())?;
                    #[cfg(feature = "logging")]
                    println!("Imported theme. Removing archive.");
                    fs::remove_file(&tname)?;
                    #[cfg(feature = "logging")]
                    println!("Downloading metadata.");
                    let meta = get_metadata(name.as_str())?;
                    let mut st = load_store(name.as_str())?;


@@ 388,19 419,23 @@ where
                    }
                    Ok(true)
                } else {
                    #[cfg(feature = "logging")]
                    println!("Removing downloaded archive.");
                    fs::remove_file(&tname)?;
                    Ok(false)
                }
            } else {
                if res.status() == reqwest::StatusCode::ALREADY_REPORTED {
                    #[cfg(feature = "logging")]
                    print!(
                            "This theme has recently been reported, and has not been approved by an admin. It is not advisable to install this theme. Continuing because of --force."
                        );
                }
                import(tname.as_str())?;
                #[cfg(feature = "logging")]
                println!("Imported theme. Removing archive.");
                fs::remove_file(tname)?;
                #[cfg(feature = "logging")]
                println!("Downloading metadata.");
                let meta = get_metadata(name.as_str())?;
                let mut st = load_store(name.as_str())?;


@@ 423,9 458,11 @@ where
            }
        } else {
            if res.status() == reqwest::StatusCode::NOT_FOUND {
                #[cfg(feature = "logging")]
                println!("Theme has not been uploaded");
                Err(ErrorKind::Server(RavenServerErrorKind::DoesNotExist(name).into()).into())
            } else {
                #[cfg(feature = "logging")]
                println!("Server error. Code {:?}", res.status());
                Err(ErrorKind::Server(RavenServerErrorKind::ServerError(res.status()).into()).into())
            }


@@ 441,15 478,18 @@ where
        .get(&(get_host()? + "/themes/user/login?name=" + &name.into() + "&pass=" + &pass.into()))
        .send()?;
        if res.status().is_success() {
            #[cfg(feature = "logging")]
            println!("Successfully signed in. Writing login info to disk.");
            let info = res.json().unwrap();
            let info = res.json()?;
            up_info(info)?;
            Ok(())
        } else {
            if res.status() == reqwest::StatusCode::FORBIDDEN {
                #[cfg(feature = "logging")]
                println!("Wrong login info. Try again!");
                Err(ErrorKind::Server(RavenServerErrorKind::PermissionDenied.into()).into())
            } else {
                #[cfg(feature = "logging")]
                println!("Server error. Code {:?}", res.status());
                Err(ErrorKind::Server(RavenServerErrorKind::ServerError(res.status()).into()).into())
            }

M src/themes.rs => src/themes.rs +18 -5
@@ 87,11 87,13 @@ impl Theme {
            "st_subltheme" => self.load_sublt("st_subltheme", v.as_str())?,
            "vscode" => self.load_vscode(v.as_str())?,
            _ => {
                #[cfg(feature = "logging")]
                println!("Unrecognized key {}", k);
                ok = false;
                false
            }
        };
        #[cfg(feature = "logging")]
        println!("Loaded key option {}", k);
        Ok(ok)
    }


@@ 116,6 118,7 @@ impl Theme {
            .map(|x| x.to_owned())
            .collect();
        up_theme(store)?;
        #[cfg(feature = "logging")]
        println!("Converted option {} to new key-value system", key);
        Ok(self.load_k(key, value)?)
    }


@@ 127,6 130,7 @@ impl Theme {
        let len = opt.len();
        while i <= len {
            let ref option = opt[len - i];
            #[cfg(feature = "logging")]
            println!("Loading option {}", option.to_string());
            match option {
                Polybar => self.load_poly(self.monitor).unwrap(),


@@ 160,10 164,12 @@ impl Theme {
                    self.convert_single("vscode").unwrap();
                },
            };
            #[cfg(feature = "logging")]
            println!("Loaded option {}", option.to_string());
            i += 1;
        }
        self.load_kv();
        #[cfg(feature = "logging")]
        println!("Loaded all options for theme {}", self.name);
        Ok(())
    }


@@ 288,6 294,7 @@ impl Theme {
        let path1 = get_home() + "/.config/Code/User";
        let path2 = get_home() + "/.config/Code - OSS/User";
        if fs::metadata(&path1).is_err() && fs::metadata(&path2).is_err() {
            #[cfg(feature = "logging")]
            println!(
                "Couldn't find neither .config/Code nor .config/Code - OSS. Do you have VSCode installed? \
                Skipping."


@@ 311,6 318,7 @@ impl Theme {
        let stype = &stype.into();
        let path = get_home() + "/.config/sublime-text-3/Packages/User";
        if fs::metadata(&path).is_err() {
            #[cfg(feature = "logging")]
            println!(
                "Couldn't find {}. Do you have sublime text 3 installed? \
                 Skipping.",


@@ 350,6 358,7 @@ impl Theme {
                get_home() + "/.ncmpcpp/config",
            )?;
        } else {
            #[cfg(feature = "logging")]
            println!(
                "Couldn't detect a ncmpcpp config directory in ~/.config/ncmppcp or ~/.ncmpcpp."
            );


@@ 387,10 396,8 @@ impl Theme {
        }
        let mut app = String::new();
        if isw {
            fs::File::open(get_home() + "/.config/raven/themes/" + &self.name + "/wm")
                .unwrap()
                .read_to_string(&mut app)
                .unwrap();
            fs::File::open(get_home() + "/.config/raven/themes/" + &self.name + "/wm")?
                .read_to_string(&mut app)?;
        } else {
            fs::File::open(get_home() + "/.config/raven/themes/" + &self.name + "/i3")?
                .read_to_string(&mut app)?;


@@ 474,6 481,7 @@ where
        let mut conf = get_config()?;
        conf.editing = theme_name.to_string();
        up_config(conf)?;
        #[cfg(feature = "logging")]
        println!("You are now editing the theme {}", &theme_name);
        Ok(theme_name)
    } else {


@@ 505,6 513,7 @@ where
        run_theme(&load_theme(last.trim())?)?;
        Ok(())
    } else {
        #[cfg(feature = "logging")]
        println!("No last theme saved. Cannot refresh.");
        Err(ErrorKind::InvalidThemeName(last).into())
    }


@@ 589,6 598,7 @@ where
    let mut i = 0;
    while i < new_themes.options.len() {
        if &new_themes.options[i] == &option {
            #[cfg(feature = "logging")]
            println!("Found option {}", option);
            found = true;
            new_themes.options.remove(i);


@@ 599,6 609,7 @@ where
        up_theme(new_themes)?;
        Ok(())
    } else {
        #[cfg(feature = "logging")]
        println!("Couldn't find option {}", option);
        Err(ErrorKind::InvalidThemeName(theme_name).into())
    }


@@ 645,10 656,11 @@ where
    let conf = get_config()?;
    let ent_res = fs::read_dir(get_home() + "/.config/raven/themes/" + &theme_name);
    if ent_res.is_ok() {
        #[cfg(feature = "logging")]
        println!("Found theme {}", theme_name);
        if fs::metadata(get_home() + "/.config/raven/themes/" + &theme_name + "/theme.json").is_ok()
        {
            let theme_info = load_store(theme_name.as_str()).unwrap();
            let theme_info = load_store(theme_name.as_str())?;
            let opts: Vec<ROption> = theme_info
                .options
                .iter()


@@ 673,6 685,7 @@ where
            Err(ErrorKind::InvalidThemeName(theme_name).into())
        }
    } else {
        #[cfg(feature = "logging")]
        println!("Theme does not exist.");
        Err(ErrorKind::InvalidThemeName(theme_name).into())
    }

M tests/raven => tests/raven +1 -1
@@ 1,1 1,1 @@
Subproject commit 88021c863e6c4182eb5376f35530c6401859c1f5
Subproject commit ed314b9371fc434e95be485eb0639586f9dc26c0