Coder Social home page Coder Social logo

grammateus's People

Contributors

mattrob33 avatar

Watchers

 avatar

grammateus's Issues

Make Illegal States Unrepresentable

Hello, @mattrob33!

I found your crate in the "New Crates" section in crates.io. I am interesting - what is your motivation to create this package? Are you a researcher in Greek history, or linguist, or something other?

I am also want to give you a tip how you can improve your code. As I can see a GreekChar is either have two or three bytes, but it isn't represented in its struct:

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GreekChar {
bytes: [u8; 3],
len: u8
}

Therefore, it requires to match its length if it is not 2 or 3, although we know it can not be:

3 => panic!("Not yet implented"), // TODO
_ => false

The approach to solve this problem is called "Make Illegal States Unrepresentable". In your case, we can make GreekChar not a byte array with its length, but enum with either two or three bytes, like this:

pub enum GreekChar {
    Two(u8, u8),
    Three(u8, u8, u8),
}

This will allow you to get rid of unreachable checks such as checking that length is not 2 or 3. For example, this is how is_lowercase_greek can be rewritten for new GreekChar enum (TODO is copied from source code):

pub fn is_lowercase_greek(char: &GreekChar) -> bool {
    match char {
        GreekChar::Two(0xCE, 0xB1..=0xBF) => true,
        GreekChar::Two(0xCF, 0x80..=0x89) => true,
        GreekChar::Two(_, _) => false,
        // TODO: implement is_lowercase for three-length char
        GreekChar::Three(_, _, _) => panic!("Not yet implented"),
    }
}

Now there is no need to check for length other than 2 or 3.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.